| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Order; |
| 6 |
|
| 7 |
use Packetery\Core\Api\Soap; |
| 8 |
use Packetery\Core\Entity; |
| 9 |
use Packetery\Core\Log; |
| 10 |
use Packetery\Module\Framework\WcAdapter; |
| 11 |
use Packetery\Module\Options\OptionsProvider; |
| 12 |
|
| 13 |
class ConsignPasswordSynchronizer { |
| 14 |
|
| 15 |
private const HOOK_CONSIGN_PASSWORD_FETCH = 'packetery_consign_password_fetch_hook'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Soap\Client |
| 19 |
*/ |
| 20 |
private $soapApiClient; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var Log\ILogger |
| 24 |
*/ |
| 25 |
private $logger; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var Repository |
| 29 |
*/ |
| 30 |
private $orderRepository; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var OptionsProvider |
| 34 |
*/ |
| 35 |
private $optionsProvider; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var WcAdapter |
| 39 |
*/ |
| 40 |
private $wcAdapter; |
| 41 |
|
| 42 |
public function __construct( |
| 43 |
Soap\Client $soapApiClient, |
| 44 |
Log\ILogger $logger, |
| 45 |
Repository $orderRepository, |
| 46 |
OptionsProvider $optionsProvider, |
| 47 |
WcAdapter $wcAdapter |
| 48 |
) { |
| 49 |
$this->soapApiClient = $soapApiClient; |
| 50 |
$this->logger = $logger; |
| 51 |
$this->orderRepository = $orderRepository; |
| 52 |
$this->optionsProvider = $optionsProvider; |
| 53 |
$this->wcAdapter = $wcAdapter; |
| 54 |
} |
| 55 |
|
| 56 |
public function register(): void { |
| 57 |
add_action( |
| 58 |
self::HOOK_CONSIGN_PASSWORD_FETCH, |
| 59 |
function ( $orderId ): void { |
| 60 |
$this->processById( (string) $orderId ); |
| 61 |
}, |
| 62 |
10, |
| 63 |
1 |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
public function schedule( Entity\Order $order, bool $immediatePacketStatusCheck ): void { |
| 68 |
if ( ! $this->optionsProvider->isShowConsignPasswordForZBoxEnabled() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
if ( $order->getPacketId() === null ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$orderNumber = $order->getNumber(); |
| 76 |
if ( $orderNumber === null || $orderNumber === '' ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( $immediatePacketStatusCheck || ! $this->wcAdapter->isActionSchedulerEnqueueAvailable() ) { |
| 81 |
$this->processById( $orderNumber ); |
| 82 |
|
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$this->wcAdapter->asEnqueueAsyncAction( self::HOOK_CONSIGN_PASSWORD_FETCH, [ $orderNumber ] ); |
| 87 |
} |
| 88 |
|
| 89 |
public function processById( string $orderId ): void { |
| 90 |
if ( ! $this->optionsProvider->isShowConsignPasswordForZBoxEnabled() ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$order = $this->orderRepository->getByIdWithValidCarrier( (int) $orderId ); |
| 95 |
if ( $order === null ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$packetId = $order->getPacketId(); |
| 100 |
if ( $packetId === null ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$response = $this->soapApiClient->packetInfo( new Soap\Request\PacketInfo( $packetId ) ); |
| 105 |
if ( $response->hasFault() ) { |
| 106 |
$record = new Log\Record(); |
| 107 |
$record->action = Log\Record::ACTION_PACKET_INFO; |
| 108 |
$record->status = Log\Record::STATUS_ERROR; |
| 109 |
$record->title = __( 'Packet info request failed', 'packeta' ); |
| 110 |
$record->orderId = $orderId; |
| 111 |
$record->params = [ |
| 112 |
'request' => [ 'packetId' => $packetId ], |
| 113 |
'response' => [ |
| 114 |
'faultString' => $response->getFaultString(), |
| 115 |
], |
| 116 |
]; |
| 117 |
$this->logger->add( $record ); |
| 118 |
|
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$password = $response->getConsignPassword(); |
| 123 |
if ( $password === null || $password === '' ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$order->setConsignPassword( $password ); |
| 128 |
$this->orderRepository->save( $order ); |
| 129 |
} |
| 130 |
} |
| 131 |
|