Skip to content

Commit

Permalink
add enum for api type values
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Oct 27, 2023
1 parent 189f06e commit e455080
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/Builder/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Diglin\Sylius\ApiClient\SyliusAdminClientBuilder;
use Diglin\Sylius\ApiClient\SyliusShopClientBuilder;
use Kiboko\Plugin\Sylius\MissingAuthenticationMethodException;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;

Expand All @@ -20,7 +21,7 @@ final class Client implements Builder
private ?Node\Expr $httpRequestFactory = null;
private ?Node\Expr $httpStreamFactory = null;
private ?Node\Expr $fileSystem = null;
private string $apiType = '';
private ApiType $apiType;

public const API_ADMIN_KEY = 'admin';
public const API_SHOP_KEY = 'shop';
Expand All @@ -38,7 +39,7 @@ public function withToken(Node\Expr $token, Node\Expr $refreshToken): self
return $this;
}

public function withApiType(string $apiType): self
public function withApiType(ApiType $apiType): self
{
$this->apiType = $apiType;

Expand Down Expand Up @@ -135,9 +136,9 @@ public function getNode(): Node\Expr\MethodCall
private function getClientBuilderNode(): Node\Expr\MethodCall
{
$className = match ($this->apiType) {
self::API_ADMIN_KEY => SyliusAdminClientBuilder::class,
self::API_SHOP_KEY => SyliusShopClientBuilder::class,
self::API_LEGACY_KEY => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder'
ApiType::ADMIN => SyliusAdminClientBuilder::class,
ApiType::SHOP => SyliusShopClientBuilder::class,
ApiType::LEGACY => 'Diglin\\Sylius\\ApiClient\\SyliusClientBuilder',
};

return new Node\Expr\MethodCall(
Expand Down
12 changes: 12 additions & 0 deletions src/Validator/ApiType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Kiboko\Plugin\Sylius\Validator;

enum ApiType: string
{
case ADMIN = 'admin';
case SHOP = 'shop';
case LEGACY = 'legacy';
}
6 changes: 3 additions & 3 deletions src/Validator/ExtractorConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,15 @@ class ExtractorConfigurationValidator implements ConfigurationValidatorInterface
public static function validate(array $item): array
{
switch($item['api_type']) {
case 'admin':
case ApiType::ADMIN:
$endpoints = self::$endpointsAdmin;
$doubleEndpoints = self::$doubleEndpointsAdmin;
break;
case 'shop':
case ApiType::SHOP:
$endpoints = self::$endpointsShop;
$doubleEndpoints = self::$doubleEndpointsShop;
break;
case 'legacy':
case ApiType::LEGACY:
$endpoints = self::$endpointsLegacy;
$doubleEndpoints = self::$doubleEndpointsLegacy;
break;
Expand Down
6 changes: 3 additions & 3 deletions src/Validator/LoaderConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ class LoaderConfigurationValidator implements ConfigurationValidatorInterface
public static function validate(array $item): array
{
$endpoints = match ($item['api_type']) {

Check failure on line 254 in src/Validator/LoaderConfigurationValidator.php

View workflow job for this annotation

GitHub Actions / phpstan

Match expression does not handle remaining value: mixed
'admin' => self::$endpointsAdmin,
'shop' => self::$endpointsShop,
'legacy' => self::$endpointsLegacy
ApiType::ADMIN => self::$endpointsAdmin,
ApiType::SHOP => self::$endpointsShop,
ApiType::LEGACY => self::$endpointsLegacy
};
if (!\in_array($item['type'], array_keys($endpoints))) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR)));
Expand Down

0 comments on commit e455080

Please sign in to comment.