PluginProbe
Packeta / 1.5.0
Packeta v1.5.0
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 1.5.0, at src/Packetery/Module/Order/PickupPointValidator.php

143 lines 4.6 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\IDownloader;
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\Options\Provider;
20 use PacketeryGuzzleHttp\Client;
21 use PacketeryGuzzleHttp\Exception\GuzzleException;
22 use PacketeryGuzzleHttp\PacketeryPsr7\Response;
23
24 /**
25 * Class PickupPointValidator
26 *
27 * @package Packetery\Module
28 */
29 class PickupPointValidator implements IDownloader {
30
31 // TODO: It needs to be thoroughly tested.
32 public const IS_ACTIVE = false;
33
34 public const VALIDATION_HTTP_ERROR_SESSION_KEY = 'packetery_validation_http_error';
35
36 /**
37 * Guzzle client.
38 *
39 * @var Client
40 */
41 private $guzzleClient;
42
43 /**
44 * Options provider.
45 *
46 * @var Provider
47 */
48 private $optionsProvider;
49
50 /**
51 * Logger.
52 *
53 * @var ILogger
54 */
55 private $logger;
56
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;
68 }
69
70 /**
71 * Validates pickup point.
72 *
73 * @param PickupPointValidateRequest $request Pickup point validate request.
74 *
75 * @return PickupPointValidateResponse
76 */
77 public function validate( PickupPointValidateRequest $request ): PickupPointValidateResponse {
78 $pickupPointValidate = new PickupPointValidate( $this, $this->optionsProvider->get_api_key() );
79
80 try {
81 // We do not log successful requests.
82 return $pickupPointValidate->validate( $request );
83 } 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' );
88 $record->params = [
89 'errorMessage' => $exception->getMessage(),
90 'request' => $request->getSubmittableData(),
91 ];
92 $this->logger->add( $record );
93 WC()->session->set( self::VALIDATION_HTTP_ERROR_SESSION_KEY, $exception->getMessage() );
94
95 return new PickupPointValidateResponse( true, [] );
96 }
97 }
98
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 );
115
116 return $resultResponse->getBody()->getContents();
117 }
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 }
143