PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / PickupPointValidator.php

PickupPointValidator.php in Packeta 2.0.9, at src/Packetery/Module/Order/PickupPointValidator.php

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