Skip to content

Commit

Permalink
Merge pull request #2 from airlst/feature/refactor
Browse files Browse the repository at this point in the history
Use Saloon PHP on top of Core API
  • Loading branch information
jantinnerezo authored Mar 19, 2024
2 parents adfe4d2 + ec527a8 commit 1e06575
Show file tree
Hide file tree
Showing 28 changed files with 574 additions and 352 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
php-version: 8.2

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run PHPUnit
run: vendor/bin/phpunit

- name: Run PHPStan
run: vendor/bin/phpstan analyze -l 6 src
run: composer phpstan

- name: Run PHPUnit
run: composer test
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
}
],
"require": {
"php": "^8.2",
"guzzlehttp/guzzle": "^6.0|^7.0",
"php": "^8.3"
"saloonphp/saloon": "^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0|^3.14",
Expand All @@ -34,7 +35,7 @@
"test": "./vendor/bin/phpunit",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html .coverage",
"fix": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --config=.php-cs-fixer.php",
"phpstan": "./vendor/bin/phpstan analyse src"
"phpstan": "./vendor/bin/phpstan analyze -l 8 src"
},
"config": {
"sort-packages": true
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/EventResourceContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Contracts;

use Saloon\Http\Response;

interface EventResourceContract
{
public function list(): Response;

public function get(string $eventId): Response;
}
24 changes: 24 additions & 0 deletions src/Contracts/GuestResourceContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Contracts;

use Saloon\Http\Response;

interface GuestResourceContract
{
public function validateCode(string $code): Response;

public function get(string $code): Response;

/**
* @param array<string, mixed> $data
*/
public function create(array $data): Response;

/**
* @param array<string, mixed> $data
*/
public function update(string $code, array $data): Response;
}
88 changes: 20 additions & 68 deletions src/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,44 @@

namespace AirLST\SdkPhp;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\HandlerStack;
use AirLST\SdkPhp\Resources\EventResource;
use AirLST\SdkPhp\Resources\GuestResource;
use Saloon\Http\Connector;

class CoreApi
class CoreApi extends Connector
{
public string $baseURL = 'https://airlst.app';
public string $apiKey;
public string $locale = 'de-DE';
public string $eventId;
protected ?HandlerStack $handler = null;
protected string $baseUrl = 'https://airlst.app/api';

public function setBaseURL(string $baseURL): self
{
$this->baseURL = $baseURL;
public function __construct(protected readonly string $apiKey) {}

return $this;
public function resolveBaseUrl(): string
{
return $this->baseUrl;
}

public function setApiKey(string $apiKey): self
public function setBaseUrl(string $baseUrl): void
{
$this->apiKey = $apiKey;

return $this;
$this->baseUrl = $baseUrl;
}

public function setLocale(string $locale): self
public function event(): EventResource
{
$this->locale = $locale;

return $this;
return new EventResource($this);
}

public function setEventId(string $eventId): self
public function guest(string $eventId): GuestResource
{
$this->eventId = $eventId;
$this->baseUrl = $this->baseUrl . "/events/{$eventId}/guests";

return $this;
return new GuestResource($this);
}

/**
* @return array<string, string>
*/
public function getRequestHeaders(): array
protected function defaultHeaders(): array
{
return [
'content-type' => 'application/json',
'accept' => 'application/json',
'x-api-key' => $this->apiKey,
'accept-language' => $this->locale,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Api-Key' => $this->apiKey
];
}

public function setHandler(HandlerStack $handler): void
{
$this->handler = $handler;
}

/**
* @param string $uri
* @param RequestMethod $method
* @param array<mixed> $options
* @return array<mixed>
*/
public function send(
string $uri,
RequestMethod $method = RequestMethod::GET,
array $options = []
): array
{
try {
$client = new Client([
'base_uri' => $this->baseURL,
'headers' => $this->getRequestHeaders(),
'handler' => $this->handler
]);

$response = $client->request($method->value, "/api$uri", $options);

return json_decode((string) $response->getBody(), true);
} catch (ClientException $exception) {
throw new \RuntimeException(
$exception->getResponse()->getBody()->getContents(),
$exception->getCode(),
$exception
);
}
}
}
13 changes: 0 additions & 13 deletions src/RequestMethod.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Requests/Event/GetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Event;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class GetRequest extends Request
{
protected Method $method = Method::GET;

public function __construct(protected string $eventId) {}

public function resolveEndpoint(): string
{
return '/events/' . $this->eventId;
}
}
18 changes: 18 additions & 0 deletions src/Requests/Event/ListRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Event;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class ListRequest extends Request
{
protected Method $method = Method::GET;

public function resolveEndpoint(): string
{
return '/companies/events';
}
}
35 changes: 35 additions & 0 deletions src/Requests/Guest/CreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Guest;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class CreateRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

/**
* @param array<string, mixed> $data
*/
public function __construct(protected array $data) {}

public function resolveEndpoint(): string
{
return '/';
}

/**
* @return array<string, mixed>
*/
protected function defaultBody(): array
{
return $this->data;
}
}
20 changes: 20 additions & 0 deletions src/Requests/Guest/GetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Guest;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class GetRequest extends Request
{
protected Method $method = Method::GET;

public function __construct(protected string $code) {}

public function resolveEndpoint(): string
{
return '/' . $this->code;
}
}
35 changes: 35 additions & 0 deletions src/Requests/Guest/UpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Guest;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class UpdateRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::PUT;

/**
* @param array<string, mixed> $data
*/
public function __construct(protected string $code, protected array $data) {}

public function resolveEndpoint(): string
{
return '/' . $this->code;
}

/**
* @return array<string, mixed>
*/
protected function defaultBody(): array
{
return $this->data;
}
}
34 changes: 34 additions & 0 deletions src/Requests/Guest/ValidateCodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace AirLST\SdkPhp\Requests\Guest;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class ValidateCodeRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected string $code) {}

public function resolveEndpoint(): string
{
return '/validate-code';
}

/**
* @return array<string, string>
*/
protected function defaultBody(): array
{
return [
'code' => $this->code,
];
}
}
Loading

0 comments on commit 1e06575

Please sign in to comment.