| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PacketCanceller |
| 4 |
* |
| 5 |
* @package Packetery\Module\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Module\MessageManager; |
| 14 |
use Packetery\Module\ModuleHelper; |
| 15 |
use Packetery\Module\Plugin; |
| 16 |
use Packetery\Nette\Http\Request; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class PacketActionsBrain |
| 20 |
* |
| 21 |
* @package Packetery\Module\Order |
| 22 |
*/ |
| 23 |
class PacketActionsCommonLogic { |
| 24 |
|
| 25 |
public const PARAM_ORDER_ID = 'order_id'; |
| 26 |
public const PARAM_PACKET_ID = 'packet_id'; |
| 27 |
public const PARAM_REDIRECT_TO = 'packetery_redirect_to'; |
| 28 |
public const PARAM_ORDER_GRID_PARAMS = 'packetery_order_grid_params'; |
| 29 |
public const REDIRECT_TO_ORDER_GRID = 'order-grid'; |
| 30 |
public const REDIRECT_TO_ORDER_DETAIL = 'order-detail'; |
| 31 |
public const ACTION_CANCEL_PACKET = 'cancel_packet'; |
| 32 |
public const ACTION_SUBMIT_PACKET = 'submit_packet'; |
| 33 |
public const ACTION_SUBMIT_PACKET_CLAIM = 'submit_packet_claim'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Order repository. |
| 37 |
* |
| 38 |
* @var Repository |
| 39 |
*/ |
| 40 |
protected $orderRepository; |
| 41 |
|
| 42 |
/** |
| 43 |
* Request. |
| 44 |
* |
| 45 |
* @var Request |
| 46 |
*/ |
| 47 |
protected $request; |
| 48 |
|
| 49 |
/** |
| 50 |
* Message manager. |
| 51 |
* |
| 52 |
* @var MessageManager |
| 53 |
*/ |
| 54 |
protected $messageManager; |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor. |
| 58 |
* |
| 59 |
* @param Repository $orderRepository Order repository. |
| 60 |
* @param Request $request Request. |
| 61 |
* @param MessageManager $messageManager Message manager. |
| 62 |
*/ |
| 63 |
public function __construct( |
| 64 |
Repository $orderRepository, |
| 65 |
Request $request, |
| 66 |
MessageManager $messageManager |
| 67 |
) { |
| 68 |
$this->orderRepository = $orderRepository; |
| 69 |
$this->request = $request; |
| 70 |
$this->messageManager = $messageManager; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Process actions. |
| 75 |
* |
| 76 |
* @param string $action Action. |
| 77 |
* @param Entity\Order $order Order. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function checkAction( string $action, Entity\Order $order ): void { |
| 82 |
$redirectTo = $this->request->getQuery( self::PARAM_REDIRECT_TO ); |
| 83 |
|
| 84 |
if ( wp_verify_nonce( $this->request->getQuery( Plugin::PARAM_NONCE ), self::createNonceAction( $action, $order->getNumber() ) ) !== 1 ) { |
| 85 |
$this->messageManager->flash_message( __( 'Link has expired. Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 86 |
$this->redirectTo( $redirectTo, $order ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public static function createNonceAction( string $action, ?string $orderNumber ): string { |
| 91 |
if ( $orderNumber === null ) { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
return $action . '_' . $orderNumber; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Redirects. |
| 100 |
* |
| 101 |
* @param string $redirectTo Redirect to. |
| 102 |
* @param Entity\Order|null $order Order. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function redirectTo( string $redirectTo, ?Entity\Order $order = null ): void { |
| 107 |
if ( $redirectTo === self::REDIRECT_TO_ORDER_GRID ) { |
| 108 |
$queryVars = []; |
| 109 |
parse_str( $this->request->getQuery( self::PARAM_ORDER_GRID_PARAMS ) ?? '', $queryVars ); |
| 110 |
|
| 111 |
if ( wp_safe_redirect( ModuleHelper::getOrderGridUrl( $queryVars ) ) ) { |
| 112 |
exit; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( |
| 117 |
$redirectTo === self::REDIRECT_TO_ORDER_DETAIL && |
| 118 |
$order !== null && |
| 119 |
wp_safe_redirect( ModuleHelper::getOrderDetailUrl( (int) $order->getNumber() ) ) |
| 120 |
) { |
| 121 |
exit; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Gets order ID. |
| 127 |
* |
| 128 |
* @return int|null |
| 129 |
*/ |
| 130 |
private function getOrderId(): ?int { |
| 131 |
$orderId = $this->request->getQuery( self::PARAM_ORDER_ID ); |
| 132 |
if ( is_numeric( $orderId ) ) { |
| 133 |
return (int) $orderId; |
| 134 |
} |
| 135 |
|
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Gets order. |
| 141 |
* |
| 142 |
* @return Entity\Order|null |
| 143 |
*/ |
| 144 |
public function getOrder(): ?Entity\Order { |
| 145 |
$orderId = $this->getOrderId(); |
| 146 |
if ( $orderId !== null ) { |
| 147 |
return $this->orderRepository->getByIdWithValidCarrier( $orderId ); |
| 148 |
} |
| 149 |
|
| 150 |
return null; |
| 151 |
} |
| 152 |
} |
| 153 |
|