| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Synchronizer |
| 4 |
* |
| 5 |
* @package Packetery\Module\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Api; |
| 13 |
use Packetery\Core\Entity\Order; |
| 14 |
use Packetery\Core\Entity\PacketStatus; |
| 15 |
use Packetery\Core\Log; |
| 16 |
use Packetery\Module\Exception\InvalidPasswordException; |
| 17 |
use Packetery\Module\Framework\WcAdapter; |
| 18 |
use Packetery\Module\Options\OptionsProvider; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Synchronizer |
| 22 |
* |
| 23 |
* @package Packetery\Module\Order |
| 24 |
*/ |
| 25 |
class PacketSynchronizer { |
| 26 |
|
| 27 |
private const HOOK_NAME_SYNC_ORDER_STATUS = 'packetery_sync_order_status'; |
| 28 |
|
| 29 |
/** |
| 30 |
* API soap client. |
| 31 |
* |
| 32 |
* @var Api\Soap\Client |
| 33 |
*/ |
| 34 |
private $apiSoapClient; |
| 35 |
|
| 36 |
/** |
| 37 |
* Options provider. |
| 38 |
* |
| 39 |
* @var OptionsProvider |
| 40 |
*/ |
| 41 |
private $optionsProvider; |
| 42 |
|
| 43 |
/** |
| 44 |
* Logger. |
| 45 |
* |
| 46 |
* @var Log\ILogger |
| 47 |
*/ |
| 48 |
private $logger; |
| 49 |
|
| 50 |
/** |
| 51 |
* Order repository. |
| 52 |
* |
| 53 |
* @var Repository |
| 54 |
*/ |
| 55 |
private $orderRepository; |
| 56 |
|
| 57 |
/** |
| 58 |
* WC order actions. |
| 59 |
* |
| 60 |
* @var WcOrderActions |
| 61 |
*/ |
| 62 |
private $wcOrderActions; |
| 63 |
|
| 64 |
/** |
| 65 |
* WC adapter. |
| 66 |
* |
| 67 |
* @var WcAdapter |
| 68 |
*/ |
| 69 |
private $wcAdapter; |
| 70 |
|
| 71 |
/** |
| 72 |
* Constructor. |
| 73 |
* |
| 74 |
* @param Api\Soap\Client $apiSoapClient API soap client. |
| 75 |
* @param Log\ILogger $logger Logger. |
| 76 |
* @param OptionsProvider $optionsProvider Options provider. |
| 77 |
* @param Repository $orderRepository Order repository. |
| 78 |
* @param WcOrderActions $wcOrderActions WC order actions. |
| 79 |
* @param WcAdapter $wcAdapter WC adapter. |
| 80 |
*/ |
| 81 |
public function __construct( |
| 82 |
Api\Soap\Client $apiSoapClient, |
| 83 |
Log\ILogger $logger, |
| 84 |
OptionsProvider $optionsProvider, |
| 85 |
Repository $orderRepository, |
| 86 |
WcOrderActions $wcOrderActions, |
| 87 |
WcAdapter $wcAdapter |
| 88 |
) { |
| 89 |
$this->apiSoapClient = $apiSoapClient; |
| 90 |
$this->logger = $logger; |
| 91 |
$this->optionsProvider = $optionsProvider; |
| 92 |
$this->orderRepository = $orderRepository; |
| 93 |
$this->wcOrderActions = $wcOrderActions; |
| 94 |
$this->wcAdapter = $wcAdapter; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register hook. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function register() { |
| 103 |
add_action( self::HOOK_NAME_SYNC_ORDER_STATUS, [ $this, 'syncStatusById' ] ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Synchronizes packets. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
* @throws \Exception Exception. |
| 111 |
*/ |
| 112 |
public function syncStatuses(): void { |
| 113 |
$syncingOrderIds = $this->orderRepository->findStatusSyncingOrderIds( |
| 114 |
$this->optionsProvider->getStatusSyncingPacketStatuses(), |
| 115 |
$this->optionsProvider->getExistingStatusSyncingOrderStatuses(), |
| 116 |
$this->optionsProvider->getMaxDaysOfPacketStatusSyncing(), |
| 117 |
$this->optionsProvider->getMaxStatusSyncingPackets() |
| 118 |
); |
| 119 |
|
| 120 |
foreach ( $syncingOrderIds as $orderId ) { |
| 121 |
$this->wcAdapter->asScheduleSingleAction( time(), self::HOOK_NAME_SYNC_ORDER_STATUS, [ $orderId ] ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Synchronizes status for one order. |
| 127 |
* |
| 128 |
* @param int $orderId Order id. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function syncStatusById( int $orderId ): void { |
| 133 |
$order = $this->orderRepository->getById( $orderId ); |
| 134 |
if ( $order === null ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$this->syncStatus( $order ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Synchronizes status for one order. |
| 143 |
* |
| 144 |
* @param Order $order Order. |
| 145 |
* |
| 146 |
* @return void |
| 147 |
* @throws InvalidPasswordException InvalidPasswordException. |
| 148 |
*/ |
| 149 |
public function syncStatus( Order $order ): void { |
| 150 |
$packetId = $order->getPacketId(); |
| 151 |
|
| 152 |
$request = new Api\Soap\Request\PacketStatus( $packetId ); |
| 153 |
$response = $this->apiSoapClient->packetStatus( $request ); |
| 154 |
|
| 155 |
if ( $response->hasFault() ) { |
| 156 |
$record = new Log\Record(); |
| 157 |
$record->action = Log\Record::ACTION_PACKET_STATUS_SYNC; |
| 158 |
$record->status = Log\Record::STATUS_ERROR; |
| 159 |
$record->title = __( 'Packet status could not be synchronized.', 'packeta' ); |
| 160 |
$record->params = [ |
| 161 |
'orderId' => $order->getNumber(), |
| 162 |
'packetId' => $request->getPacketId(), |
| 163 |
'errorMessage' => $response->getFaultString(), |
| 164 |
]; |
| 165 |
$record->orderId = $order->getNumber(); |
| 166 |
$this->logger->add( $record ); |
| 167 |
|
| 168 |
if ( $response->hasWrongPassword() ) { |
| 169 |
throw new InvalidPasswordException( 'Wrong password.' ); |
| 170 |
} |
| 171 |
|
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$storedUntilResponse = $response->getStoredUntil(); |
| 176 |
$storedUntilOrder = $order->getStoredUntil(); |
| 177 |
|
| 178 |
if ( $storedUntilResponse === $storedUntilOrder && $response->getCodeText() === $order->getPacketStatus() ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if ( $storedUntilResponse !== $storedUntilOrder ) { |
| 183 |
$order->setStoredUntil( $storedUntilResponse ); |
| 184 |
} |
| 185 |
|
| 186 |
if ( $response->getCodeText() !== $order->getPacketStatus() ) { |
| 187 |
$order->setPacketStatus( $response->getCodeText() ); |
| 188 |
$this->wcOrderActions->updateOrderStatus( $order->getNumber(), $response->getCodeText() ); |
| 189 |
} |
| 190 |
|
| 191 |
$this->orderRepository->save( $order ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Gets packet statuses and default values. |
| 196 |
* |
| 197 |
* @return PacketStatus[] |
| 198 |
*/ |
| 199 |
public static function getPacketStatuses(): array { |
| 200 |
return [ |
| 201 |
PacketStatus::RECEIVED_DATA => |
| 202 |
new PacketStatus( PacketStatus::RECEIVED_DATA, __( 'Awaiting consignment', 'packeta' ), true ), |
| 203 |
PacketStatus::ARRIVED => |
| 204 |
new PacketStatus( PacketStatus::ARRIVED, __( 'Accepted at depot', 'packeta' ), true ), |
| 205 |
PacketStatus::PREPARED_FOR_DEPARTURE => |
| 206 |
new PacketStatus( PacketStatus::PREPARED_FOR_DEPARTURE, __( 'On the way', 'packeta' ), true ), |
| 207 |
PacketStatus::DEPARTED => |
| 208 |
new PacketStatus( PacketStatus::DEPARTED, __( 'Departed from depot', 'packeta' ), true ), |
| 209 |
PacketStatus::READY_FOR_PICKUP => |
| 210 |
new PacketStatus( PacketStatus::READY_FOR_PICKUP, __( 'Ready for pick-up', 'packeta' ), true ), |
| 211 |
PacketStatus::HANDED_TO_CARRIER => |
| 212 |
new PacketStatus( PacketStatus::HANDED_TO_CARRIER, __( 'Handed over to carrier company', 'packeta' ), true ), |
| 213 |
PacketStatus::DELIVERED => |
| 214 |
new PacketStatus( PacketStatus::DELIVERED, __( 'Delivered', 'packeta' ), false ), |
| 215 |
PacketStatus::POSTED_BACK => |
| 216 |
new PacketStatus( PacketStatus::POSTED_BACK, __( 'Return (on the way back)', 'packeta' ), true ), |
| 217 |
PacketStatus::RETURNED => |
| 218 |
new PacketStatus( PacketStatus::RETURNED, __( 'Returned to sender', 'packeta' ), false ), |
| 219 |
PacketStatus::CANCELLED => |
| 220 |
new PacketStatus( PacketStatus::CANCELLED, __( 'Cancelled', 'packeta' ), false ), |
| 221 |
PacketStatus::COLLECTED => |
| 222 |
new PacketStatus( PacketStatus::COLLECTED, __( 'Parcel has been collected', 'packeta' ), true ), |
| 223 |
PacketStatus::CUSTOMS => |
| 224 |
new PacketStatus( PacketStatus::CUSTOMS, __( 'Customs declaration process', 'packeta' ), true ), |
| 225 |
PacketStatus::REVERSE_PACKET_ARRIVED => |
| 226 |
new PacketStatus( PacketStatus::REVERSE_PACKET_ARRIVED, __( 'Reverse parcel has been accepted at our pick up point', 'packeta' ), true ), |
| 227 |
PacketStatus::DELIVERY_ATTEMPT => |
| 228 |
new PacketStatus( PacketStatus::DELIVERY_ATTEMPT, __( 'Unsuccessful delivery attempt of parcel', 'packeta' ), true ), |
| 229 |
PacketStatus::REJECTED_BY_RECIPIENT => |
| 230 |
new PacketStatus( PacketStatus::REJECTED_BY_RECIPIENT, __( 'Rejected by recipient response', 'packeta' ), true ), |
| 231 |
PacketStatus::UNKNOWN => |
| 232 |
new PacketStatus( PacketStatus::UNKNOWN, __( 'Unknown parcel status', 'packeta' ), false ), |
| 233 |
]; |
| 234 |
} |
| 235 |
} |
| 236 |
|