| @@ -1,25 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class BulkActions | |
| 4 | - * | |
| 5 | - * @package Packetery\Order | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| 11 | 6 | |
| 12 | -use Packetery\Module\Options; | |
| 13 | -use PacketeryLatte\Engine; | |
| 14 | -use PacketeryNette\Http\Request; | |
| 7 | +use Packetery\Latte\Engine; | |
| 8 | +use Packetery\Module\WcLogger; | |
| 9 | +use Packetery\Nette\Http\Request; | |
| 10 | +use WC_Order; | |
| 15 | 11 | |
| 16 | -/** | |
| 17 | - * Class BulkActions | |
| 18 | - * | |
| 19 | - * @package Packetery\Order | |
| 20 | - */ | |
| 21 | 12 | class BulkActions { |
| 13 | + const ACTION_SUBMIT_TO_API = 'submit_to_api'; | |
| 14 | + | |
| 22 | 15 | /** |
| 23 | 16 | * Latte engine. |
| 24 | 17 | * |
| 25 | 18 | * @var Engine |
| @@ -40,15 +33,8 @@ | ||
| 40 | 33 | */ |
| 41 | 34 | private $packetSubmitter; |
| 42 | 35 | |
| 43 | 36 | /** |
| 44 | - * Options provider. | |
| 45 | - * | |
| 46 | - * @var Options\Provider | |
| 47 | - */ | |
| 48 | - private $optionsProvider; | |
| 49 | - | |
| 50 | - /** | |
| 51 | 37 | * Order repository. |
| 52 | 38 | * |
| 53 | 39 | * @var Repository |
| 54 | 40 | */ |
| @@ -56,25 +42,22 @@ | ||
| 56 | 42 | |
| 57 | 43 | /** |
| 58 | 44 | * BulkActions constructor. |
| 59 | 45 | * |
| 60 | - * @param Engine $latteEngine Latte engine. | |
| 61 | - * @param Request $httpRequest HTTP request. | |
| 62 | - * @param PacketSubmitter $packetSubmitter Order API Client. | |
| 63 | - * @param Options\Provider $optionsProvider Options provider. | |
| 64 | - * @param Repository $orderRepository Order repository. | |
| 46 | + * @param Engine $latteEngine Latte engine. | |
| 47 | + * @param Request $httpRequest HTTP request. | |
| 48 | + * @param PacketSubmitter $packetSubmitter Order API Client. | |
| 49 | + * @param Repository $orderRepository Order repository. | |
| 65 | 50 | */ |
| 66 | 51 | public function __construct( |
| 67 | 52 | Engine $latteEngine, |
| 68 | 53 | Request $httpRequest, |
| 69 | 54 | PacketSubmitter $packetSubmitter, |
| 70 | - Options\Provider $optionsProvider, | |
| 71 | 55 | Repository $orderRepository |
| 72 | 56 | ) { |
| 73 | 57 | $this->latteEngine = $latteEngine; |
| 74 | 58 | $this->httpRequest = $httpRequest; |
| 75 | 59 | $this->packetSubmitter = $packetSubmitter; |
| 76 | - $this->optionsProvider = $optionsProvider; | |
| 77 | 60 | $this->orderRepository = $orderRepository; |
| 78 | 61 | } |
| 79 | 62 | |
| 80 | 63 | /** |
| @@ -79,18 +62,24 @@ | ||
| 79 | 62 | |
| 80 | 63 | /** |
| 81 | 64 | * Adds custom actions to dropdown in admin order list. |
| 82 | 65 | * |
| 83 | - * @param array $actions Array of action. | |
| 66 | + * @param array<string, string>|mixed $actions Array of action. | |
| 84 | 67 | * |
| 85 | - * @return array | |
| 68 | + * @return array<string, string>|mixed | |
| 86 | 69 | */ |
| 87 | - public function addActions( array $actions ): array { | |
| 88 | - $actions['submit_to_api'] = __( 'Submit orders to Packeta', 'packeta' ); | |
| 89 | - $actions[ LabelPrint::ACTION_PACKETA_LABELS ] = __( 'Print labels', 'packeta' ); | |
| 90 | - $actions[ LabelPrint::ACTION_CARRIER_LABELS ] = __( 'Print carrier labels', 'packeta' ); | |
| 91 | - $actions[ CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ] = __( 'Print AWB', 'packeta' ); | |
| 70 | + public function addActions( $actions ) { | |
| 71 | + if ( ! is_array( $actions ) ) { | |
| 72 | + WcLogger::logArgumentTypeError( __METHOD__, 'actions', 'array', $actions ); | |
| 92 | 73 | |
| 74 | + return $actions; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $actions[ self::ACTION_SUBMIT_TO_API ] = __( 'Packeta export', 'packeta' ); | |
| 78 | + $actions[ LabelPrint::ACTION_PACKETA_LABELS ] = __( 'Packeta download labels', 'packeta' ); | |
| 79 | + $actions[ LabelPrint::ACTION_CARRIER_LABELS ] = __( 'Packeta download carrier labels', 'packeta' ); | |
| 80 | + $actions[ CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ] = __( 'Packeta AWB (delivery note)', 'packeta' ); | |
| 81 | + | |
| 93 | 82 | return $actions; |
| 94 | 83 | } |
| 95 | 84 | |
| 96 | 85 | /** |
| @@ -95,16 +84,34 @@ | ||
| 95 | 84 | |
| 96 | 85 | /** |
| 97 | 86 | * Executes the action for selected orders and returns url to redirect to. |
| 98 | 87 | * |
| 99 | - * @param string $redirectTo Url. | |
| 100 | - * @param string $action Action id. | |
| 101 | - * @param array $postIds Order ids. | |
| 88 | + * @param string|mixed $redirectTo Url. | |
| 89 | + * @param string|mixed $action Action id. | |
| 90 | + * @param int[]|mixed $postIds Order ids. | |
| 102 | 91 | * |
| 103 | - * @return string | |
| 92 | + * @return string|mixed | |
| 104 | 93 | */ |
| 105 | - public function handleActions( string $redirectTo, string $action, array $postIds ): string { | |
| 106 | - if ( CollectionPrint::ACTION_PRINT_ORDER_COLLECTION === $action ) { | |
| 94 | + public function handleActions( $redirectTo, $action, $postIds ) { | |
| 95 | + if ( ! is_string( $redirectTo ) ) { | |
| 96 | + WcLogger::logArgumentTypeError( __METHOD__, 'redirectTo', 'string', $redirectTo ); | |
| 97 | + | |
| 98 | + return $redirectTo; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( ! is_string( $action ) ) { | |
| 102 | + WcLogger::logArgumentTypeError( __METHOD__, 'action', 'string', $action ); | |
| 103 | + | |
| 104 | + return $redirectTo; | |
| 105 | + } | |
| 106 | + | |
| 107 | + if ( ! is_array( $postIds ) ) { | |
| 108 | + WcLogger::logArgumentTypeError( __METHOD__, 'postIds', 'array', $postIds ); | |
| 109 | + | |
| 110 | + return $redirectTo; | |
| 111 | + } | |
| 112 | + | |
| 113 | + if ( $action === CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ) { | |
| 107 | 114 | set_transient( CollectionPrint::getOrderIdsTransientName(), $postIds ); |
| 108 | 115 | |
| 109 | 116 | return add_query_arg( |
| 110 | 117 | [ |
| @@ -126,23 +133,35 @@ | ||
| 126 | 133 | 'admin.php' |
| 127 | 134 | ); |
| 128 | 135 | } |
| 129 | 136 | |
| 130 | - if ( 'submit_to_api' === $action ) { | |
| 137 | + if ( $action === self::ACTION_SUBMIT_TO_API ) { | |
| 131 | 138 | $finalSubmissionResult = new PacketSubmissionResult(); |
| 132 | 139 | foreach ( $postIds as $postId ) { |
| 133 | - $wcOrder = $this->orderRepository->getWcOrderById( $postId ); | |
| 134 | - if ( null !== $wcOrder ) { | |
| 135 | - $submissionResult = $this->packetSubmitter->submitPacket( | |
| 136 | - $wcOrder, | |
| 137 | - $this->optionsProvider->isOrderStatusAutoChangeEnabled() | |
| 138 | - ); | |
| 139 | - $finalSubmissionResult->merge( $submissionResult ); | |
| 140 | + if ( ! is_numeric( $postId ) ) { | |
| 141 | + WcLogger::logArgumentTypeError( __METHOD__, 'postId', 'is_numeric', $postId ); | |
| 142 | + | |
| 143 | + continue; | |
| 140 | 144 | } |
| 145 | + | |
| 146 | + $wcOrder = $this->orderRepository->getWcOrderById( (int) $postId ); | |
| 147 | + | |
| 148 | + if ( ! $wcOrder instanceof WC_Order ) { | |
| 149 | + WcLogger::logArgumentTypeError( __METHOD__, 'wcOrder', 'WC_Order', $wcOrder ); | |
| 150 | + | |
| 151 | + continue; | |
| 152 | + } | |
| 153 | + | |
| 154 | + $submissionResult = $this->packetSubmitter->submitPacket( | |
| 155 | + $wcOrder, | |
| 156 | + null, | |
| 157 | + true | |
| 158 | + ); | |
| 159 | + $finalSubmissionResult->merge( $submissionResult ); | |
| 141 | 160 | } |
| 142 | 161 | |
| 143 | - $queryArgs = $finalSubmissionResult->getCounter(); | |
| 144 | - $queryArgs['submit_to_api'] = true; | |
| 162 | + $queryArgs = $finalSubmissionResult->getCounter(); | |
| 163 | + $queryArgs[ self::ACTION_SUBMIT_TO_API ] = true; | |
| 145 | 164 | |
| 146 | 165 | if ( count( $postIds ) === 1 ) { |
| 147 | 166 | $queryArgs['packetery_order_id'] = array_pop( $postIds ); |
| 148 | 167 | } |
| @@ -157,9 +176,9 @@ | ||
| 157 | 176 | * Renders packets export result. |
| 158 | 177 | */ |
| 159 | 178 | public function renderPacketsExportResult(): void { |
| 160 | 179 | $get = $this->httpRequest->getQuery(); |
| 161 | - if ( empty( $get['submit_to_api'] ) ) { | |
| 180 | + if ( ! isset( $get[ self::ACTION_SUBMIT_TO_API ] ) ) { | |
| 162 | 181 | return; |
| 163 | 182 | } |
| 164 | 183 | |
| 165 | 184 | $orderId = ( $get['packetery_order_id'] ?? null ); |