PluginProbe
Packeta / 1.6.3
Packeta v1.6.3
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / PacketActionsCommonLogic.php

PacketActionsCommonLogic.php in Packeta 1.6.3, at src/Packetery/Module/Order/PacketActionsCommonLogic.php

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