PluginProbe
Packeta / 1.2.4
Packeta v1.2.4
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 / Controller.php

Controller.php in Packeta 1.2.4, at src/Packetery/Module/Order/Controller.php

194 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Controller
4 *
5 * @package Packetery\Module\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Core\Helper;
13 use Packetery\Module\Calculator;
14 use Packetery\Module\Order;
15 use WP_Error;
16 use WP_REST_Controller;
17 use WP_REST_Request;
18 use WP_REST_Response;
19 use WP_REST_Server;
20
21 /**
22 * Class Controller
23 *
24 * @package Packetery\Module\Order
25 */
26 class Controller extends WP_REST_Controller {
27
28 public const PATH_SAVE_MODAL = '/save';
29 public const PATH_SUBMIT_TO_API = '/submit-to-api';
30
31 /**
32 * Order modal.
33 *
34 * @var Modal
35 */
36 private $orderModal;
37
38 /**
39 * Router.
40 *
41 * @var ControllerRouter
42 */
43 private $router;
44
45 /**
46 * Packet submitter.
47 *
48 * @var PacketSubmitter
49 */
50 private $packetSubmitter;
51
52 /**
53 * Order repository.
54 *
55 * @var Repository
56 */
57 private $orderRepository;
58
59 /**
60 * Calculator.
61 *
62 * @var Calculator
63 */
64 private $calculator;
65
66 /**
67 * Controller constructor.
68 *
69 * @param Modal $orderModal Modal.
70 * @param ControllerRouter $controllerRouter Router.
71 * @param PacketSubmitter $packetSubmitter Packet submitter.
72 * @param Order\Repository $orderRepository Order repository.
73 * @param Calculator $calculator Calculator.
74 */
75 public function __construct(
76 Modal $orderModal,
77 ControllerRouter $controllerRouter,
78 PacketSubmitter $packetSubmitter,
79 Order\Repository $orderRepository,
80 Calculator $calculator
81 ) {
82 $this->orderModal = $orderModal;
83 $this->router = $controllerRouter;
84 $this->namespace = $controllerRouter->getNamespace();
85 $this->rest_base = $controllerRouter->getRestBase();
86 $this->packetSubmitter = $packetSubmitter;
87 $this->orderRepository = $orderRepository;
88 $this->calculator = $calculator;
89 }
90
91 /**
92 * Register the routes of the controller.
93 *
94 * @return void
95 */
96 public function registerRoutes(): void {
97 $this->router->registerRoute(
98 self::PATH_SAVE_MODAL,
99 [
100 [
101 'methods' => WP_REST_Server::EDITABLE,
102 'callback' => [ $this, 'saveModal' ],
103 'permission_callback' => function () {
104 return current_user_can( 'edit_posts' );
105 },
106 ],
107 ]
108 );
109 $this->router->registerRoute(
110 self::PATH_SUBMIT_TO_API,
111 [
112 [
113 'methods' => WP_REST_Server::ALLMETHODS,
114 'callback' => [ $this, 'submitToApi' ],
115 'permission_callback' => function () {
116 return current_user_can( 'edit_posts' );
117 },
118 ],
119 ]
120 );
121 }
122
123 /**
124 * Submit packet to API.
125 *
126 * @param WP_REST_Request $request Full data about the request.
127 *
128 * @return WP_REST_Response|WP_Error
129 */
130 public function submitToApi( $request ) {
131 $data = [];
132 $parameters = $request->get_body_params();
133 $orderId = $parameters['orderId'];
134 $order = wc_get_order( $orderId );
135
136 $resultsCounter = [
137 'success' => 0,
138 'ignored' => 0,
139 'errors' => 0,
140 ];
141 $this->packetSubmitter->submitPacket( $order, $resultsCounter );
142 $data['redirectTo'] = add_query_arg(
143 [
144 'post_type' => 'shop_order',
145 'submit_to_api' => '1',
146 ] + $resultsCounter,
147 admin_url( 'edit.php' )
148 );
149
150 return new WP_REST_Response( $data, 200 );
151 }
152
153 /**
154 * Update one item from the collection
155 *
156 * @param WP_REST_Request $request Full data about the request.
157 *
158 * @return WP_REST_Response|WP_Error
159 */
160 public function saveModal( $request ) {
161 $data = [];
162 $parameters = $request->get_body_params();
163 $packeteryWeight = $parameters['packeteryWeight'];
164 $orderId = (int) $parameters['orderId'];
165
166 $form = $this->orderModal->createForm();
167 $form->setValues(
168 [
169 'packetery_weight' => $packeteryWeight,
170 ]
171 );
172
173 if ( false === $form->isValid() ) {
174 return new WP_Error( 'form_invalid', implode( ', ', $form->getErrors() ), 400 );
175 }
176
177 $values = $form->getValues( 'array' );
178 if ( ! is_numeric( $values['packetery_weight'] ) ) {
179 $values['packetery_weight'] = $this->calculator->calculateOrderWeight( wc_get_order( $orderId ) );
180 }
181
182 $order = $this->orderRepository->getById( $orderId );
183 $order->setWeight( Helper::simplifyWeight( $values['packetery_weight'] ) );
184 $this->orderRepository->save( $order );
185
186 $data['message'] = __( 'Success', 'packetery' );
187 $data['data'] = [
188 'packetery_weight' => $values['packetery_weight'],
189 ];
190
191 return new WP_REST_Response( $data, 200 );
192 }
193 }
194