Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared Client Authentication #10

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Client/AccessDeniedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class AccessDeniedException extends \RuntimeException
{
}
final class AccessDeniedException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Client/ApiRateExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class ApiRateExceededException extends \RuntimeException
{
}
final class ApiRateExceededException extends \RuntimeException {}
9 changes: 2 additions & 7 deletions src/Client/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function __construct(
private readonly string $clientSecret,
private string $accessToken,
private readonly string $refreshToken,
) {
}
) {}

public function sendRequest(RequestInterface $request): ResponseInterface
{
Expand Down Expand Up @@ -62,11 +61,7 @@ private function refreshToken(): void
);

if (200 !== $response->getStatusCode()) {
throw new AccessDeniedException(sprintf(
'Something went wrong while refreshing your credentials: %d - "%s"',
$response->getStatusCode(),
$response->getBody()->getContents(),
));
throw new AccessDeniedException(sprintf('Something went wrong while refreshing your credentials: %d - "%s"', $response->getStatusCode(), $response->getBody()->getContents()));
}

$credentials = json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion src/Client/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class BadRequestException extends \RuntimeException
{
public function __construct(string $message, private readonly ResponseInterface $response, int $code = 0, \Throwable|null $previous = null)
public function __construct(string $message, private readonly ResponseInterface $response, int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public function __construct(
private readonly RequestFactoryInterface $requestFactory,
private readonly UriFactoryInterface $uriFactory,
private readonly StreamFactoryInterface $streamFactory,
) {
}
) {}

/**
* @throws ClientExceptionInterface
Expand Down Expand Up @@ -189,7 +188,7 @@ public function searchOrder(string $subject, string $store): array
return $result['data'][0];
}

private function processResponse(ResponseInterface $response, array|null $body = []): void
private function processResponse(ResponseInterface $response, null|array $body = []): void
{
if (400 === $response->getStatusCode()) {
throw new BadRequestException('The format of the request is not correct. Please check the information sent.', $response);
Expand Down
4 changes: 1 addition & 3 deletions src/Client/ForbiddenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class ForbiddenException extends \RuntimeException
{
}
final class ForbiddenException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Client/InternalServerErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class InternalServerErrorException extends \RuntimeException
{
}
final class InternalServerErrorException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Client/InvalidCodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class InvalidCodeException extends \RuntimeException
{
}
final class InvalidCodeException extends \RuntimeException {}
8 changes: 4 additions & 4 deletions src/Client/MultiStatusResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

final class MultiStatusResponseException extends \RuntimeException
{
public function __construct(private readonly ResponseInterface $response, array|null $body = [], int $code = 0, \Throwable|null $previous = null)
public function __construct(private readonly ResponseInterface $response, null|array $body = [], int $code = 0, \Throwable $previous = null)
{
$contents = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
$contents = json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);

$messages = [];
foreach ($contents['data'] as $key => $item) {
if ($item['code'] !== 'SUCCESS') {
if ('SUCCESS' !== $item['code']) {
$messages[] = [
'response' => $item,
'body' => $body[$key],
Expand All @@ -23,7 +23,7 @@ public function __construct(private readonly ResponseInterface $response, array|
}

parent::__construct(
sprintf('Zoho\'s response contains multiple statuses, %d items in the batch may have failed: %s', count($messages), json_encode($messages, JSON_THROW_ON_ERROR)),
sprintf('Zoho\'s response contains multiple statuses, %d items in the batch may have failed: %s', \count($messages), json_encode($messages, \JSON_THROW_ON_ERROR)),
$code,
$previous
);
Expand Down
4 changes: 1 addition & 3 deletions src/Client/NoContentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class NoContentException extends \RuntimeException
{
}
final class NoContentException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Client/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class NotFoundException extends \RuntimeException
{
}
final class NotFoundException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Client/RequestEntityTooLargeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Kiboko\Component\Flow\ZohoCRM\Client;

final class RequestEntityTooLargeException extends \RuntimeException
{
}
final class RequestEntityTooLargeException extends \RuntimeException {}
31 changes: 31 additions & 0 deletions src/Client/SharedAuthenticationMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Flow\ZohoCRM\Client;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

class SharedAuthenticationMiddleware
{
private static ?AuthenticationMiddleware $instance = null;

public static function getInstance(
ClientInterface $decorated,
RequestFactoryInterface $requestFactory,
UriFactoryInterface $uriFactory,
string $oauthBaseUri,
string $clientId,
string $clientSecret,
string $accessToken,
string $refreshToken,
): AuthenticationMiddleware {
if (null === self::$instance) {
self::$instance = new AuthenticationMiddleware($decorated, $requestFactory, $uriFactory, $oauthBaseUri, $clientId, $clientSecret, $accessToken, $refreshToken);
}

return self::$instance;
}
}
8 changes: 3 additions & 5 deletions src/ContactLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

final readonly class ContactLoader implements LoaderInterface
{
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger)
{
}
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger) {}

public function load(): \Generator
{
$line = yield;
do {
try {
$this->client->upsertContacts($line);
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);

yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (ForbiddenException|RequestEntityTooLargeException|NotFoundException $exception) {
} catch (ForbiddenException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (BadRequestException|MultiStatusResponseException $exception) {
Expand Down
7 changes: 3 additions & 4 deletions src/ContactLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public function __construct(
private CacheInterface $cache,
private CompiledMapperInterface $mapper,
private string $mappingField,
) {
}
) {}

public function transform(): \Generator
{
Expand All @@ -42,12 +41,12 @@ public function transform(): \Generator

$this->cache->set($encodedEmail, $lookup);
}
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);

return;
} catch (BadRequestException|ForbiddenException|RequestEntityTooLargeException|NotFoundException|NoContentException $exception) {
} catch (BadRequestException|ForbiddenException|NoContentException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);
continue;
Expand Down
7 changes: 3 additions & 4 deletions src/DealLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
public function __construct(
private Client $client,
private \Psr\Log\LoggerInterface $logger,
) {
}
) {}

public function load(): \Generator
{
$line = yield;
do {
try {
$this->client->upsertDeals($line);
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);

yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (ForbiddenException|RequestEntityTooLargeException|NotFoundException $exception) {
} catch (ForbiddenException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (BadRequestException|MultiStatusResponseException $exception) {
Expand Down
3 changes: 1 addition & 2 deletions src/DealTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class DealTransformer implements TransformerInterface
{
public function __construct(
private string $orderItemsField,
) {
}
) {}

public function transform(): \Generator
{
Expand Down
7 changes: 3 additions & 4 deletions src/GetOrderLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function __construct(
private CompiledMapperInterface $mapper,
private string $subjectMappingField,
private string $storeMappingField,
) {
}
) {}

public function transform(): \Generator
{
Expand Down Expand Up @@ -57,12 +56,12 @@ public function transform(): \Generator
$line = yield new AcceptanceResultBucket($output);
} catch (NoContentException) {
$line = yield new AcceptanceResultBucket($line);
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);

return;
} catch (BadRequestException|ForbiddenException|RequestEntityTooLargeException|NotFoundException $exception) {
} catch (BadRequestException|ForbiddenException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);
continue;
Expand Down
8 changes: 3 additions & 5 deletions src/OrderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

final readonly class OrderLoader implements LoaderInterface
{
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger)
{
}
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger) {}

public function load(): \Generator
{
$line = yield;
do {
try {
$this->client->upsertOrders($line);
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);

yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (ForbiddenException|RequestEntityTooLargeException|NotFoundException $exception) {
} catch (ForbiddenException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (BadRequestException|MultiStatusResponseException $exception) {
Expand Down
7 changes: 3 additions & 4 deletions src/OrderLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function __construct(
private CompiledMapperInterface $mapper,
private string $subjectMappingField,
private string $storeMappingField,
) {
}
) {}

public function transform(): \Generator
{
Expand All @@ -43,12 +42,12 @@ public function transform(): \Generator

$this->cache->set($encodingKey, $lookup);
}
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);

return;
} catch (BadRequestException|ForbiddenException|RequestEntityTooLargeException|NotFoundException|NoContentException $exception) {
} catch (BadRequestException|ForbiddenException|NoContentException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);
continue;
Expand Down
8 changes: 3 additions & 5 deletions src/ProductLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

final readonly class ProductLoader implements LoaderInterface
{
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger)
{
}
public function __construct(private Client $client, private \Psr\Log\LoggerInterface $logger) {}

public function load(): \Generator
{
$line = yield;
do {
try {
$this->client->upsertProducts($line);
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);

yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (ForbiddenException|RequestEntityTooLargeException|NotFoundException $exception) {
} catch (ForbiddenException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
yield new \Kiboko\Component\Bucket\RejectionResultBucket($line);
} catch (BadRequestException|MultiStatusResponseException $exception) {
Expand Down
9 changes: 4 additions & 5 deletions src/ProductLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public function __construct(
private string $mappingField,
private string $orderItemsField,
private string $propertyPath,
) {
}
) {}

public function transform(): \Generator
{
Expand All @@ -46,15 +45,15 @@ public function transform(): \Generator

$this->cache->set(sprintf('product.%s', $item[$this->mappingField]), $lookup);
}
} catch (InternalServerErrorException|ApiRateExceededException $exception) {
} catch (ApiRateExceededException|InternalServerErrorException $exception) {
$this->logger->critical($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);

return;
} catch (BadRequestException|ForbiddenException|RequestEntityTooLargeException|NotFoundException|NoContentException $exception) {
} catch (BadRequestException|ForbiddenException|NoContentException|NotFoundException|RequestEntityTooLargeException $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
$line = yield new RejectionResultBucket($line);
continue;
continue 2;
}

$output = ($this->mapper)(
Expand Down