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