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
← All changes | src/Packetery/Module/Order/PacketSynchronizer.php +155 -33 1.2.32.0.9 View file →
@@ -6,14 +6,17 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module\Order;
12 11
13 12 use Packetery\Core\Api;
13 +use Packetery\Core\Entity\Order;
14 +use Packetery\Core\Entity\PacketStatus;
14 15 use Packetery\Core\Log;
15 -use Packetery\Module\Options;
16 +use Packetery\Module\Exception\InvalidPasswordException;
17 +use Packetery\Module\Framework\WcAdapter;
18 +use Packetery\Module\Options\OptionsProvider;
16 19
17 20 /**
18 21 * Class Synchronizer
19 22 *
@@ -20,8 +23,10 @@
20 23 * @package Packetery\Module\Order
21 24 */
22 25 class PacketSynchronizer {
23 26
27 + private const HOOK_NAME_SYNC_ORDER_STATUS = 'packetery_sync_order_status';
28 +
24 29 /**
25 30 * API soap client.
26 31 *
27 32 * @var Api\Soap\Client
@@ -30,9 +35,9 @@
30 35
31 36 /**
32 37 * Options provider.
33 38 *
34 - * @var Options\Provider
39 + * @var OptionsProvider
35 40 */
36 41 private $optionsProvider;
37 42
38 43 /**
@@ -49,65 +54,182 @@
49 54 */
50 55 private $orderRepository;
51 56
52 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 + /**
53 72 * Constructor.
54 73 *
55 - * @param Api\Soap\Client $apiSoapClient API soap client.
56 - * @param Log\ILogger $logger Logger.
57 - * @param Options\Provider $optionsProvider Options provider.
58 - * @param Repository $orderRepository Order repository.
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.
59 80 */
60 81 public function __construct(
61 82 Api\Soap\Client $apiSoapClient,
62 83 Log\ILogger $logger,
63 - Options\Provider $optionsProvider,
64 - Repository $orderRepository
84 + OptionsProvider $optionsProvider,
85 + Repository $orderRepository,
86 + WcOrderActions $wcOrderActions,
87 + WcAdapter $wcAdapter
65 88 ) {
66 89 $this->apiSoapClient = $apiSoapClient;
67 90 $this->logger = $logger;
68 91 $this->optionsProvider = $optionsProvider;
69 92 $this->orderRepository = $orderRepository;
93 + $this->wcOrderActions = $wcOrderActions;
94 + $this->wcAdapter = $wcAdapter;
70 95 }
71 96
72 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 + /**
73 107 * Synchronizes packets.
74 108 *
75 109 * @return void
110 + * @throws \Exception Exception.
76 111 */
77 112 public function syncStatuses(): void {
78 - $results = $this->orderRepository->findStatusSyncingOrders( $this->optionsProvider->getMaxStatusSyncingPackets() );
113 + $syncingOrderIds = $this->orderRepository->findStatusSyncingOrderIds(
114 + $this->optionsProvider->getStatusSyncingPacketStatuses(),
115 + $this->optionsProvider->getExistingStatusSyncingOrderStatuses(),
116 + $this->optionsProvider->getMaxDaysOfPacketStatusSyncing(),
117 + $this->optionsProvider->getMaxStatusSyncingPackets()
118 + );
79 119
80 - foreach ( $results as $order ) {
81 - $packetId = $order->getPacketId();
120 + foreach ( $syncingOrderIds as $orderId ) {
121 + $this->wcAdapter->asScheduleSingleAction( time(), self::HOOK_NAME_SYNC_ORDER_STATUS, [ $orderId ] );
122 + }
123 + }
82 124
83 - $request = new Api\Soap\Request\PacketStatus( (int) $packetId );
84 - $response = $this->apiSoapClient->packetStatus( $request );
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 + }
85 137
86 - if ( $response->hasFault() ) {
87 - $record = new Log\Record();
88 - $record->action = Log\Record::ACTION_PACKET_STATUS_SYNC;
89 - $record->status = Log\Record::STATUS_ERROR;
90 - $record->title = __( 'packetStatusSyncErrorLogTitle', 'packetery' );
91 - $record->params = [
92 - 'orderId' => $order->getNumber(),
93 - 'packetId' => $request->getPacketId(),
94 - 'errorMessage' => $response->getFaultString(),
95 - ];
96 - $this->logger->add( $record );
138 + $this->syncStatus( $order );
139 + }
97 140
98 - if ( $response->hasWrongPassword() ) {
99 - break;
100 - }
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();
101 151
102 - continue;
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.' );
103 170 }
104 171
105 - if ( $response->getCodeText() === $order->getPacketStatus() ) {
106 - continue;
107 - }
172 + return;
173 + }
108 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() ) {
109 187 $order->setPacketStatus( $response->getCodeText() );
110 - $this->orderRepository->save( $order );
188 + $this->wcOrderActions->updateOrderStatus( $order->getNumber(), $response->getCodeText() );
111 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 + ];
112 234 }
113 235 }