| @@ -1,10 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class PacketClaimSubmitter. | |
| 4 | - * | |
| 5 | - * @package Packetery | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| @@ -9,78 +4,62 @@ | ||
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| 11 | 6 | |
| 12 | 7 | use Packetery\Core\Api\Soap; |
| 8 | +use Packetery\Core\CoreHelper; | |
| 13 | 9 | use Packetery\Core\Log; |
| 10 | +use Packetery\Module; | |
| 14 | 11 | use Packetery\Module\MessageManager; |
| 12 | +use Packetery\Module\ModuleHelper; | |
| 15 | 13 | use Packetery\Nette\Http\Request; |
| 16 | -use Packetery\Module; | |
| 17 | 14 | |
| 18 | -/** | |
| 19 | - * Class PacketClaimSubmitter. | |
| 20 | - */ | |
| 21 | 15 | class PacketClaimSubmitter { |
| 22 | 16 | |
| 23 | 17 | /** |
| 24 | - * SOAP API Client. | |
| 25 | - * | |
| 26 | - * @var Soap\Client SOAP API Client. | |
| 18 | + * @var Soap\Client | |
| 27 | 19 | */ |
| 28 | 20 | private $soapApiClient; |
| 29 | 21 | |
| 30 | 22 | /** |
| 31 | - * ILogger. | |
| 32 | - * | |
| 33 | 23 | * @var Log\ILogger |
| 34 | 24 | */ |
| 35 | 25 | private $logger; |
| 36 | 26 | |
| 37 | 27 | /** |
| 38 | - * Order repository. | |
| 39 | - * | |
| 40 | 28 | * @var Repository |
| 41 | 29 | */ |
| 42 | 30 | private $orderRepository; |
| 43 | 31 | |
| 44 | 32 | /** |
| 45 | - * Request. | |
| 46 | - * | |
| 47 | 33 | * @var Request |
| 48 | 34 | */ |
| 49 | 35 | private $request; |
| 50 | 36 | |
| 51 | 37 | /** |
| 52 | - * Message manager. | |
| 53 | - * | |
| 54 | 38 | * @var MessageManager |
| 55 | 39 | */ |
| 56 | 40 | private $messageManager; |
| 57 | 41 | |
| 58 | 42 | /** |
| 59 | - * Log page. | |
| 60 | - * | |
| 61 | 43 | * @var Module\Log\Page |
| 62 | 44 | */ |
| 63 | 45 | private $logPage; |
| 64 | 46 | |
| 65 | 47 | /** |
| 66 | - * Common logic. | |
| 67 | - * | |
| 68 | 48 | * @var PacketActionsCommonLogic |
| 69 | 49 | */ |
| 70 | 50 | private $commonLogic; |
| 71 | 51 | |
| 72 | 52 | /** |
| 73 | - * OrderApi constructor. | |
| 74 | - * | |
| 75 | - * @param Soap\Client $soapApiClient SOAP API Client. | |
| 76 | - * @param Log\ILogger $logger Logger. | |
| 77 | - * @param Repository $orderRepository Order repository. | |
| 78 | - * @param Request $request Request. | |
| 79 | - * @param MessageManager $messageManager Message manager. | |
| 80 | - * @param Module\Log\Page $logPage Log page. | |
| 81 | - * @param PacketActionsCommonLogic $commonLogic Common logic. | |
| 53 | + * @var ModuleHelper | |
| 82 | 54 | */ |
| 55 | + private $moduleHelper; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var CoreHelper | |
| 59 | + */ | |
| 60 | + private $coreHelper; | |
| 61 | + | |
| 83 | 62 | public function __construct( |
| 84 | 63 | Soap\Client $soapApiClient, |
| 85 | 64 | Log\ILogger $logger, |
| 86 | 65 | Repository $orderRepository, |
| @@ -86,9 +65,11 @@ | ||
| 86 | 65 | Repository $orderRepository, |
| 87 | 66 | Request $request, |
| 88 | 67 | MessageManager $messageManager, |
| 89 | 68 | Module\Log\Page $logPage, |
| 90 | - PacketActionsCommonLogic $commonLogic | |
| 69 | + PacketActionsCommonLogic $commonLogic, | |
| 70 | + ModuleHelper $moduleHelper, | |
| 71 | + CoreHelper $coreHelper | |
| 91 | 72 | ) { |
| 92 | 73 | $this->soapApiClient = $soapApiClient; |
| 93 | 74 | $this->logger = $logger; |
| 94 | 75 | $this->orderRepository = $orderRepository; |
| @@ -95,8 +76,10 @@ | ||
| 95 | 76 | $this->request = $request; |
| 96 | 77 | $this->messageManager = $messageManager; |
| 97 | 78 | $this->logPage = $logPage; |
| 98 | 79 | $this->commonLogic = $commonLogic; |
| 80 | + $this->moduleHelper = $moduleHelper; | |
| 81 | + $this->coreHelper = $coreHelper; | |
| 99 | 82 | } |
| 100 | 83 | |
| 101 | 84 | /** |
| 102 | 85 | * Process action |
| @@ -108,9 +91,9 @@ | ||
| 108 | 91 | $redirectTo = $this->request->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); |
| 109 | 92 | |
| 110 | 93 | $record = new Log\Record(); |
| 111 | 94 | $record->action = Log\Record::ACTION_PACKET_CLAIM_SENDING; |
| 112 | - if ( null === $order ) { | |
| 95 | + if ( $order === null ) { | |
| 113 | 96 | $record->status = Log\Record::STATUS_ERROR; |
| 114 | 97 | $record->orderId = null; |
| 115 | 98 | $record->title = __( 'Packet claim submission error', 'packeta' ); |
| 116 | 99 | $record->params = [ |
| @@ -128,9 +111,9 @@ | ||
| 128 | 111 | |
| 129 | 112 | $this->commonLogic->checkAction( PacketActionsCommonLogic::ACTION_SUBMIT_PACKET_CLAIM, $order ); |
| 130 | 113 | |
| 131 | 114 | $record->orderId = $order->getNumber(); |
| 132 | - if ( false === $order->isPacketClaimCreationPossible() ) { | |
| 115 | + if ( $order->isPacketClaimCreationPossible() === false ) { | |
| 133 | 116 | $record->status = Log\Record::STATUS_ERROR; |
| 134 | 117 | $record->title = __( 'Packet claim submission error', 'packeta' ); |
| 135 | 118 | $record->params = [ |
| 136 | 119 | 'origin' => (string) $this->request->getOrigin(), |
| @@ -142,9 +125,9 @@ | ||
| 142 | 125 | $this->logger->add( $record ); |
| 143 | 126 | |
| 144 | 127 | $faultFlashMessage = sprintf( // translators: 1: link start 2: link end. |
| 145 | 128 | esc_html__( 'Packet claim creation is not possible. %1$sShow logs%2$s', 'packeta' ), |
| 146 | - ...Module\Plugin::createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 129 | + ...$this->moduleHelper->createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 147 | 130 | ); |
| 148 | 131 | |
| 149 | 132 | $this->messageManager->flashMessageObject( |
| 150 | 133 | Module\Message::create() |
| @@ -169,9 +152,9 @@ | ||
| 169 | 152 | ]; |
| 170 | 153 | |
| 171 | 154 | $faultFlashMessage = sprintf( // translators: 1: link start 2: link end. |
| 172 | 155 | esc_html__( 'Packet claim could not be created. %1$sShow logs%2$s', 'packeta' ), |
| 173 | - ...Module\Plugin::createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 156 | + ...$this->moduleHelper->createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 174 | 157 | ); |
| 175 | 158 | |
| 176 | 159 | $this->messageManager->flashMessageObject( |
| 177 | 160 | Module\Message::create() |
| @@ -187,14 +170,27 @@ | ||
| 187 | 170 | 'packetId' => $response->getId(), |
| 188 | 171 | ]; |
| 189 | 172 | |
| 190 | 173 | $order->setPacketClaimId( $response->getId() ); |
| 174 | + $order->setPacketClaimTrackingUrl( $this->coreHelper->getTrackingUrl( $response->getId() ) ); | |
| 191 | 175 | $order->setPacketClaimPassword( $response->getPassword() ); |
| 192 | 176 | $this->orderRepository->save( $order ); |
| 193 | 177 | |
| 178 | + $wcOrder = $this->orderRepository->getWcOrderById( (int) $order->getNumber() ); | |
| 179 | + if ( $wcOrder !== null ) { | |
| 180 | + $wcOrder->add_order_note( | |
| 181 | + sprintf( | |
| 182 | + // translators: %s represents a packet tracking link. | |
| 183 | + __( 'Packeta: Packet claim %s has been created', 'packeta' ), | |
| 184 | + $this->moduleHelper->createHtmlLink( $order->getPacketClaimTrackingUrl(), $order->getPacketClaimBarcode() ) | |
| 185 | + ) | |
| 186 | + ); | |
| 187 | + $wcOrder->save(); | |
| 188 | + } | |
| 189 | + | |
| 194 | 190 | $flashMessage = sprintf( // translators: 1: link start 2: link end. |
| 195 | 191 | esc_html__( 'Packet claim submitted. %1$sShow logs%2$s', 'packeta' ), |
| 196 | - ...Module\Plugin::createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 192 | + ...$this->moduleHelper->createLinkParts( $this->logPage->createLogListUrl( (int) $order->getNumber() ) ) | |
| 197 | 193 | ); |
| 198 | 194 | |
| 199 | 195 | $this->messageManager->flashMessageObject( |
| 200 | 196 | Module\Message::create() |