diff --git a/Model/Cache/Identifier.php b/Model/Cache/Identifier.php index 86fd010..21e4c57 100755 --- a/Model/Cache/Identifier.php +++ b/Model/Cache/Identifier.php @@ -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 @@ -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(); diff --git a/Model/Cache/RestApiCache.php b/Model/Cache/RestApiCache.php index 9bc18d5..49c193c 100755 --- a/Model/Cache/RestApiCache.php +++ b/Model/Cache/RestApiCache.php @@ -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); diff --git a/Model/ConfigRequest.php b/Model/ConfigRequest.php index f96b791..4c42924 100755 --- a/Model/ConfigRequest.php +++ b/Model/ConfigRequest.php @@ -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 diff --git a/Model/HttpClient.php b/Model/HttpClient.php index 2289eb0..e6dc80b 100755 --- a/Model/HttpClient.php +++ b/Model/HttpClient.php @@ -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__; @@ -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); diff --git a/Model/ParametersInterface.php b/Model/ParametersInterface.php index f716d7b..3bd0c65 100755 --- a/Model/ParametersInterface.php +++ b/Model/ParametersInterface.php @@ -18,7 +18,7 @@ use Magento\Framework\DataObject; /** - * @method DataObject toArray() + * @method array toArray() */ interface ParametersInterface { diff --git a/Model/RequestAdapter.php b/Model/RequestAdapter.php index 94a62d6..372aabd 100755 --- a/Model/RequestAdapter.php +++ b/Model/RequestAdapter.php @@ -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'; @@ -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 ]; } diff --git a/README.md b/README.md index 92ca6b8..2bb50ce 100755 --- a/README.md +++ b/README.md @@ -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(); } } ``` @@ -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 @@ -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' diff --git a/Service/ApiFactory.php b/Service/ApiFactory.php index a2de7ff..60ece58 100755 --- a/Service/ApiFactory.php +++ b/Service/ApiFactory.php @@ -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 */ diff --git a/Service/ApiPool.php b/Service/ApiPool.php index 2cf80eb..e9f9f74 100644 --- a/Service/ApiPool.php +++ b/Service/ApiPool.php @@ -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} diff --git a/Service/ApiProvider.php b/Service/ApiProvider.php index 50c442c..3154d9a 100644 --- a/Service/ApiProvider.php +++ b/Service/ApiProvider.php @@ -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} diff --git a/Service/ApiRequest.php b/Service/ApiRequest.php index a5a3463..63bd846 100755 --- a/Service/ApiRequest.php +++ b/Service/ApiRequest.php @@ -16,70 +16,44 @@ namespace Zepgram\Rest\Service; use Magento\Framework\DataObject; +use Magento\Framework\Event\ManagerInterface; use Zepgram\Rest\Model\HttpClient; class ApiRequest implements ApiRequestInterface { - /** @var HttpClient */ - private $httpClient; + private mixed $result = false; - /** @var ApiProviderInterface */ - private $apiProvider; - - /** @var array */ - private $rawData; - - /** @var string */ - private $serviceName; - - /** @var mixed */ - private $result = false; - - /** - * @param HttpClient $httpClient - * @param ApiProviderInterface $apiProvider - * @param DataObject $rawData - * @param string $serviceName - */ public function __construct( - HttpClient $httpClient, - ApiProviderInterface $apiProvider, - DataObject $rawData, - string $serviceName - ) { - $this->httpClient = $httpClient; - $this->apiProvider = $apiProvider; - $this->rawData = $rawData; - $this->serviceName = $serviceName; - } + private HttpClient $httpClient, + private ApiProviderInterface $apiProvider, + private ManagerInterface $eventManager, + private DataObject $rawData, + private string $serviceName + ) {} /** * {@inheritDoc} */ - public function sendRequest() + public function sendRequest(): string|int|bool|array|null|float { if ($result = $this->getResult()) { return $result; } + $this->eventManager->dispatch($this->serviceName . '_send_before', ['raw_data' => $this->rawData]); $parameters = $this->apiProvider->build($this->rawData, $this->serviceName); $result = $this->httpClient->request($parameters); + $this->eventManager->dispatch($this->serviceName . '_send_after', ['result' => $result]); $this->setResult($result); return $result; } - /** - * @return mixed - */ - private function getResult() + private function getResult(): string|int|bool|array|null|float { return $this->result; } - /** - * @param mixed $result - */ private function setResult($result): void { $this->result = $result; diff --git a/Service/ApiRequestInterface.php b/Service/ApiRequestInterface.php index 18395f4..54d7458 100755 --- a/Service/ApiRequestInterface.php +++ b/Service/ApiRequestInterface.php @@ -31,5 +31,5 @@ interface ApiRequestInterface * @throws InternalException * @return mixed */ - public function sendRequest(); + public function sendRequest(): mixed; } diff --git a/composer.json b/composer.json index 362a507..09c389b 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "zepgram/module-rest", "description": "Technical module to industrialize API REST call with dependency injection pattern using Guzzle library in Magento 2", "type": "magento2-module", - "version": "1.0.1", + "version": "1.1.0", "authors": [ { "name": "Benjamin Calef", @@ -10,10 +10,11 @@ } ], "require": { - "guzzlehttp/guzzle": "^6.0.0", - "magento/framework": "^101.0.0|^102.0.0|^103.0.0", + "php": "^8.1", + "guzzlehttp/guzzle": "^7.0", + "magento/framework": "^103.0.4", "zepgram/module-base": "~0.0.1", - "zepgram/module-json-schema": "~0.0.1" + "zepgram/module-json-schema": "~0.1.0" }, "autoload": { "files": [