← All changes
|
src/Packetery/Module/Order/PickupPointValidator.php
+69
-103
1.5.2
→
2.3.2
View file →
| @@ -1,16 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class PickupPointValidator | |
| 4 | - * | |
| 5 | - * @package Packetery\Module | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| 11 | 6 | |
| 12 | -use Packetery\Core\Api\Rest\IDownloader; | |
| 7 | +use Packetery\Core\Api\Rest\Exception\InvalidApiKeyException; | |
| 13 | 8 | use Packetery\Core\Api\Rest\PickupPointValidate; |
| 14 | 9 | use Packetery\Core\Api\Rest\PickupPointValidateRequest; |
| 15 | 10 | use Packetery\Core\Api\Rest\PickupPointValidateResponse; |
| 16 | 11 | use Packetery\Core\Api\Rest\RestException; |
| @@ -15,128 +10,99 @@ | ||
| 15 | 10 | use Packetery\Core\Api\Rest\PickupPointValidateResponse; |
| 16 | 11 | use Packetery\Core\Api\Rest\RestException; |
| 17 | 12 | use Packetery\Core\Log\ILogger; |
| 18 | 13 | use Packetery\Core\Log\Record; |
| 19 | -use Packetery\Module\Options\Provider; | |
| 20 | -use PacketeryGuzzleHttp\Client; | |
| 21 | -use PacketeryGuzzleHttp\Exception\GuzzleException; | |
| 22 | -use PacketeryGuzzleHttp\PacketeryPsr7\Response; | |
| 14 | +use Packetery\Module\Framework\WcAdapter; | |
| 15 | +use Packetery\Module\Framework\WpAdapter; | |
| 16 | +use Packetery\Module\Options\OptionsProvider; | |
| 17 | +use Packetery\Module\WebRequestClient; | |
| 23 | 18 | |
| 24 | -/** | |
| 25 | - * Class PickupPointValidator | |
| 26 | - * | |
| 27 | - * @package Packetery\Module | |
| 28 | - */ | |
| 29 | -class PickupPointValidator implements IDownloader { | |
| 19 | +class PickupPointValidator { | |
| 30 | 20 | |
| 31 | - // TODO: It needs to be thoroughly tested. | |
| 32 | - public const IS_ACTIVE = false; | |
| 33 | - | |
| 34 | 21 | public const VALIDATION_HTTP_ERROR_SESSION_KEY = 'packetery_validation_http_error'; |
| 35 | 22 | |
| 36 | - /** | |
| 37 | - * Guzzle client. | |
| 38 | - * | |
| 39 | - * @var Client | |
| 40 | - */ | |
| 41 | - private $guzzleClient; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Options provider. | |
| 45 | - * | |
| 46 | - * @var Provider | |
| 47 | - */ | |
| 23 | + /** @var OptionsProvider */ | |
| 48 | 24 | private $optionsProvider; |
| 49 | 25 | |
| 50 | - /** | |
| 51 | - * Logger. | |
| 52 | - * | |
| 53 | - * @var ILogger | |
| 54 | - */ | |
| 26 | + /** @var ILogger */ | |
| 55 | 27 | private $logger; |
| 56 | 28 | |
| 57 | - /** | |
| 58 | - * PickupPointValidator constructor. | |
| 59 | - * | |
| 60 | - * @param Client $guzzleClient Guzzle client. | |
| 61 | - * @param Provider $optionsProvider Options provider. | |
| 62 | - * @param ILogger $logger Logger. | |
| 63 | - */ | |
| 64 | - public function __construct( Client $guzzleClient, Provider $optionsProvider, ILogger $logger ) { | |
| 65 | - $this->guzzleClient = $guzzleClient; | |
| 66 | - $this->optionsProvider = $optionsProvider; | |
| 67 | - $this->logger = $logger; | |
| 29 | + /** @var WebRequestClient */ | |
| 30 | + private $webRequestClient; | |
| 31 | + | |
| 32 | + /** @var WpAdapter */ | |
| 33 | + private $wpAdapter; | |
| 34 | + | |
| 35 | + /** @var WcAdapter */ | |
| 36 | + private $wcAdapter; | |
| 37 | + | |
| 38 | + public function __construct( | |
| 39 | + OptionsProvider $optionsProvider, | |
| 40 | + ILogger $logger, | |
| 41 | + WebRequestClient $webRequestClient, | |
| 42 | + WpAdapter $wpAdapter, | |
| 43 | + WcAdapter $wcAdapter | |
| 44 | + ) { | |
| 45 | + $this->optionsProvider = $optionsProvider; | |
| 46 | + $this->logger = $logger; | |
| 47 | + $this->webRequestClient = $webRequestClient; | |
| 48 | + $this->wpAdapter = $wpAdapter; | |
| 49 | + $this->wcAdapter = $wcAdapter; | |
| 68 | 50 | } |
| 69 | 51 | |
| 70 | - /** | |
| 71 | - * Validates pickup point. | |
| 72 | - * | |
| 73 | - * @param PickupPointValidateRequest $request Pickup point validate request. | |
| 74 | - * | |
| 75 | - * @return PickupPointValidateResponse | |
| 76 | - */ | |
| 77 | 52 | public function validate( PickupPointValidateRequest $request ): PickupPointValidateResponse { |
| 78 | - $pickupPointValidate = new PickupPointValidate( $this, $this->optionsProvider->get_api_key() ); | |
| 53 | + $apiKey = $this->optionsProvider->get_api_key(); | |
| 54 | + try { | |
| 55 | + $pickupPointValidate = PickupPointValidate::createWithValidApiKey( $this->webRequestClient, $apiKey ); | |
| 56 | + } catch ( InvalidApiKeyException $exception ) { | |
| 57 | + $record = $this->createPickUpPointValidateErrorRecord(); | |
| 58 | + $record->params = [ | |
| 59 | + 'errorMessage' => $this->wpAdapter->__( 'API credentials are not set correctly.', 'packeta' ), | |
| 60 | + ]; | |
| 79 | 61 | |
| 62 | + $this->logger->add( $record ); | |
| 63 | + | |
| 64 | + return new PickupPointValidateResponse( true, [] ); | |
| 65 | + } | |
| 66 | + | |
| 80 | 67 | try { |
| 81 | - // We do not log successful requests. | |
| 82 | - return $pickupPointValidate->validate( $request ); | |
| 68 | + $validationResponse = $pickupPointValidate->validate( $request ); | |
| 69 | + | |
| 70 | + if ( $validationResponse->isValid() === false ) { | |
| 71 | + $record = $this->createPickUpPointValidateErrorRecord(); | |
| 72 | + } else { | |
| 73 | + $record = new Record(); | |
| 74 | + $record->action = Record::ACTION_PICKUP_POINT_VALIDATE; | |
| 75 | + $record->status = Record::STATUS_SUCCESS; | |
| 76 | + $record->title = $this->wpAdapter->__( 'Pickup point validated.', 'packeta' ); | |
| 77 | + } | |
| 78 | + $record->params = [ | |
| 79 | + 'request' => $request->getSubmittableData(), | |
| 80 | + 'isValid' => $validationResponse->isValid(), | |
| 81 | + 'errors' => $validationResponse->getErrors(), | |
| 82 | + ]; | |
| 83 | + $this->logger->add( $record ); | |
| 84 | + | |
| 85 | + return $validationResponse; | |
| 83 | 86 | } catch ( RestException $exception ) { |
| 84 | - $record = new Record(); | |
| 85 | - $record->action = Record::ACTION_PICKUP_POINT_VALIDATE; | |
| 86 | - $record->status = Record::STATUS_ERROR; | |
| 87 | - $record->title = __( 'Pickup point could not be validated.', 'packeta' ); | |
| 87 | + $record = $this->createPickUpPointValidateErrorRecord(); | |
| 88 | 88 | $record->params = [ |
| 89 | 89 | 'errorMessage' => $exception->getMessage(), |
| 90 | 90 | 'request' => $request->getSubmittableData(), |
| 91 | 91 | ]; |
| 92 | + | |
| 92 | 93 | $this->logger->add( $record ); |
| 93 | - WC()->session->set( self::VALIDATION_HTTP_ERROR_SESSION_KEY, $exception->getMessage() ); | |
| 94 | + $this->wcAdapter->sessionSet( self::VALIDATION_HTTP_ERROR_SESSION_KEY, $exception->getMessage() ); | |
| 94 | 95 | |
| 95 | 96 | return new PickupPointValidateResponse( true, [] ); |
| 96 | 97 | } |
| 97 | 98 | } |
| 98 | 99 | |
| 99 | - /** | |
| 100 | - * Accepts parameters in Guzzle format. | |
| 101 | - * | |
| 102 | - * @param string $uri Target URI. | |
| 103 | - * @param array $options Options. | |
| 104 | - * | |
| 105 | - * @return string | |
| 106 | - * @throws GuzzleException Thrown on failure. | |
| 107 | - */ | |
| 108 | - public function post( string $uri, array $options ): string { | |
| 109 | - /** | |
| 110 | - * Guzzle response. | |
| 111 | - * | |
| 112 | - * @var Response $result Guzzle response. | |
| 113 | - */ | |
| 114 | - $resultResponse = $this->guzzleClient->post( $uri, $options ); | |
| 100 | + private function createPickUpPointValidateErrorRecord(): Record { | |
| 101 | + $record = new Record(); | |
| 102 | + $record->action = Record::ACTION_PICKUP_POINT_VALIDATE; | |
| 103 | + $record->status = Record::STATUS_ERROR; | |
| 104 | + $record->title = $this->wpAdapter->__( 'Pickup point could not be validated.', 'packeta' ); | |
| 115 | 105 | |
| 116 | - return $resultResponse->getBody()->getContents(); | |
| 106 | + return $record; | |
| 117 | 107 | } |
| 118 | - | |
| 119 | - /** | |
| 120 | - * Returns translated validation errors. | |
| 121 | - * | |
| 122 | - * @return array | |
| 123 | - */ | |
| 124 | - public function getTranslatedError(): array { | |
| 125 | - return [ | |
| 126 | - 'NotFound' => __( 'The pick-up point was not found.', 'packeta' ), | |
| 127 | - 'InvalidCarrier' => __( 'The pick-up point has not allowed carrier.', 'packeta' ), | |
| 128 | - 'InvalidCountry' => __( 'The pick-up point is not in allowed country.', 'packeta' ), | |
| 129 | - 'EmptyListOfAllowedCountries' => __( 'Cannot perform country validation because the list of allowed countries is empty.', 'packeta' ), | |
| 130 | - 'NoClaimAssistant' => __( 'The pick-up point does not offer Complaints Assistant Service.', 'packeta' ), | |
| 131 | - 'NoPacketConsignment' => __( 'The pick-up point is not submission point.', 'packeta' ), | |
| 132 | - 'InvalidWeight' => __( 'The pick-up point does not accept packets with given weight.', 'packeta' ), | |
| 133 | - 'NoAgeVerification' => __( 'The pick-up point does not offer Age Verification Service.', 'packeta' ), | |
| 134 | - 'PickupPointVacation' => __( 'The pick-up point currently does not accept any packets due to reported holiday.', 'packeta' ), | |
| 135 | - 'PickupPointClosing' => __( 'The pick-up point does not accept new shipments because it will be closed soon.', 'packeta' ), | |
| 136 | - 'PickupPointIsFull' => __( 'The pick-up point does not accept any packets at the moment due to its full capacity.', 'packeta' ), | |
| 137 | - 'PickupPointForbidden' => __( 'The pick-up point cannot be selected.', 'packeta' ), | |
| 138 | - 'PickupPointTechnicalReason' => __( 'The pick-up point cannot be chosen as a final destination of your packet due to technical reasons.', 'packeta' ), | |
| 139 | - ]; | |
| 140 | - } | |
| 141 | - | |
| 142 | 108 | } |