Skip to content

Commit

Permalink
[v1.1.0] compatibility php8.1 and magento 2.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Calef committed Apr 1, 2022
1 parent a9bc4b9 commit 77b8771
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 244 deletions.
23 changes: 5 additions & 18 deletions Model/Cache/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,10 @@

class Identifier
{
/** @var Encryptor */
private $encryptor;

/** @var SerializerInterface */
private $serializer;

/**
* @param Encryptor $encryptor
* @param SerializerInterface $serializer
*/
public function __construct(
Encryptor $encryptor,
SerializerInterface $serializer
) {
$this->encryptor = $encryptor;
$this->serializer = $serializer;
}
private Encryptor $encryptor,
private SerializerInterface $serializer
) {}

/**
* @param ParametersInterface $parameters
Expand All @@ -62,9 +49,9 @@ public function getRegistryKey(string $serviceName, array $data): string

/**
* @param $value
* @return array|mixed
* @return mixed
*/
private function recursiveExtract($value)
private function recursiveExtract($value): mixed
{
if (is_object($value) && method_exists($value, 'toArray')) {
return $value->toArray();
Expand Down
3 changes: 0 additions & 3 deletions Model/Cache/RestApiCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class RestApiCache extends TagScope
/** @var string */
public const CACHE_TAG = 'REST_API_RESULT';

/**
* @param FrontendPool $cacheFrontendPool
*/
public function __construct(FrontendPool $cacheFrontendPool)
{
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
Expand Down
19 changes: 3 additions & 16 deletions Model/ConfigRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,10 @@ class ConfigRequest
/** @var string */
public const REST_API_CONFIG_DEFAULT = 'general';

/** @var ScopeConfigInterface */
private $scopeConfig;

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

/**
* @param ScopeConfigInterface $scopeConfig
* @param string $configName
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
string $configName
) {
$this->scopeConfig = $scopeConfig;
$this->configName = $configName;
}
private ScopeConfigInterface $scopeConfig,
private string $configName
) {}

/**
* @return string
Expand Down
41 changes: 10 additions & 31 deletions Model/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,21 @@

class HttpClient
{
/** @var GuzzleClient */
private $guzzleClientFactory;

/** @var SerializerInterface */
private $serializer;

/** @var RestApiCache */
private $restApiCache;

/** @var Identifier */
private $identifier;

/** @var LoggerInterface */
private $logger;

public function __construct(
GuzzleClientFactory $guzzleClientFactory,
SerializerInterface $serializer,
RestApiCache $restApiCache,
Identifier $identifier,
LoggerInterface $logger
) {
$this->guzzleClientFactory = $guzzleClientFactory;
$this->serializer = $serializer;
$this->restApiCache = $restApiCache;
$this->identifier = $identifier;
$this->logger = $logger;
}
private GuzzleClientFactory $guzzleClientFactory,
private SerializerInterface $serializer,
private RestApiCache $restApiCache,
private Identifier $identifier,
private LoggerInterface $logger
) {}

/**
* @param ParametersInterface $parameters
* @return array|bool|float|int|string|null
* @return string|int|bool|array|null|float
* @throws ExternalException
* @throws InternalException
*/
public function request(ParametersInterface $parameters)
public function request(ParametersInterface $parameters): string|int|bool|array|null|float
{
$config = $parameters->getConfig();
$context['method'] = __METHOD__;
Expand Down Expand Up @@ -169,9 +148,9 @@ private function guzzleRequest(ParametersInterface $parameters): ResponseInterfa

/**
* @param ParametersInterface $parameters
* @return array|bool|float|int|string|null
* @return string|int|bool|array|null|float
*/
private function getCache(ParametersInterface $parameters)
private function getCache(ParametersInterface $parameters): string|int|bool|array|null|float
{
$cacheKey = $this->identifier->getCacheKey($parameters);
$cache = $this->restApiCache->load($cacheKey);
Expand Down
2 changes: 1 addition & 1 deletion Model/ParametersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Magento\Framework\DataObject;

/**
* @method DataObject toArray()
* @method array toArray()
*/
interface ParametersInterface
{
Expand Down
6 changes: 3 additions & 3 deletions Model/RequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class RequestAdapter
protected const HEADER_CONTENT_TYPE = 'Content-Type';

/** @var string */
protected const CONTENT_JSON = 'application/json';
protected const CONTENT_TYPE = 'application/json';

/** @var string */
protected const HEADER_ACCEPT_LANGUAGE = 'Accept-Language';
Expand Down Expand Up @@ -55,8 +55,8 @@ public function getBody(): array
public function getHeaders(): array
{
return [
self::HEADER_ACCEPT => self::CONTENT_JSON,
self::HEADER_CONTENT_TYPE => self::CONTENT_JSON
self::HEADER_ACCEPT => static::CONTENT_TYPE,
self::HEADER_CONTENT_TYPE => static::CONTENT_TYPE
];
}

Expand Down
44 changes: 8 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,12 @@ class FoxtrotOrderRequestAdapter extends RequestAdapter
];
}

/**
* {@inheritDoc}
*/
public function getHeaders(): array
{
return [
self::HEADER_ACCEPT => self::CONTENT_JSON,
self::HEADER_CONTENT_TYPE => self::CONTENT_JSON,
];
}

/**
* {@inheritDoc}
*/
public function getUri(): ?string
{
return self::SERVICE_ENDPOINT;
}

/**
* {@inheritDoc}
*/
public function getCacheKey(): ?string
{
return null;
return $this->order->getEntityId();
}
}
```
Expand Down Expand Up @@ -222,19 +203,10 @@ use Zepgram\Sales\Api\OrderRepositoryInterface;

class OrderDataExample
{
/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var ApiFactory */
private $apiFactory;

public function __construct(
OrderRepositoryInterface $orderRepository,
ApiFactory $apiFactory
) {
$this->orderRepository = $orderRepository;
$this->apiFactory = $apiFactory;
}
private OrderRepositoryInterface $orderRepository,
private ApiFactory $apiFactory
) {}

/**
* @param int $orderId
Expand All @@ -244,14 +216,14 @@ class OrderDataExample
public function execute(int $orderId): void
{
try {
// load raw data
// get raw data
$order = $this->orderRepository->get($orderId);
// prepare request
// transform data
$foxtrotApiRequest = $this->apiFactory->get('foxtrot_order', ['order' => $order]);
// send request
$result = $foxtrotApiRequest->sendRequest();
$foxtrotOrderResult = $foxtrotApiRequest->sendRequest();
// handle result
$order->setData('foxtrot_order_result', $result);
$order->setFoxtrotData($foxtrotOrderResult);
$this->orderRepository->save($order);
} catch (ExternalException $e) {
$context['context_error'] = 'We could not reach foxtrot order service'
Expand Down
38 changes: 7 additions & 31 deletions Service/ApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,18 @@

class ApiFactory
{
/** @var ObjectManagerInterface */
private $objectManager;
private array $apiRequestRegistry;

/** @var ApiPoolInterface */
private $apiPool;

/** @var Identifier */
private $identifier;

/** @var DataObjectFactory */
private $dataObjectFactory;

/** @var array */
private $apiRequestRegistry;

/**
* @param ObjectManagerInterface $objectManager
* @param ApiPoolInterface $apiPool
* @param Identifier $identifier
* @param DataObjectFactory $dataObjectFactory
*/
public function __construct(
ObjectManagerInterface $objectManager,
ApiPoolInterface $apiPool,
Identifier $identifier,
DataObjectFactory $dataObjectFactory
) {
$this->objectManager = $objectManager;
$this->apiPool = $apiPool;
$this->identifier = $identifier;
$this->dataObjectFactory = $dataObjectFactory;
}
private ObjectManagerInterface $objectManager,
private ApiPoolInterface $apiPool,
private Identifier $identifier,
private DataObjectFactory $dataObjectFactory
) {}

/**
* @param string $serviceName
* @param mixed $rawData
* @param array $rawData
* @return ApiRequestInterface
* @throws LogicException
*/
Expand Down
13 changes: 3 additions & 10 deletions Service/ApiPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@

class ApiPool implements ApiPoolInterface
{
/** @var array */
private $apiProviders;

/**
* @param array $apiProviders
*/
public function __construct(array $apiProviders = [])
{
$this->apiProviders = $apiProviders;
}
public function __construct(
private array $apiProviders = []
) {}

/**
* {@inheritDoc}
Expand Down
63 changes: 11 additions & 52 deletions Service/ApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,59 +32,18 @@

class ApiProvider implements ApiProviderInterface
{
/** @var ParametersInterfaceFactory */
private $parametersFactory;

/** @var ConfigRequestFactory */
private $configRequestFactory;

/** @var SerializerInterface */
private $serializer;

/** @var RequestAdapter */
private $requestAdapter;

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

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

/** @var bool */
private $isVerify;

/** @var bool */
private $isJsonRequest;

/** @var bool */
private $isJsonResponse;

/** @var Validator|null */
private $validator;

public function __construct(
ParametersInterfaceFactory $parametersFactory,
ConfigRequestFactory $configRequestFactory,
SerializerInterface $serializer,
RequestAdapter $requestAdapter,
string $configName = ConfigRequest::REST_API_CONFIG_DEFAULT,
string $method = Request::METHOD_GET,
bool $isVerify = true,
bool $isJsonRequest = true,
bool $isJsonResponse = true,
Validator $validator = null
) {
$this->parametersFactory = $parametersFactory;
$this->configRequestFactory = $configRequestFactory;
$this->serializer = $serializer;
$this->requestAdapter = $requestAdapter;
$this->configName = $configName;
$this->method = $method;
$this->isVerify = $isVerify;
$this->isJsonRequest = $isJsonRequest;
$this->isJsonResponse = $isJsonResponse;
$this->validator = $validator;
}
private ParametersInterfaceFactory $parametersFactory,
private ConfigRequestFactory $configRequestFactory,
private SerializerInterface $serializer,
private RequestAdapter $requestAdapter,
private string $configName = ConfigRequest::REST_API_CONFIG_DEFAULT,
private string $method = Request::METHOD_GET,
private bool $isVerify = true,
private bool $isJsonRequest = true,
private bool $isJsonResponse = true,
private ?Validator $validator = null
) {}

/**
* {@inheritDoc}
Expand Down
Loading

0 comments on commit 77b8771

Please sign in to comment.