Skip to content

Commit

Permalink
Upgrade to use PHP 8 syntax (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 4, 2022
1 parent 48f8e71 commit a7279c0
Show file tree
Hide file tree
Showing 31 changed files with 138 additions and 594 deletions.
58 changes: 18 additions & 40 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,30 @@

final class ApiClient implements ApiClientInterface, AttributeOptionsApiClientInterface, FamilyAwareApiClientInterface, MeasurementFamiliesApiClientInterface
{
/** @var string|null */
private $accessToken;
private ?string $accessToken = null;

/** @var string|null */
private $refreshToken;
private ?string $refreshToken = null;

/** @var ClientInterface */
private $httpClient;
private string $baseUrl;

/** @var string */
private $baseUrl;

/** @var string */
private $username;

/** @var string */
private $password;

/** @var string */
private $clientId;

/** @var string */
private $secret;

/** @var TemporaryFilesManagerInterface|null */
private $temporaryFilesManager;
private ?\Webgriffe\SyliusAkeneoPlugin\TemporaryFilesManagerInterface $temporaryFilesManager;

public function __construct(
ClientInterface $httpClient,
private ClientInterface $httpClient,
string $baseUrl,
string $username,
string $password,
string $clientId,
string $secret,
private string $username,
private string $password,
private string $clientId,
private string $secret,
TemporaryFilesManagerInterface $temporaryFilesManager = null
) {
$this->httpClient = $httpClient;
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->clientId = $clientId;
$this->secret = $secret;
if (null === $temporaryFilesManager) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'1.3',
'Not passing a temporary files manager to %s is deprecated and will not be possible anymore in %s',
__CLASS__,
self::class,
'2.0'
);
}
Expand All @@ -75,7 +51,7 @@ public function __construct(
*/
public function authenticatedRequest(string $uri, string $method, array $headers, bool $withRefresh = false): array
{
if (strpos($uri, '/') === 0) {
if (str_starts_with($uri, '/')) {
$uri = $this->baseUrl . $uri;
}

Expand All @@ -99,7 +75,7 @@ public function authenticatedRequest(string $uri, string $method, array $headers
try {
$response = $this->httpClient->send($request);

return (array) json_decode($response->getBody()->getContents(), true);
return (array) json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);
} catch (RequestException $requestException) {
$erroredResponse = $requestException->getResponse();
Assert::notNull($erroredResponse);
Expand Down Expand Up @@ -155,7 +131,7 @@ public function downloadFile(string $code): \SplFileInfo
$statusClass = (int) ($response->getStatusCode() / 100);
$bodyContents = $response->getBody()->getContents();
if ($statusClass !== 2) {
$responseResult = json_decode($bodyContents, true);
$responseResult = json_decode($bodyContents, true, 512, \JSON_THROW_ON_ERROR);

throw new \HttpException($responseResult['message'], $responseResult['code']);
}
Expand Down Expand Up @@ -226,7 +202,8 @@ private function login(): void
'grant_type' => 'password',
'username' => $this->username,
'password' => $this->password,
]
],
\JSON_THROW_ON_ERROR
);
Assert::string($body);
$responseResult = $this->makeOauthRequest($body);
Expand All @@ -241,7 +218,8 @@ private function refreshAccessToken(): void
[
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
]
],
\JSON_THROW_ON_ERROR
);
Assert::string($body);
$responseResult = $this->makeOauthRequest($body);
Expand Down Expand Up @@ -321,7 +299,7 @@ private function makeOauthRequest(string $body): array
$rawResponse = $this->httpClient->send($request, $options);

/** @var array{access_token: string, refresh_token: string, expires_in: int, token_type: string, scope: string|null} $result */
$result = json_decode($rawResponse->getBody()->getContents(), true);
$result = json_decode($rawResponse->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);

return $result;
}
Expand Down
22 changes: 3 additions & 19 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ final class Importer implements ImporterInterface

private const MULTISELECT_TYPE = 'pim_catalog_multiselect';

/** @var ApiClientInterface */
private $apiClient;

/** @var RepositoryInterface */
private $attributeRepository;

public function __construct(ApiClientInterface $apiClient, RepositoryInterface $attributeRepository)
public function __construct(private ApiClientInterface $apiClient, private RepositoryInterface $attributeRepository)
{
$this->apiClient = $apiClient;
$this->attributeRepository = $attributeRepository;
}

public function getAkeneoEntity(): string
Expand All @@ -48,13 +40,7 @@ public function import(string $identifier): void
$attributeOptions = $this->apiClient->findAllAttributeOptions($identifier);
usort(
$attributeOptions,
static function (array $option1, array $option2): int {
if (($option1['sort_order'] ?? 0) === ($option2['sort_order'] ?? 0)) {
return 0;
}

return ($option1['sort_order'] ?? 0) > ($option2['sort_order'] ?? 0) ? 1 : -1;
}
static fn (array $option1, array $option2): int => ($option2['sort_order'] ?? 0) <=> ($option1['sort_order'] ?? 0)
);
$configuration = $attribute->getConfiguration();
$configuration['choices'] = $this->convertAkeneoAttributeOptionsIntoSyliusChoices($attributeOptions);
Expand All @@ -72,9 +58,7 @@ public function getIdentifiersModifiedSince(\DateTime $sinceDate): array
$syliusSelectAttributes = $this->attributeRepository->findBy(['type' => SelectAttributeType::TYPE]);
$syliusSelectAttributes = array_filter(
array_map(
static function (ProductAttributeInterface $attribute): ?string {
return $attribute->getCode();
},
static fn (ProductAttributeInterface $attribute): ?string => $attribute->getCode(),
$syliusSelectAttributes
)
);
Expand Down
18 changes: 3 additions & 15 deletions src/Command/ConsumeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@ final class ConsumeCommand extends Command

protected static $defaultName = 'webgriffe:akeneo:consume';

/** @var QueueItemRepositoryInterface */
private $queueItemRepository;

/** @var ImporterRegistryInterface */
private $importerRegistry;

/** @var ManagerRegistry */
private $managerRegistry;

public function __construct(
QueueItemRepositoryInterface $queueItemRepository,
ImporterRegistryInterface $importerRegistry,
ManagerRegistry $managerRegistry
private QueueItemRepositoryInterface $queueItemRepository,
private ImporterRegistryInterface $importerRegistry,
private ManagerRegistry $managerRegistry
) {
$this->queueItemRepository = $queueItemRepository;
$this->importerRegistry = $importerRegistry;
$this->managerRegistry = $managerRegistry;
parent::__construct();
}

Expand Down
30 changes: 6 additions & 24 deletions src/Command/EnqueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,12 @@ final class EnqueueCommand extends Command

protected static $defaultName = 'webgriffe:akeneo:enqueue';

/** @var QueueItemRepositoryInterface */
private $queueItemRepository;

/** @var FactoryInterface */
private $queueItemFactory;

/** @var DateTimeBuilderInterface */
private $dateTimeBuilder;

/** @var ImporterRegistryInterface */
private $importerRegistry;

public function __construct(
QueueItemRepositoryInterface $queueItemRepository,
FactoryInterface $queueItemFactory,
DateTimeBuilderInterface $dateTimeBuilder,
ImporterRegistryInterface $importerRegistry
private QueueItemRepositoryInterface $queueItemRepository,
private FactoryInterface $queueItemFactory,
private DateTimeBuilderInterface $dateTimeBuilder,
private ImporterRegistryInterface $importerRegistry
) {
$this->queueItemRepository = $queueItemRepository;
$this->queueItemFactory = $queueItemFactory;
$this->dateTimeBuilder = $dateTimeBuilder;
$this->importerRegistry = $importerRegistry;
parent::__construct();
}

Expand Down Expand Up @@ -93,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ('' !== $sinceOptionValue = (string) $input->getOption(self::SINCE_OPTION_NAME)) {
try {
$sinceDate = new \DateTime($sinceOptionValue);
} catch (\Throwable $t) {
} catch (\Throwable) {
throw new \InvalidArgumentException(
sprintf('The "%s" argument must be a valid date', self::SINCE_OPTION_NAME)
);
Expand Down Expand Up @@ -216,9 +200,7 @@ private function getImporters(InputInterface $input): array
throw new \RuntimeException('There are no importers in registry.');
}
$importersCodes = array_map(
static function (ImporterInterface $importer): string {
return $importer->getAkeneoEntity();
},
static fn (ImporterInterface $importer): string => $importer->getAkeneoEntity(),
$allImporters
);

Expand Down
6 changes: 1 addition & 5 deletions src/Command/QueueCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ final class QueueCleanupCommand extends Command

private const DAYS_ARGUMENT_NAME = 'days';

/** @var CleanableQueueItemRepositoryInterface */
private $queueItemRepository;

// the name of the command (the part after "bin/console")
protected static $defaultName = 'webgriffe:akeneo:cleanup-queue';

/**
* QueueCleanupCommand constructor.
*/
public function __construct(CleanableQueueItemRepositoryInterface $queueItemRepository)
public function __construct(private CleanableQueueItemRepositoryInterface $queueItemRepository)
{
$this->queueItemRepository = $queueItemRepository;
parent::__construct();
}

Expand Down
10 changes: 2 additions & 8 deletions src/Command/ReconcileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ final class ReconcileCommand extends Command

protected static $defaultName = 'webgriffe:akeneo:reconcile';

/** @var ReconcilerRegistryInterface */
private $reconciliationRegistry;

/**
* ReconcileCommand constructor.
*/
public function __construct(ReconcilerRegistryInterface $reconciliationRegistry)
public function __construct(private ReconcilerRegistryInterface $reconciliationRegistry)
{
parent::__construct();
$this->reconciliationRegistry = $reconciliationRegistry;
}

protected function configure(): void
Expand Down Expand Up @@ -81,9 +77,7 @@ private function getReconcilers(InputInterface $input): array
return [];
}
$reconcilersCodes = array_map(
static function (ReconcilerInterface $reconciler): string {
return $reconciler->getAkeneoEntity();
},
static fn (ReconcilerInterface $reconciler): string => $reconciler->getAkeneoEntity(),
$allReconcilers
);

Expand Down
20 changes: 4 additions & 16 deletions src/Controller/ProductEnqueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,23 @@

final class ProductEnqueueController extends AbstractController
{
/** @var QueueItemRepositoryInterface */
private $queueItemRepository;

/** @var UrlGeneratorInterface */
private $urlGenerator;

/** @var ProductRepositoryInterface */
private $productRepository;

private ?TranslatorInterface $translator;

/**
* ProductEnqueueController constructor.
*/
public function __construct(
QueueItemRepositoryInterface $queueItemRepository,
ProductRepositoryInterface $productRepository,
UrlGeneratorInterface $urlGenerator,
private QueueItemRepositoryInterface $queueItemRepository,
private ProductRepositoryInterface $productRepository,
private UrlGeneratorInterface $urlGenerator,
TranslatorInterface $translator = null
) {
$this->queueItemRepository = $queueItemRepository;
$this->urlGenerator = $urlGenerator;
$this->productRepository = $productRepository;
if ($translator === null) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'1.12',
'Not passing a translator to "%s" is deprecated and will be removed in %s.',
__CLASS__,
self::class,
'2.0'
);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Converter/UnitMeasurementValueConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ final class UnitMeasurementValueConverter implements UnitMeasurementValueConvert
{
private const RECOGNIZED_OPERATORS = ['add', 'sub', 'mul', 'div'];

/** @var MeasurementFamiliesApiClientInterface */
private $apiClient;

/**
* UnitMeasurementValueConverter constructor.
*/
public function __construct(MeasurementFamiliesApiClientInterface $apiClient)
public function __construct(private MeasurementFamiliesApiClientInterface $apiClient)
{
$this->apiClient = $apiClient;
}

public function convert(string $amount, string $sourceUnitMeasurementCode, ?string $destinationUnitMeasurementCode): float
Expand Down
5 changes: 2 additions & 3 deletions src/Converter/ValueConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

final class ValueConverter implements ValueConverterInterface
{
/** @var TranslatorInterface|null */
private $translator;
private ?TranslatorInterface $translator;

/**
* ValueConverter constructor.
Expand All @@ -25,7 +24,7 @@ public function __construct(
'webgriffe/sylius-akeneo-plugin',
'1.8',
'Not passing a translator to "%s" is deprecated and will be removed in %s.',
__CLASS__,
self::class,
'2.0'
);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Converter/ValueConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@

interface ValueConverterInterface
{
/**
* @param int|string|bool|array $value
*
* @return array|int|string|bool
*/
public function convert(AttributeInterface $attribute, $value, string $localeCode);
public function convert(AttributeInterface $attribute, array|bool|int|string $value, string $localeCode): array|bool|int|string;
}
Loading

0 comments on commit a7279c0

Please sign in to comment.