diff --git a/.gitignore b/.gitignore index dec31c6..6053503 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ *.sublime-* *.AppleDouble *.AppleDB -*.AppleDesktop \ No newline at end of file +*.AppleDesktop diff --git a/.license b/.license index cf29a25..591bbb5 100644 --- a/.license +++ b/.license @@ -1,4 +1,4 @@ /** * MIT License - * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. + * For full license information, please view the LICENSE file that was distributed with this source code. */ diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..e14e073 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,15 @@ +build: + environment: + php: + version: 7.1 + +checks: + php: + code_rating: true + +filter: + excluded_paths: + - config/* + - tests/* + - src/Generated/* + - src/Pyz/* diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d0ad88d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,47 @@ +language: php + +notifications: + email: false + +sudo: required + +matrix: + fast_finish: true + include: + - php: 7.1 + +services: +- postgresql +- redis +- rabbitmq + +addons: + postgresql: 9.4 + + apt: + packages: + - graphviz + + hosts: + - zed.de.spryker.test + - www.de.spryker.test + +env: + global: + - APPLICATION_ENV=devtest + - APPLICATION_STORE=DE + - MODULE_DIR=module + - SHOP_DIR=current + - MODULE_NAME=sevensenders + +cache: + directories: + - $SHOP_DIR/current/vendor + - $HOME/.composer/cache + +before_install: +- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + +script: +- git clone -b 0.1.1 https://github.com/spryker-eco/eco-ci.git ecoci +- ./ecoci/build/travis.sh diff --git a/LICENSE b/LICENSE index 5e95b7b..a60efe3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 SPRYKER SYSTEMS GMBH +Copyright (c) 2016-present Spryker Systems GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..220b648 --- /dev/null +++ b/codeception.yml @@ -0,0 +1,24 @@ +namespace: Sevensenders + +include: + - tests/SprykerEcoTest/Zed/Sevensenders + +paths: + tests: tests + support: . + log: tests/_output + data: tests/_data + envs: tests/_envs + +settings: + bootstrap: _bootstrap.php + suite_class: \PHPUnit_Framework_TestSuite + colors: true + memory_limit: 1024M + log: true + +coverage: + enabled: true + whitelist: + include: + - 'src/*' diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a14ea39 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "spryker-eco/sevensenders", + "description": "Sevensenders Module", + "require": { + "php": ">=7.1", + "spryker/kernel": "^3.0.0", + "spryker/guzzle": "^2.2.0", + "spryker/oms": "^8.0.0", + "spryker/customer": "^7.10.0", + "spryker/mail": "^4.0.0" + }, + "require-dev": { + "spryker/code-sniffer": "*", + "spryker/monolog": "*", + "spryker/queue": "*", + "spryker/testify": "*" + }, + "autoload": { + "psr-4": { + "SprykerEco\\": "src/SprykerEco/" + } + }, + "minimum-stability": "dev", + "autoload-dev": { + "psr-4": { + "SprykerTest\\": "tests/SprykerEcoTest/" + } + } +} diff --git a/phpstan.json b/phpstan.json new file mode 100644 index 0000000..5853d23 --- /dev/null +++ b/phpstan.json @@ -0,0 +1,3 @@ +{ + "defaultLevel": 5 +} diff --git a/src/SprykerEco/Shared/Sevensenders/SevensendersConstants.php b/src/SprykerEco/Shared/Sevensenders/SevensendersConstants.php new file mode 100644 index 0000000..7465e33 --- /dev/null +++ b/src/SprykerEco/Shared/Sevensenders/SevensendersConstants.php @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Api/Adapter/AdapterInterface.php b/src/SprykerEco/Zed/Sevensenders/Business/Api/Adapter/AdapterInterface.php new file mode 100644 index 0000000..0649abd --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Api/Adapter/AdapterInterface.php @@ -0,0 +1,22 @@ + 'application/json', + ]; + + /** + * @var \GuzzleHttp\Client + */ + protected $client; + + /** + * @var \SprykerEco\Zed\Sevensenders\SevensendersConfig + */ + protected $config; + + /** + * @var \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface + */ + protected $utilEncodingService; + + /** + * @param \SprykerEco\Zed\Sevensenders\SevensendersConfig $config + * @param \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface $utilEncodingService + */ + public function __construct(SevensendersConfig $config, SevensendersToUtilEncodingServiceInterface $utilEncodingService) + { + $this->config = $config; + $this->utilEncodingService = $utilEncodingService; + $this->client = new Client([ + RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT, + ]); + } + + /** + * @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer + * @param string $resource + * + * @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiHttpRequestException + * + * @return \Generated\Shared\Transfer\SevensendersResponseTransfer + */ + public function send(SevensendersRequestTransfer $transfer, string $resource): SevensendersResponseTransfer + { + try { + $options = $this->prepareOptions($transfer); + $response = $this->client->post( + $this->getUrl($resource), + $options + ); + } catch (RequestException $requestException) { + throw new SevensendersApiHttpRequestException( + $requestException->getMessage(), + $requestException->getCode(), + $requestException + ); + } + + $responseTransfer = new SevensendersResponseTransfer(); + $responseTransfer->setStatus($response->getStatusCode()); + $responseTransfer->setRequestPayload($options[RequestOptions::BODY]); + $responseTransfer->setResponsePayload($response->getBody()->getContents()); + + return $responseTransfer; + } + + /** + * @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiBadCredentialsException + * + * @return string + */ + protected function auth(): string + { + $response = $this->utilEncodingService->decodeJson($this->client->post($this->getUrl(static::AUTH_RESOURCE), [ + RequestOptions::BODY => $this->utilEncodingService->encodeJson([ + static::REQUEST_KEY_ACCESS_KEY => $this->config->getApiKey(), + ]), + ])->getBody()->getContents()); + + if (!array_key_exists(static::RESPONSE_KEY_TOKEN, $response)) { + throw new SevensendersApiBadCredentialsException('Bad credentials', 401); + } + + return 'Bearer ' . $response[static::RESPONSE_KEY_TOKEN]; + } + + /** + * @param string $resource + * + * @return string + */ + protected function getUrl(string $resource): string + { + return $this->config->getApiUrl() . $resource; + } + + /**i + * + * @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer + * + * @return array + */ + protected function prepareOptions(SevensendersRequestTransfer $transfer): array + { + $options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson($transfer->getPayload()); + $options[RequestOptions::HEADERS] = $this->prepareHeaders(); + + return $options; + } + + /** + * @return array + */ + protected function prepareHeaders(): array + { + $auth = [ + 'Authorization' => $this->auth(), + ]; + + return array_merge(static::DEFAULT_HEADERS, $auth); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Exception/SevensendersApiBadCredentialsException.php b/src/SprykerEco/Zed/Sevensenders/Business/Exception/SevensendersApiBadCredentialsException.php new file mode 100644 index 0000000..49eea40 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Exception/SevensendersApiBadCredentialsException.php @@ -0,0 +1,14 @@ +mapper = $mapper; + $this->adapter = $adapter; + $this->salesFacade = $salesFacade; + $this->entityManager = $entityManager; + $this->utilEncodingService = $utilEncodingService; + } + + /** + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * + * @return \Generated\Shared\Transfer\SevensendersRequestTransfer + */ + protected function map(OrderTransfer $orderTransfer): SevensendersRequestTransfer + { + return $this->mapper->map($orderTransfer); + } + + /** + * @param \Generated\Shared\Transfer\SevensendersRequestTransfer $requestTransfer + * @param string $resource + * + * @return void + */ + protected function sendRequest(SevensendersRequestTransfer $requestTransfer, string $resource): void + { + $response = $this->adapter->send($requestTransfer, $resource); + + $this->saveResult($response, $resource); + } + + /** + * @param \Generated\Shared\Transfer\SevensendersResponseTransfer $responseTransfer + * @param string $resource + * + * @return void + */ + protected function saveResult(SevensendersResponseTransfer $responseTransfer, string $resource): void + { + $requestPayload = $this->utilEncodingService->decodeJson($responseTransfer->getRequestPayload()); + $responsePayload = $this->utilEncodingService->decodeJson($responseTransfer->getResponsePayload()); + + $entityTransfer = new SpySevensendersResponseEntityTransfer(); + + $entityTransfer->setRequestPayload($responseTransfer->getRequestPayload()); + $entityTransfer->setResponseStatus($responseTransfer->getStatus()); + $entityTransfer->setResourceType($resource); + $entityTransfer->setFkSalesOrder($requestPayload['order_id']); + $entityTransfer->setResponsePayload($responseTransfer->getResponsePayload()); + + if ($responseTransfer->getStatus() === static::STATUS_CREATED) { + $entityTransfer->setIri($responsePayload[static::KEY_IRI]); + } + + $this->entityManager->createSevensendersResponse($entityTransfer, $resource); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Handler/HandlerInterface.php b/src/SprykerEco/Zed/Sevensenders/Business/Handler/HandlerInterface.php new file mode 100644 index 0000000..d9edcc0 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Handler/HandlerInterface.php @@ -0,0 +1,18 @@ +salesFacade->getOrderByIdSalesOrder($idSalesOrder); + $requestTransfer = $this->map($orderTransfer); + + $this->sendRequest($requestTransfer, SevensendersApiAdapter::ORDER_RESOURCE); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Handler/ShipmentHandler.php b/src/SprykerEco/Zed/Sevensenders/Business/Handler/ShipmentHandler.php new file mode 100644 index 0000000..f78e5e0 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Handler/ShipmentHandler.php @@ -0,0 +1,26 @@ +salesFacade->getOrderByIdSalesOrder($idSalesOrder); + $requestTransfer = $this->map($orderTransfer); + + $this->sendRequest($requestTransfer, SevensendersApiAdapter::SHIPMENT_RESOURCE); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Helper/OrderResponseHelper.php b/src/SprykerEco/Zed/Sevensenders/Business/Helper/OrderResponseHelper.php new file mode 100644 index 0000000..4e1ee8a --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Helper/OrderResponseHelper.php @@ -0,0 +1,42 @@ +sevensendersRepository = $sevensendersRepository; + } + + /** + * @param int $isSalesOrder + * + * @return bool + */ + public function isLastResponseSuccessful(int $isSalesOrder): bool + { + return in_array($this->sevensendersRepository->getResponseByOrderId($isSalesOrder)->getStatus(), [ + static::STATUS_CREATED, + static::STATUS_UPDATED, + ]); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Helper/ResponseHelperInterface.php b/src/SprykerEco/Zed/Sevensenders/Business/Helper/ResponseHelperInterface.php new file mode 100644 index 0000000..c11606f --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Helper/ResponseHelperInterface.php @@ -0,0 +1,18 @@ +sevensendersRepository = $sevensendersRepository; + } + + /** + * @param int $isSalesOrder + * + * @return bool + */ + public function isLastResponseSuccessful(int $isSalesOrder): bool + { + return in_array($this->sevensendersRepository->getResponseByOrderId($isSalesOrder, SevensendersApiAdapter::SHIPMENT_RESOURCE)->getStatus(), [ + static::STATUS_UPDATED, + static::STATUS_CREATED, + ]); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Mapper/MapperInterface.php b/src/SprykerEco/Zed/Sevensenders/Business/Mapper/MapperInterface.php new file mode 100644 index 0000000..ea770f3 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Mapper/MapperInterface.php @@ -0,0 +1,21 @@ + (string)$orderTransfer->getIdSalesOrder(), + 'order_url' => '', + 'order_date' => $orderTransfer->getCreatedAt(), + 'delivered_with_seven_senders' => true, + 'boarding_complete' => true, + 'language' => $orderTransfer->getLocale() ? $orderTransfer->getLocale()->getLocaleName() : '', + 'promised_delivery_date' => $orderTransfer->getShipmentDeliveryTime(), + ]; + + $transfer = new SevensendersRequestTransfer(); + $transfer->setPayload($payload); + + return $transfer; + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/Mapper/ShipmentMapper.php b/src/SprykerEco/Zed/Sevensenders/Business/Mapper/ShipmentMapper.php new file mode 100644 index 0000000..6937c65 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/Mapper/ShipmentMapper.php @@ -0,0 +1,90 @@ +getShipmentMethods()->offsetGet(0); + + $payload = [ + 'order_id' => (string)$orderTransfer->getIdSalesOrder(), + 'reference_number' => $orderTransfer->getOrderReference(), + 'tracking_code' => '', + 'package_no' => (int)date('U'), + 'delivered_with_seven_senders' => true, + 'carrier' => [ + 'name' => $methodTransfer->getCarrierName(), + 'country' => '', + ], + 'carrier_service' => $methodTransfer->getCarrierName(), + 'recipient_first_name' => $orderTransfer->getShippingAddress()->getFirstName(), + 'recipient_last_name' => $orderTransfer->getShippingAddress()->getLastName(), + 'recipient_email' => $orderTransfer->getEmail(), + 'recipient_address' => $this->getRecipientAddress($orderTransfer), + 'recipient_zip' => $orderTransfer->getShippingAddress()->getZipCode(), + 'recipient_city' => $orderTransfer->getShippingAddress()->getCity(), + 'recipient_country' => $orderTransfer->getShippingAddress()->getCountry()->getIso2Code(), + 'recipient_phone' => $orderTransfer->getShippingAddress()->getPhone(), + 'recipient_company_name' => $orderTransfer->getShippingAddress()->getCompany(), + 'sender_first_name' => '', + 'sender_last_name' => '', + 'sender_company_name' => '', + 'sender_street' => '', + 'sender_house_no' => '', + 'sender_zip' => '', + 'sender_city' => '', + 'sender_country' => '', + 'sender_phone' => '', + 'sender_email' => '', + 'cod' => true, + 'cod_value' => 0, + 'return_parcel' => true, + 'pickup_point_selected' => true, + 'weight' => 0, + 'planned_pickup_datetime' => $methodTransfer->getDeliveryTime(), + 'comment' => '', + 'warehouse_address' => '', + 'warehouse' => '', + ]; + + $transfer = new SevensendersRequestTransfer(); + $transfer->setPayload($payload); + + return $transfer; + } + + /** + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * + * @return string + */ + protected function getRecipientAddress(OrderTransfer $orderTransfer): string + { + $address = $orderTransfer->getShippingAddress()->getAddress1(); + + if ($orderTransfer->getShippingAddress()->getAddress2()) { + $address .= ', ' . $orderTransfer->getShippingAddress()->getAddress2(); + } + + if ($orderTransfer->getShippingAddress()->getAddress3()) { + $address .= ', ' . $orderTransfer->getShippingAddress()->getAddress3(); + } + + return $address; + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/SevensendersBusinessFactory.php b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersBusinessFactory.php new file mode 100644 index 0000000..4738a2a --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersBusinessFactory.php @@ -0,0 +1,116 @@ +createOrderMapper(), + $this->createSevensendersApiAdapter(), + $this->getFacadeSales(), + $this->getEntityManager(), + $this->getUtilEncodingService() + ); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Handler\HandlerInterface + */ + public function createShipmentHandler(): HandlerInterface + { + return new ShipmentHandler( + $this->createShipmentMapper(), + $this->createSevensendersApiAdapter(), + $this->getFacadeSales(), + $this->getEntityManager(), + $this->getUtilEncodingService() + ); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Helper\ResponseHelperInterface + */ + public function createOrderResponseHelper(): ResponseHelperInterface + { + return new OrderResponseHelper($this->getRepository()); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Helper\ResponseHelperInterface + */ + public function createShipmentResponseHelper(): ResponseHelperInterface + { + return new ShipmentResponseHelper($this->getRepository()); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Mapper\MapperInterface + */ + protected function createOrderMapper(): MapperInterface + { + return new OrderMapper(); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Mapper\MapperInterface + */ + protected function createShipmentMapper(): MapperInterface + { + return new ShipmentMapper(); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Dependency\Facade\SevensendersToSalesFacadeInterface + */ + protected function getFacadeSales(): SevensendersToSalesFacadeInterface + { + return $this->getProvidedDependency(SevensendersDependencyProvider::FACADE_SALES); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface + */ + protected function getUtilEncodingService(): SevensendersToUtilEncodingServiceInterface + { + return $this->getProvidedDependency(SevensendersDependencyProvider::UTIL_ENCODING_SERVICE); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Api\Adapter\AdapterInterface + */ + protected function createSevensendersApiAdapter(): AdapterInterface + { + return new SevensendersApiAdapter($this->getConfig(), $this->getUtilEncodingService()); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacade.php b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacade.php new file mode 100644 index 0000000..5aa577c --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacade.php @@ -0,0 +1,65 @@ +getFactory() + ->createOrderHandler() + ->handle($idSalesOrder); + } + + /** + * @param int $idSalesOrder + * + * @return void + */ + public function handleShipmentEvent(int $idSalesOrder): void + { + $this->getFactory() + ->createShipmentHandler() + ->handle($idSalesOrder); + } + + /** + * @param int $idSalesOrder + * + * @return bool + */ + public function isLastOrderResponseSuccessful(int $idSalesOrder): bool + { + return $this->getFactory() + ->createOrderResponseHelper() + ->isLastResponseSuccessful($idSalesOrder); + } + + /** + * @param int $idSalesOrder + * + * @return bool + */ + public function isLastShipmentResponseSuccessful(int $idSalesOrder): bool + { + return $this->getFactory() + ->createShipmentResponseHelper() + ->isLastResponseSuccessful($idSalesOrder); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacadeInterface.php b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacadeInterface.php new file mode 100644 index 0000000..1e77cc0 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Business/SevensendersFacadeInterface.php @@ -0,0 +1,39 @@ +getFacade()->handleOrderEvent($orderEntity->getIdSalesOrder()); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Command/SevenordersShipmentPlugin.php b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Command/SevenordersShipmentPlugin.php new file mode 100644 index 0000000..1627ce9 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Command/SevenordersShipmentPlugin.php @@ -0,0 +1,36 @@ +getFacade()->handleShipmentEvent($orderEntity->getIdSalesOrder()); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousOrderResponseConditionPlugin.php b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousOrderResponseConditionPlugin.php new file mode 100644 index 0000000..6f0c914 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousOrderResponseConditionPlugin.php @@ -0,0 +1,31 @@ +getFacade()->isLastOrderResponseSuccessful($orderItem->getOrder()->getIdSalesOrder()); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousShipmentResponseConditionPlugin.php b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousShipmentResponseConditionPlugin.php new file mode 100644 index 0000000..190e532 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Communication/Plugin/Oms/Condition/IsSuccessfulPreviousShipmentResponseConditionPlugin.php @@ -0,0 +1,31 @@ +getFacade()->isLastOrderResponseSuccessful($orderItem->getOrder()->getIdSalesOrder()); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Communication/SevensendersCommunicationFactory.php b/src/SprykerEco/Zed/Sevensenders/Communication/SevensendersCommunicationFactory.php new file mode 100644 index 0000000..55ca087 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Communication/SevensendersCommunicationFactory.php @@ -0,0 +1,17 @@ +salesFacade = $salesFacade; + } + + /** + * @param int $idSalesOrder + * + * @return \Generated\Shared\Transfer\OrderTransfer + */ + public function getOrderByIdSalesOrder($idSalesOrder): OrderTransfer + { + return $this->salesFacade->getOrderByIdSalesOrder($idSalesOrder); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Dependency/Facade/SevensendersToSalesFacadeInterface.php b/src/SprykerEco/Zed/Sevensenders/Dependency/Facade/SevensendersToSalesFacadeInterface.php new file mode 100644 index 0000000..a34448c --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Dependency/Facade/SevensendersToSalesFacadeInterface.php @@ -0,0 +1,20 @@ +utilEncodingService = $utilEncodingService; + } + + /** + * @param mixed $value + * @param int|null $options + * @param int|null $depth + * + * @return string|null + */ + public function encodeJson($value, $options = null, $depth = null): ?string + { + return $this->utilEncodingService->encodeJson($value, $options, $depth); + } + + /** + * @param string $jsonValue + * @param bool $assoc + * @param int|null $depth + * @param int|null $options + * + * @return array|null + */ + public function decodeJson($jsonValue, $assoc = true, $depth = null, $options = null): ?array + { + return $this->utilEncodingService->decodeJson($jsonValue, $assoc, $depth, $options); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Dependency/Service/SevensendersToUtilEncodingServiceInterface.php b/src/SprykerEco/Zed/Sevensenders/Dependency/Service/SevensendersToUtilEncodingServiceInterface.php new file mode 100644 index 0000000..c0077c4 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Dependency/Service/SevensendersToUtilEncodingServiceInterface.php @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + +
+
\ No newline at end of file diff --git a/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManager.php b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManager.php new file mode 100644 index 0000000..ed80c49 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManager.php @@ -0,0 +1,50 @@ +getFactory() + ->createSpySevensendersResponseQuery() + ->filterByResourceType($transfer->getResourceType()) + ->filterByFkSalesOrder($transfer->getFkSalesOrder()) + ->findOneOrCreate(); + + $entity->setRequestPayload($transfer->getRequestPayload()); + $entity->setResponseStatus($transfer->getResponseStatus()); + $entity->setResourceType($transfer->getResourceType()); + $entity->setFkSalesOrder($transfer->getFkSalesOrder()); + $entity->setResponsePayload($transfer->getResponsePayload()); + $entity->setIri($transfer->getIri()); + + $entity->save(); + + return $entity; + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManagerInterface.php b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManagerInterface.php new file mode 100644 index 0000000..c5257ba --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersEntityManagerInterface.php @@ -0,0 +1,22 @@ +getFactory() + ->createSpySevensendersResponseQuery() + ->filterByFkSalesOrder($idSalesOrder) + ->filterByResourceType($resource) + ->findOne(); + + $transfer = new SevensendersResponseTransfer(); + + $transfer->setRequestPayload($spyResponse ? $spyResponse->getRequestPayload() : ''); + $transfer->setResponsePayload($spyResponse ? $spyResponse->getResponsePayload() : ''); + $transfer->setStatus($spyResponse ? $spyResponse->getResponseStatus() : null); + + return $transfer; + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersRepositoryInterface.php b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersRepositoryInterface.php new file mode 100644 index 0000000..dc32e08 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/Persistence/SevensendersRepositoryInterface.php @@ -0,0 +1,22 @@ +get(SevensendersConstants::API_URL); + } + + /** + * @return string + */ + public function getApiKey(): string + { + return $this->get(SevensendersConstants::API_KEY); + } +} diff --git a/src/SprykerEco/Zed/Sevensenders/SevensendersDependencyProvider.php b/src/SprykerEco/Zed/Sevensenders/SevensendersDependencyProvider.php new file mode 100644 index 0000000..4d73fd7 --- /dev/null +++ b/src/SprykerEco/Zed/Sevensenders/SevensendersDependencyProvider.php @@ -0,0 +1,62 @@ +addSalesFacade($container); + $container = $this->addUtilEncodingService($container); + + return $container; + } + + /** + * @param \Spryker\Zed\Kernel\Container $container + * + * @return \Spryker\Zed\Kernel\Container + */ + protected function addSalesFacade(Container $container): Container + { + $container[static::FACADE_SALES] = function (Container $container) { + return new SevensendersToSalesFacadeBridge($container->getLocator()->sales()->facade()); + }; + + return $container; + } + + /** + * @param \Spryker\Zed\Kernel\Container $container + * + * @return \Spryker\Zed\Kernel\Container + */ + protected function addUtilEncodingService(Container $container): Container + { + $container[static::UTIL_ENCODING_SERVICE] = function (Container $container) { + return new SevensendersToUtilEncodingServiceBridge($container->getLocator()->utilEncoding()->service()); + }; + + return $container; + } +} diff --git a/tests/SprykerEcoTest/Zed/Sevensenders/SevensendersTest.php b/tests/SprykerEcoTest/Zed/Sevensenders/SevensendersTest.php new file mode 100644 index 0000000..b9904e3 --- /dev/null +++ b/tests/SprykerEcoTest/Zed/Sevensenders/SevensendersTest.php @@ -0,0 +1,249 @@ +prepareFacade(); + $this->assertEmpty($facade->handleOrderEvent(1)); + } + + /** + * @return void + */ + public function testShipmentEvent(): void + { + $facade = $this->prepareFacade(); + $this->assertEmpty($facade->handleShipmentEvent(1)); + } + + /** + * @return void + */ + public function testIsLastOrderResponseSuccessful(): void + { + $facade = $this->prepareFacade(); + + $this->assertTrue($facade->isLastOrderResponseSuccessful(1)); + $this->assertFalse($facade->isLastOrderResponseSuccessful(2)); + } + + /** + * @return void + */ + public function testLastShipmentResponseSuccessful(): void + { + $facade = $this->prepareFacade(); + + $this->assertTrue($facade->isLastShipmentResponseSuccessful(1)); + $this->assertFalse($facade->isLastShipmentResponseSuccessful(2)); + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\SevensendersFacadeInterface + */ + protected function prepareFacade(): SevensendersFacadeInterface + { + $facade = new SevensendersFacade(); + $facade->setFactory($this->prepareFactory()); + + return $facade; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\SevensendersBusinessFactory + */ + protected function prepareFactory(): SevensendersBusinessFactory + { + $factory = $this->getMockBuilder(SevensendersBusinessFactory::class) + ->setMethods([ + 'createOrderResponseHelper', + 'createShipmentResponseHelper', + 'createOrderHandler', + 'createShipmentHandler', + ]) + ->getMock(); + + $factory->method('createOrderResponseHelper')->willReturn($this->createOrderResponseHelperMock()); + $factory->method('createShipmentResponseHelper')->willReturn($this->createShipmentResponseHelperMock()); + $factory->method('createOrderHandler')->willReturn($this->createOrderHandlerMock()); + $factory->method('createShipmentHandler')->willReturn($this->createShipmentHandlerMock()); + + return $factory; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Helper\ResponseHelperInterface + */ + protected function createOrderResponseHelperMock(): ResponseHelperInterface + { + $helper = $this->getMockBuilder(OrderResponseHelper::class) + ->disableOriginalConstructor() + ->setMethods(['isLastResponseSuccessful']) + ->getMock(); + + $helper->method('isLastResponseSuccessful')->willReturnOnConsecutiveCalls(true, false); + + return $helper; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Helper\ResponseHelperInterface + */ + protected function createShipmentResponseHelperMock(): ResponseHelperInterface + { + $helper = $this->getMockBuilder(ShipmentResponseHelper::class) + ->disableOriginalConstructor() + ->setMethods(['isLastResponseSuccessful']) + ->getMock(); + + $helper->method('isLastResponseSuccessful')->willReturnOnConsecutiveCalls(true, false); + + return $helper; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Handler\HandlerInterface + */ + protected function createOrderHandlerMock(): HandlerInterface + { + $handler = $this->getMockBuilder(OrderHandler::class) + ->disableOriginalConstructor() + ->setConstructorArgs([ + $this->createMapperInterface(), + $this->createAdapterInterface(), + $this->createSalesFacadeMock(), + $this->createEntityManagerInterface(), + $this->createUtilEncodingServiceMock(), + ]) + ->enableOriginalConstructor() + ->setMethods([ + 'map', + 'sendRequest', + ]) + ->getMock(); + + $handler->method('map')->willReturn(new SevensendersRequestTransfer()); + $handler->method('sendRequest')->willReturnCallback(function () { + }); + + return $handler; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Handler\HandlerInterface + */ + protected function createShipmentHandlerMock(): HandlerInterface + { + $handler = $this->getMockBuilder(ShipmentHandler::class) + ->disableOriginalConstructor() + ->setConstructorArgs([ + $this->createMapperInterface(), + $this->createAdapterInterface(), + $this->createSalesFacadeMock(), + $this->createEntityManagerInterface(), + $this->createUtilEncodingServiceMock(), + ]) + ->enableOriginalConstructor() + ->setMethods([ + 'map', + 'sendRequest', + ]) + ->getMock(); + + $handler->method('map')->willReturn(new SevensendersRequestTransfer()); + $handler->method('sendRequest')->willReturnCallback(function () { + }); + + return $handler; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Dependency\Facade\SevensendersToSalesFacadeInterface + */ + protected function createSalesFacadeMock(): SevensendersToSalesFacadeInterface + { + $bridge = $this->getMockBuilder(SevensendersToSalesFacadeInterface::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getOrderByIdSalesOrder', + ]) + ->getMock(); + + $bridge->method('getOrderByIdSalesOrder')->willReturn(new OrderTransfer()); + + return $bridge; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Mapper\MapperInterface + */ + protected function createMapperInterface(): MapperInterface + { + $mapper = $this->getMockBuilder(MapperInterface::class) + ->getMock(); + + return $mapper; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Business\Api\Adapter\AdapterInterface + */ + protected function createAdapterInterface(): AdapterInterface + { + $adapter = $this->getMockBuilder(AdapterInterface::class) + ->getMock(); + + return $adapter; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Persistence\SevensendersEntityManagerInterface + */ + protected function createEntityManagerInterface(): SevensendersEntityManagerInterface + { + $em = $this->getMockBuilder(SevensendersEntityManagerInterface::class) + ->getMock(); + + return $em; + } + + /** + * @return \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface + */ + protected function createUtilEncodingServiceMock(): SevensendersToUtilEncodingServiceInterface + { + $service = $this->getMockBuilder(SevensendersToUtilEncodingServiceInterface::class) + ->getMock(); + + return $service; + } +} diff --git a/tests/SprykerEcoTest/Zed/Sevensenders/codeception.yml b/tests/SprykerEcoTest/Zed/Sevensenders/codeception.yml new file mode 100644 index 0000000..e622559 --- /dev/null +++ b/tests/SprykerEcoTest/Zed/Sevensenders/codeception.yml @@ -0,0 +1,22 @@ +namespace: SprykerEcoTest\Zed\Sevensenders + +paths: + tests: . + data: ../../../_data + support: _support + log: ../../../_output + output: ../../../_output + +coverage: + enabled: true + remote: false + whitelist: { include: ['../../../../src/*'] } + +suites: + Service: + path: . + class_name: Sevensenders + modules: + enabled: + - Asserts + - \PyzTest\Shared\Testify\Helper\Environment