← All changes
|
src/Packetery/Module/Order/PacketAutoSubmitter.php
+54
-51
1.5.3
→
2.3.2
View file →
| @@ -8,9 +8,12 @@ | ||
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | -use Packetery\Module; | |
| 12 | +use Packetery\Module\Options\OptionsProvider; | |
| 13 | +use Packetery\Module\PaymentGatewayHelper; | |
| 14 | +use Packetery\Module\WcLogger; | |
| 15 | +use WC_Payment_Gateway; | |
| 13 | 16 | |
| 14 | 17 | /** |
| 15 | 18 | * Class PacketAutoSubmitter |
| 16 | 19 | * |
| @@ -26,9 +29,9 @@ | ||
| 26 | 29 | |
| 27 | 30 | /** |
| 28 | 31 | * Options provider. |
| 29 | 32 | * |
| 30 | - * @var Module\Options\Provider | |
| 33 | + * @var OptionsProvider | |
| 31 | 34 | */ |
| 32 | 35 | private $optionsProvider; |
| 33 | 36 | |
| 34 | 37 | /** |
| @@ -38,15 +41,8 @@ | ||
| 38 | 41 | */ |
| 39 | 42 | private $packetSubmitter; |
| 40 | 43 | |
| 41 | 44 | /** |
| 42 | - * Options page. | |
| 43 | - * | |
| 44 | - * @var Module\Options\Page | |
| 45 | - */ | |
| 46 | - private $optionsPage; | |
| 47 | - | |
| 48 | - /** | |
| 49 | 45 | * Order repository. |
| 50 | 46 | * |
| 51 | 47 | * @var Repository |
| 52 | 48 | */ |
| @@ -54,22 +50,19 @@ | ||
| 54 | 50 | |
| 55 | 51 | /** |
| 56 | 52 | * Constructor. |
| 57 | 53 | * |
| 58 | - * @param Module\Options\Provider $optionsProvider Options provider. | |
| 59 | - * @param PacketSubmitter $packetSubmitter Packet submitter. | |
| 60 | - * @param Module\Options\Page $optionsPage Options page. | |
| 61 | - * @param Repository $orderRepository Order repository. | |
| 54 | + * @param OptionsProvider $optionsProvider Options provider. | |
| 55 | + * @param PacketSubmitter $packetSubmitter Packet submitter. | |
| 56 | + * @param Repository $orderRepository Order repository. | |
| 62 | 57 | */ |
| 63 | 58 | public function __construct( |
| 64 | - Module\Options\Provider $optionsProvider, | |
| 59 | + OptionsProvider $optionsProvider, | |
| 65 | 60 | PacketSubmitter $packetSubmitter, |
| 66 | - Module\Options\Page $optionsPage, | |
| 67 | 61 | Repository $orderRepository |
| 68 | 62 | ) { |
| 69 | 63 | $this->optionsProvider = $optionsProvider; |
| 70 | 64 | $this->packetSubmitter = $packetSubmitter; |
| 71 | - $this->optionsPage = $optionsPage; | |
| 72 | 65 | $this->orderRepository = $orderRepository; |
| 73 | 66 | } |
| 74 | 67 | |
| 75 | 68 | /** |
| @@ -77,31 +70,44 @@ | ||
| 77 | 70 | * |
| 78 | 71 | * @return void |
| 79 | 72 | */ |
| 80 | 73 | public function register(): void { |
| 81 | - if ( false === $this->optionsProvider->isPacketAutoSubmissionEnabled() ) { | |
| 74 | + if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { | |
| 82 | 75 | return; |
| 83 | 76 | } |
| 84 | 77 | |
| 85 | - add_action( self::HOOK_NAME_HANDLE_EVENT, [ $this, 'handleEvent' ], 10, 3 ); | |
| 78 | + add_action( self::HOOK_NAME_HANDLE_EVENT, [ $this, 'handleEvent' ], 10, 2 ); | |
| 86 | 79 | |
| 87 | 80 | $mappedEvents = $this->optionsProvider->getPacketAutoSubmissionMappedUniqueEvents(); |
| 88 | 81 | foreach ( $mappedEvents as $mappedEvent ) { |
| 89 | - if ( self::EVENT_ON_ORDER_COMPLETED === $mappedEvent ) { | |
| 82 | + if ( $mappedEvent === self::EVENT_ON_ORDER_COMPLETED ) { | |
| 90 | 83 | add_action( |
| 91 | 84 | 'woocommerce_order_status_completed', |
| 92 | - function ( int $orderId ): void { | |
| 93 | - $this->handleEvent( self::EVENT_ON_ORDER_COMPLETED, $orderId, is_admin() === false ); | |
| 85 | + function ( $orderId ): void { | |
| 86 | + if ( ! is_int( $orderId ) ) { | |
| 87 | + WcLogger::logArgumentTypeError( __METHOD__, 'orderId', 'int', $orderId ); | |
| 88 | + | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $this->handleEvent( self::EVENT_ON_ORDER_COMPLETED, $orderId ); | |
| 94 | 93 | } |
| 95 | 94 | ); |
| 95 | + | |
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - if ( self::EVENT_ON_ORDER_PROCESSING === $mappedEvent ) { | |
| 99 | + if ( $mappedEvent === self::EVENT_ON_ORDER_PROCESSING ) { | |
| 100 | 100 | add_action( |
| 101 | 101 | 'woocommerce_order_status_processing', |
| 102 | - function ( int $orderId ): void { | |
| 103 | - $this->handleEvent( self::EVENT_ON_ORDER_PROCESSING, $orderId, is_admin() === false ); | |
| 102 | + function ( $orderId ): void { | |
| 103 | + if ( ! is_int( $orderId ) ) { | |
| 104 | + WcLogger::logArgumentTypeError( __METHOD__, 'orderId', 'int', $orderId ); | |
| 105 | + | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + $this->handleEvent( self::EVENT_ON_ORDER_PROCESSING, $orderId ); | |
| 104 | 110 | } |
| 105 | 111 | ); |
| 106 | 112 | } |
| 107 | 113 | } |
| @@ -109,24 +115,37 @@ | ||
| 109 | 115 | |
| 110 | 116 | /** |
| 111 | 117 | * Handle event. |
| 112 | 118 | * |
| 113 | - * @param string $event Event. | |
| 114 | - * @param int $orderId WC Order. | |
| 115 | - * @param bool|null $triggeredByFrontend Tells if event is triggered by frontend logic. NULL is passed when data from previous versions are being processed. | |
| 116 | - * | |
| 119 | + * @param string|mixed $event Event. | |
| 120 | + * @param int|mixed $orderId WC Order. | |
| 117 | 121 | * @return void |
| 118 | 122 | */ |
| 119 | - public function handleEvent( string $event, int $orderId, ?bool $triggeredByFrontend = null ): void { | |
| 120 | - if ( false === $this->optionsProvider->isPacketAutoSubmissionEnabled() ) { | |
| 123 | + public function handleEvent( $event, $orderId ): void { | |
| 124 | + if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { | |
| 121 | 125 | return; |
| 122 | 126 | } |
| 123 | 127 | |
| 128 | + if ( ! is_string( $event ) ) { | |
| 129 | + WcLogger::logArgumentTypeError( __METHOD__, 'event', 'string', $event ); | |
| 130 | + | |
| 131 | + return; | |
| 132 | + } | |
| 133 | + | |
| 134 | + if ( ! is_int( $orderId ) ) { | |
| 135 | + WcLogger::logArgumentTypeError( __METHOD__, 'orderId', 'int', $orderId ); | |
| 136 | + | |
| 137 | + return; | |
| 138 | + } | |
| 139 | + | |
| 124 | 140 | $wcOrder = $this->orderRepository->getWcOrderById( $orderId ); |
| 125 | - assert( null !== $wcOrder, 'WC order has to be present' ); | |
| 141 | + assert( $wcOrder !== null, 'WC order has to be present' ); | |
| 126 | 142 | |
| 127 | 143 | $paymentGateway = wc_get_payment_gateway_by_order( $wcOrder ); |
| 128 | - if ( false === $this->optionsPage->hasPaymentGateway( $paymentGateway ) ) { | |
| 144 | + if ( | |
| 145 | + ! $paymentGateway instanceof WC_Payment_Gateway || | |
| 146 | + array_key_exists( $paymentGateway->id, PaymentGatewayHelper::getAvailablePaymentGateways() ) === false | |
| 147 | + ) { | |
| 129 | 148 | return; |
| 130 | 149 | } |
| 131 | 150 | |
| 132 | 151 | $mappingEventForGateway = $this->optionsProvider->getPacketAutoSubmissionEventForPaymentGateway( |
| @@ -131,31 +150,16 @@ | ||
| 131 | 150 | |
| 132 | 151 | $mappingEventForGateway = $this->optionsProvider->getPacketAutoSubmissionEventForPaymentGateway( |
| 133 | 152 | $this->optionsProvider->sanitizePaymentGatewayId( $paymentGateway->id ) |
| 134 | 153 | ); |
| 135 | - if ( null === $mappingEventForGateway || $mappingEventForGateway !== $event ) { | |
| 154 | + if ( $mappingEventForGateway === null || $mappingEventForGateway !== $event ) { | |
| 136 | 155 | return; |
| 137 | 156 | } |
| 138 | 157 | |
| 139 | - $this->packetSubmitter->submitPacket( | |
| 140 | - $wcOrder, | |
| 141 | - $this->shouldUpdateOrderStatus( $triggeredByFrontend ) | |
| 142 | - ); | |
| 158 | + $this->packetSubmitter->submitPacket( $wcOrder ); | |
| 143 | 159 | } |
| 144 | 160 | |
| 145 | 161 | /** |
| 146 | - * Tells if WC order status should be updated. | |
| 147 | - * | |
| 148 | - * @param bool|null $triggeredByFrontend Tells if submission is triggered by frontend action. | |
| 149 | - * | |
| 150 | - * @return bool | |
| 151 | - */ | |
| 152 | - private function shouldUpdateOrderStatus( ?bool $triggeredByFrontend ): bool { | |
| 153 | - return ( false === $triggeredByFrontend && $this->optionsProvider->isOrderStatusAutoChangeEnabled() ) || | |
| 154 | - ( true === $triggeredByFrontend && $this->optionsProvider->isOrderStatusAutoChangeForAutoSubmitAtFrontendEnabled() ); | |
| 155 | - } | |
| 156 | - | |
| 157 | - /** | |
| 158 | 162 | * Handles event async. |
| 159 | 163 | * |
| 160 | 164 | * @param string $event Event. |
| 161 | 165 | * @param int $orderId Order ID. |
| @@ -162,11 +166,10 @@ | ||
| 162 | 166 | * |
| 163 | 167 | * @return void |
| 164 | 168 | */ |
| 165 | 169 | public function handleEventAsync( string $event, int $orderId ): void { |
| 166 | - if ( false === $this->optionsProvider->isPacketAutoSubmissionEnabled() ) { | |
| 170 | + if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { | |
| 167 | 171 | return; |
| 168 | 172 | } |
| 169 | 173 | as_schedule_single_action( time(), self::HOOK_NAME_HANDLE_EVENT, [ $event, $orderId, is_admin() === false ] ); |
| 170 | 174 | } |
| 171 | - | |
| 172 | 175 | } |