| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Order; |
| 6 |
|
| 7 |
use Packetery\Core\Api\Rest\Exception\InvalidApiKeyException; |
| 8 |
use Packetery\Core\Api\Rest\PickupPointValidate; |
| 9 |
use Packetery\Core\Api\Rest\PickupPointValidateRequest; |
| 10 |
use Packetery\Core\Api\Rest\PickupPointValidateResponse; |
| 11 |
use Packetery\Core\Api\Rest\RestException; |
| 12 |
use Packetery\Core\Log\ILogger; |
| 13 |
use Packetery\Core\Log\Record; |
| 14 |
use Packetery\Module\Framework\WcAdapter; |
| 15 |
use Packetery\Module\Framework\WpAdapter; |
| 16 |
use Packetery\Module\Options\OptionsProvider; |
| 17 |
use Packetery\Module\WebRequestClient; |
| 18 |
|
| 19 |
class PickupPointValidator { |
| 20 |
|
| 21 |
public const VALIDATION_HTTP_ERROR_SESSION_KEY = 'packetery_validation_http_error'; |
| 22 |
|
| 23 |
/** @var OptionsProvider */ |
| 24 |
private $optionsProvider; |
| 25 |
|
| 26 |
/** @var ILogger */ |
| 27 |
private $logger; |
| 28 |
|
| 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; |
| 50 |
} |
| 51 |
|
| 52 |
public function validate( PickupPointValidateRequest $request ): PickupPointValidateResponse { |
| 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 |
]; |
| 61 |
|
| 62 |
$this->logger->add( $record ); |
| 63 |
|
| 64 |
return new PickupPointValidateResponse( true, [] ); |
| 65 |
} |
| 66 |
|
| 67 |
try { |
| 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; |
| 86 |
} catch ( RestException $exception ) { |
| 87 |
$record = $this->createPickUpPointValidateErrorRecord(); |
| 88 |
$record->params = [ |
| 89 |
'errorMessage' => $exception->getMessage(), |
| 90 |
'request' => $request->getSubmittableData(), |
| 91 |
]; |
| 92 |
|
| 93 |
$this->logger->add( $record ); |
| 94 |
$this->wcAdapter->sessionSet( self::VALIDATION_HTTP_ERROR_SESSION_KEY, $exception->getMessage() ); |
| 95 |
|
| 96 |
return new PickupPointValidateResponse( true, [] ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 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' ); |
| 105 |
|
| 106 |
return $record; |
| 107 |
} |
| 108 |
} |
| 109 |
|