| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PacketSubmitter |
| 4 |
* |
| 5 |
* @package Packetery\Api |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Api\InvalidRequestException; |
| 13 |
use Packetery\Core\Api\Soap\Client; |
| 14 |
use Packetery\Core\Api\Soap\Request\CreatePacket; |
| 15 |
use Packetery\Core\Entity; |
| 16 |
use Packetery\Core\Log; |
| 17 |
use Packetery\Core\Validator; |
| 18 |
use Packetery\Module\ShippingMethod; |
| 19 |
use WC_Order; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class PacketSubmitter |
| 23 |
* |
| 24 |
* @package Packetery\Api |
| 25 |
*/ |
| 26 |
class PacketSubmitter { |
| 27 |
|
| 28 |
/** |
| 29 |
* SOAP API Client. |
| 30 |
* |
| 31 |
* @var Client SOAP API Client. |
| 32 |
*/ |
| 33 |
private $soapApiClient; |
| 34 |
|
| 35 |
/** |
| 36 |
* Order validator. |
| 37 |
* |
| 38 |
* @var Validator\Order |
| 39 |
*/ |
| 40 |
private $orderValidator; |
| 41 |
|
| 42 |
/** |
| 43 |
* ILogger. |
| 44 |
* |
| 45 |
* @var Log\ILogger |
| 46 |
*/ |
| 47 |
private $logger; |
| 48 |
|
| 49 |
/** |
| 50 |
* Order repository. |
| 51 |
* |
| 52 |
* @var Repository |
| 53 |
*/ |
| 54 |
private $orderRepository; |
| 55 |
|
| 56 |
/** |
| 57 |
* OrderApi constructor. |
| 58 |
* |
| 59 |
* @param Client $soapApiClient SOAP API Client. |
| 60 |
* @param Validator\Order $orderValidator Order validator. |
| 61 |
* @param Log\ILogger $logger Logger. |
| 62 |
* @param Repository $orderRepository Order repository. |
| 63 |
*/ |
| 64 |
public function __construct( |
| 65 |
Client $soapApiClient, |
| 66 |
Validator\Order $orderValidator, |
| 67 |
Log\ILogger $logger, |
| 68 |
Repository $orderRepository |
| 69 |
) { |
| 70 |
$this->soapApiClient = $soapApiClient; |
| 71 |
$this->orderValidator = $orderValidator; |
| 72 |
$this->logger = $logger; |
| 73 |
$this->orderRepository = $orderRepository; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Submits packet data to Packeta API. |
| 78 |
* |
| 79 |
* @param WC_Order $order WC order. |
| 80 |
* @param array $resultsCounter Array with results. |
| 81 |
*/ |
| 82 |
public function submitPacket( WC_Order $order, array &$resultsCounter ): void { |
| 83 |
$commonEntity = $this->orderRepository->getByWcOrder( $order ); |
| 84 |
if ( null === $commonEntity ) { |
| 85 |
$resultsCounter['ignored'] ++; |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$orderData = $order->get_data(); |
| 91 |
$shippingMethods = $order->get_shipping_methods(); |
| 92 |
$shippingMethod = reset( $shippingMethods ); |
| 93 |
|
| 94 |
$shippingMethodData = $shippingMethod->get_data(); |
| 95 |
$shippingMethodId = $shippingMethodData['method_id']; |
| 96 |
if ( ShippingMethod::PACKETERY_METHOD_ID === $shippingMethodId && ! $commonEntity->isExported() ) { |
| 97 |
try { |
| 98 |
$createPacketRequest = $this->preparePacketRequest( $commonEntity ); |
| 99 |
} catch ( InvalidRequestException $e ) { |
| 100 |
$record = new Log\Record(); |
| 101 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 102 |
$record->status = Log\Record::STATUS_ERROR; |
| 103 |
$record->title = __( 'packetSendingErrorLogTitle', 'packetery' ); |
| 104 |
$record->params = [ |
| 105 |
'orderId' => $orderData['id'], |
| 106 |
'errorMessage' => $e->getMessage(), |
| 107 |
]; |
| 108 |
$this->logger->add( $record ); |
| 109 |
|
| 110 |
$resultsCounter['errors'] ++; |
| 111 |
|
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$response = $this->soapApiClient->createPacket( $createPacketRequest ); |
| 116 |
if ( $response->hasFault() ) { |
| 117 |
$record = new Log\Record(); |
| 118 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 119 |
$record->status = Log\Record::STATUS_ERROR; |
| 120 |
$record->title = __( 'packetSendingErrorLogTitle', 'packetery' ); |
| 121 |
$record->params = [ |
| 122 |
'request' => $createPacketRequest->getSubmittableData(), |
| 123 |
'errorMessage' => $response->getErrorsAsString(), |
| 124 |
]; |
| 125 |
$this->logger->add( $record ); |
| 126 |
|
| 127 |
$resultsCounter['errors'] ++; |
| 128 |
} else { |
| 129 |
$commonEntity->setIsExported( true ); |
| 130 |
$commonEntity->setPacketId( (string) $response->getId() ); |
| 131 |
$this->orderRepository->save( $commonEntity ); |
| 132 |
|
| 133 |
$resultsCounter['success'] ++; |
| 134 |
|
| 135 |
$record = new Log\Record(); |
| 136 |
$record->action = Log\Record::ACTION_PACKET_SENDING; |
| 137 |
$record->status = Log\Record::STATUS_SUCCESS; |
| 138 |
$record->title = __( 'packetSendingSuccessLogTitle', 'packetery' ); |
| 139 |
$record->params = [ |
| 140 |
'packetId' => $response->getId(), |
| 141 |
]; |
| 142 |
$this->logger->add( $record ); |
| 143 |
} |
| 144 |
} else { |
| 145 |
$resultsCounter['ignored'] ++; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Prepares packet attributes. |
| 151 |
* |
| 152 |
* @param Entity\Order $commonEntity Order entity. |
| 153 |
* |
| 154 |
* @return CreatePacket |
| 155 |
* @throws InvalidRequestException For the case request is not eligible to be sent to API. |
| 156 |
*/ |
| 157 |
private function preparePacketRequest( Entity\Order $commonEntity ): CreatePacket { |
| 158 |
/* |
| 159 |
TODO: extend validator to return specific errors. |
| 160 |
if ( ! $this->orderValidator->validate( $commonEntity ) ) { |
| 161 |
throw new InvalidRequestException( 'All required order attributes are not set.' ); |
| 162 |
} |
| 163 |
*/ |
| 164 |
|
| 165 |
return new CreatePacket( $commonEntity ); |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|