Completed
Pull Request — experimental/sf (#3402)
by Kiyotaka
45:23 queued 04:32
created

EditController::index()   F

Complexity

Conditions 21
Paths 397

Size

Total Lines 186

Duplication

Lines 6
Ratio 3.23 %

Code Coverage

Tests 94
CRAP Score 21.5076

Importance

Changes 0
Metric Value
cc 21
nc 397
nop 2
dl 6
loc 186
ccs 94
cts 105
cp 0.8952
crap 21.5076
rs 0.6966
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Order;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Criteria;
18
use Eccube\Controller\AbstractController;
19
use Eccube\Entity\Customer;
20
use Eccube\Entity\Master\CustomerStatus;
21
use Eccube\Entity\Master\OrderItemType;
22
use Eccube\Entity\Order;
23
use Eccube\Entity\Shipping;
24
use Eccube\Event\EccubeEvents;
25
use Eccube\Event\EventArgs;
26
use Eccube\Form\Type\AddCartType;
27
use Eccube\Form\Type\Admin\OrderType;
28
use Eccube\Form\Type\Admin\SearchCustomerType;
29
use Eccube\Form\Type\Admin\SearchProductType;
30
use Eccube\Repository\CategoryRepository;
31
use Eccube\Repository\CustomerRepository;
32
use Eccube\Repository\DeliveryRepository;
33
use Eccube\Repository\Master\DeviceTypeRepository;
34
use Eccube\Repository\Master\OrderItemTypeRepository;
35
use Eccube\Repository\Master\OrderStatusRepository;
36
use Eccube\Repository\OrderRepository;
37
use Eccube\Repository\ProductRepository;
38
use Eccube\Service\OrderStateMachine;
39
use Eccube\Service\PurchaseFlow\Processor\OrderNoProcessor;
40
use Eccube\Service\PurchaseFlow\PurchaseContext;
41
use Eccube\Service\PurchaseFlow\PurchaseException;
42
use Eccube\Service\PurchaseFlow\PurchaseFlow;
43
use Eccube\Service\TaxRuleService;
44
use Knp\Component\Pager\Paginator;
45
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
46
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
47
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
48
use Symfony\Component\HttpFoundation\Request;
49
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
50
use Symfony\Component\Serializer\Serializer;
51
use Symfony\Component\Serializer\SerializerInterface;
52
53
class EditController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
54
{
55
    /**
56
     * @var TaxRuleService
57
     */
58
    protected $taxRuleService;
59
60
    /**
61
     * @var DeviceTypeRepository
62
     */
63
    protected $deviceTypeRepository;
64
65
    /**
66
     * @var ProductRepository
67
     */
68
    protected $productRepository;
69
70
    /**
71
     * @var CategoryRepository
72
     */
73
    protected $categoryRepository;
74
75
    /**
76
     * @var CustomerRepository
77
     */
78
    protected $customerRepository;
79
80
    /**
81
     * @var Serializer
82
     */
83
    protected $serializer;
84
85
    /**
86
     * @var DeliveryRepository
87
     */
88
    protected $deliveryRepository;
89
90
    /**
91
     * @var PurchaseFlow
92
     */
93
    protected $purchaseFlow;
94
95
    /**
96
     * @var OrderRepository
97
     */
98
    protected $orderRepository;
99
100
    /**
101
     * @var OrderNoProcessor
102
     */
103
    protected $orderNoProcessor;
104
105
    /**
106
     * @var OrderItemTypeRepository
107
     */
108
    protected $orderItemTypeRepository;
109
110
    /**
111
     * @var OrderStateMachine
112
     */
113
    protected $orderStateMachine;
114
115
    /**
116
     * @var OrderStatusRepository
117
     */
118
    protected $orderStatusRepository;
119
120
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$orderItemTypeRepository" missing
Loading history...
introduced by
Doc comment for parameter "$orderStatusRepository" missing
Loading history...
introduced by
Doc comment for parameter "$orderStateMachine" missing
Loading history...
121
     * EditController constructor.
122
     *
123
     * @param TaxRuleService $taxRuleService
0 ignored issues
show
introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
124
     * @param DeviceTypeRepository $deviceTypeRepository
125
     * @param ProductRepository $productRepository
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
126
     * @param CategoryRepository $categoryRepository
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
127
     * @param CustomerRepository $customerRepository
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
128
     * @param SerializerInterface $serializer
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
129
     * @param DeliveryRepository $deliveryRepository
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
130
     * @param PurchaseFlow $orderPurchaseFlow
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
131
     * @param OrderRepository $orderRepository
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
132
     * @param OrderNoProcessor $orderNoProcessor
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
133
     */
134 10
    public function __construct(
135
        TaxRuleService $taxRuleService,
136
        DeviceTypeRepository $deviceTypeRepository,
137
        ProductRepository $productRepository,
138
        CategoryRepository $categoryRepository,
139
        CustomerRepository $customerRepository,
140
        SerializerInterface $serializer,
141
        DeliveryRepository $deliveryRepository,
142
        PurchaseFlow $orderPurchaseFlow,
143
        OrderRepository $orderRepository,
144
        OrderNoProcessor $orderNoProcessor,
145
        OrderItemTypeRepository $orderItemTypeRepository,
146
        OrderStatusRepository $orderStatusRepository,
147
        OrderStateMachine $orderStateMachine
148
    ) {
149 10
        $this->taxRuleService = $taxRuleService;
150 10
        $this->deviceTypeRepository = $deviceTypeRepository;
151 10
        $this->productRepository = $productRepository;
152 10
        $this->categoryRepository = $categoryRepository;
153 10
        $this->customerRepository = $customerRepository;
154 10
        $this->serializer = $serializer;
0 ignored issues
show
Documentation Bug introduced by
$serializer is of type object<Symfony\Component...er\SerializerInterface>, but the property $serializer was declared to be of type object<Symfony\Component\Serializer\Serializer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
155 10
        $this->deliveryRepository = $deliveryRepository;
156 10
        $this->purchaseFlow = $orderPurchaseFlow;
157 10
        $this->orderRepository = $orderRepository;
158 10
        $this->orderNoProcessor = $orderNoProcessor;
159 10
        $this->orderItemTypeRepository = $orderItemTypeRepository;
160 10
        $this->orderStatusRepository = $orderStatusRepository;
161 10
        $this->orderStateMachine = $orderStateMachine;
162
    }
163
164
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
165
     * 受注登録/編集画面.
166
     *
167
     * @Route("/%eccube_admin_route%/order/new", name="admin_order_new")
168
     * @Route("/%eccube_admin_route%/order/{id}/edit", requirements={"id" = "\d+"}, name="admin_order_edit")
169
     * @Template("@admin/Order/edit.twig")
170
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
171 7
    public function index(Request $request, $id = null)
172
    {
173 7
        $TargetOrder = null;
0 ignored issues
show
Unused Code introduced by
$TargetOrder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174 7
        $OriginOrder = null;
0 ignored issues
show
Unused Code introduced by
$OriginOrder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
176 7
        if (null === $id) {
177
            // 空のエンティティを作成.
178 3
            $TargetOrder = new Order();
179 3
            $TargetOrder->addShipping((new Shipping())->setOrder($TargetOrder));
180
        } else {
181 4
            $TargetOrder = $this->orderRepository->find($id);
182 4
            if (null === $TargetOrder) {
183
                throw new NotFoundHttpException();
184
            }
185
        }
186
187
        // 編集前の受注情報を保持
188 7
        $OriginOrder = clone $TargetOrder;
189 7
        $OriginItems = new ArrayCollection();
190 7
        foreach ($TargetOrder->getOrderItems() as $Item) {
191 4
            $OriginItems->add($Item);
192
        }
193
194 7
        $builder = $this->formFactory->createBuilder(OrderType::class, $TargetOrder);
195
196 7
        $event = new EventArgs(
197
            [
198 7
                'builder' => $builder,
199 7
                'OriginOrder' => $OriginOrder,
200 7
                'TargetOrder' => $TargetOrder,
201
            ],
202 7
            $request
203
        );
204 7
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event);
205
206 7
        $form = $builder->getForm();
207
208 7
        $form->handleRequest($request);
209 7
        $purchaseContext = new PurchaseContext($OriginOrder, $OriginOrder->getCustomer());
210
211 7
        if ($form->isSubmitted() && $form['OrderItems']->isValid()) {
212 5
            $event = new EventArgs(
213
                [
214 5
                    'builder' => $builder,
215 5
                    'OriginOrder' => $OriginOrder,
216 5
                    'TargetOrder' => $TargetOrder,
217 5
                    'PurchaseContext' => $purchaseContext,
218
                ],
219 5
                $request
220
            );
221 5
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event);
222
223 5
            $flowResult = $this->purchaseFlow->validate($TargetOrder, $purchaseContext);
224
225 5
            if ($flowResult->hasWarning()) {
226
                foreach ($flowResult->getWarning() as $warning) {
227
                    $this->addWarning($warning->getMessage(), 'admin');
228
                }
229
            }
230
231 5
            if ($flowResult->hasError()) {
232
                foreach ($flowResult->getErrors() as $error) {
233
                    $this->addError($error->getMessage(), 'admin');
234
                }
235
            }
236
237
            // 登録ボタン押下
238 5
            switch ($request->get('mode')) {
239
                case 'register':
240 5
                    log_info('受注登録開始', [$TargetOrder->getId()]);
241
242 5
                    if (!$flowResult->hasError() && $form->isValid()) {
243
                        try {
244 5
                            $this->purchaseFlow->prepare($TargetOrder, $purchaseContext);
245 5
                            $this->purchaseFlow->commit($TargetOrder, $purchaseContext);
246
                        } catch (PurchaseException $e) {
247
                            $this->addError($e->getMessage(), 'admin');
248
                            break;
249
                        }
250
251
                        // ステータスが変更されている場合はステートマシンを実行.
252 5
                        $OldStatus = $OriginOrder->getOrderStatus();
253 5
                        $NewStatus = $TargetOrder->getOrderStatus();
254
255 5
                        if ($TargetOrder->getId() && $OldStatus->getId() != $NewStatus->getId()) {
256
                            // ステートマシンでステータスは更新されるので, 古いステータスに戻す.
257 3
                            $TargetOrder->setOrderStatus($OldStatus);
258
                            // FormTypeでステータスの遷移チェックは行っているのでapplyのみ実行.
259 3
                            $this->orderStateMachine->apply($TargetOrder, $NewStatus);
260
                        }
261
262 5
                        $this->entityManager->persist($TargetOrder);
263 5
                        $this->entityManager->flush();
264
265 5
                        foreach ($OriginItems as $Item) {
266 3
                            if ($TargetOrder->getOrderItems()->contains($Item) === false) {
267 3
                                $this->entityManager->remove($Item);
268
                            }
269
                        }
270 5
                        $this->entityManager->flush();
271
272
                        // 新規登録時はMySQL対応のためflushしてから採番
273 5
                        $this->orderNoProcessor->process($TargetOrder, $purchaseContext);
274 5
                        $this->entityManager->flush();
275
276
                        // 会員の場合、購入回数、購入金額などを更新
277 5
                        if ($Customer = $TargetOrder->getCustomer()) {
278 5
                            $this->orderRepository->updateOrderSummary($Customer);
279 5
                            $this->entityManager->flush($Customer);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $Customer.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
280
                        }
281
282 5
                        $event = new EventArgs(
283
                            [
284 5
                                'form' => $form,
285 5
                                'OriginOrder' => $OriginOrder,
286 5
                                'TargetOrder' => $TargetOrder,
287 5
                                'Customer' => $Customer,
288
                            ],
289 5
                            $request
290
                        );
291 5
                        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event);
292
293 5
                        $this->addSuccess('admin.order.save.complete', 'admin');
294
295 5
                        log_info('受注登録完了', [$TargetOrder->getId()]);
296
297 5
                        return $this->redirectToRoute('admin_order_edit', ['id' => $TargetOrder->getId()]);
298
                    }
299
300
                    break;
301
                default:
302
                    break;
303
            }
304
        }
305
306
        // 会員検索フォーム
307 2
        $builder = $this->formFactory
308 2
            ->createBuilder(SearchCustomerType::class);
309
310 2
        $event = new EventArgs(
311
            [
312 2
                'builder' => $builder,
313 2
                'OriginOrder' => $OriginOrder,
314 2
                'TargetOrder' => $TargetOrder,
315
            ],
316 2
            $request
317
        );
318 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event);
319
320 2
        $searchCustomerModalForm = $builder->getForm();
321
322
        // 商品検索フォーム
323 2
        $builder = $this->formFactory
324 2
            ->createBuilder(SearchProductType::class);
325
326 2
        $event = new EventArgs(
327
            [
328 2
                'builder' => $builder,
329 2
                'OriginOrder' => $OriginOrder,
330 2
                'TargetOrder' => $TargetOrder,
331
            ],
332 2
            $request
333
        );
334 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event);
335
336 2
        $searchProductModalForm = $builder->getForm();
337
338
        // 配送業者のお届け時間
339 2
        $times = [];
340 2
        $deliveries = $this->deliveryRepository->findAll();
341 2 View Code Duplication
        foreach ($deliveries as $Delivery) {
342 2
            $deliveryTiems = $Delivery->getDeliveryTimes();
343 2
            foreach ($deliveryTiems as $DeliveryTime) {
344 2
                $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime();
345
            }
346
        }
347
348
        return [
349 2
            'form' => $form->createView(),
350 2
            'searchCustomerModalForm' => $searchCustomerModalForm->createView(),
351 2
            'searchProductModalForm' => $searchProductModalForm->createView(),
352 2
            'Order' => $TargetOrder,
353 2
            'id' => $id,
354 2
            'shippingDeliveryTimes' => $this->serializer->serialize($times, 'json'),
355
        ];
356
    }
357
358
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$paginator" missing
Loading history...
359
     * 顧客情報を検索する.
360
     *
361
     * @Route("/%eccube_admin_route%/order/search/customer/html", name="admin_order_search_customer_html")
362
     * @Route("/%eccube_admin_route%/order/search/customer/html/page/{page_no}", requirements={"page_No" = "\d+"}, name="admin_order_search_customer_html_page")
363
     * @Template("@admin/Order/search_customer.twig")
364
     *
365
     * @param Request $request
366
     * @param integer $page_no
367
     *
368
     * @return \Symfony\Component\HttpFoundation\JsonResponse
369
     */
370 1
    public function searchCustomerHtml(Request $request, $page_no = null, Paginator $paginator)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
371
    {
372 1
        if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
373 1
            log_debug('search customer start.');
374 1
            $page_count = $this->eccubeConfig['eccube_default_page_count'];
375 1
            $session = $this->session;
376
377 1
            if ('POST' === $request->getMethod()) {
378 1
                $page_no = 1;
379
380
                $searchData = [
381 1
                    'multi' => $request->get('search_word'),
382
                    'customer_status' => [
383 1
                        CustomerStatus::REGULAR,
384
                    ],
385
                ];
386
387 1
                $session->set('eccube.admin.order.customer.search', $searchData);
388 1
                $session->set('eccube.admin.order.customer.search.page_no', $page_no);
389
            } else {
390
                $searchData = (array) $session->get('eccube.admin.order.customer.search');
391
                if (is_null($page_no)) {
392
                    $page_no = intval($session->get('eccube.admin.order.customer.search.page_no'));
393
                } else {
394
                    $session->set('eccube.admin.order.customer.search.page_no', $page_no);
395
                }
396
            }
397
398 1
            $qb = $this->customerRepository->getQueryBuilderBySearchData($searchData);
399
400 1
            $event = new EventArgs(
401
                [
402 1
                    'qb' => $qb,
403 1
                    'data' => $searchData,
404
                ],
405 1
                $request
406
            );
407 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_SEARCH, $event);
408
409
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
410 1
            $pagination = $paginator->paginate(
411 1
                $qb,
412 1
                $page_no,
413 1
                $page_count,
414 1
                ['wrap-queries' => true]
415
            );
416
417
            /** @var $Customers \Eccube\Entity\Customer[] */
418 1
            $Customers = $pagination->getItems();
419
420 1
            if (empty($Customers)) {
421
                log_debug('search customer not found.');
422
            }
423
424 1
            $data = [];
425 1
            $formatName = '%s%s(%s%s)';
426 1
            foreach ($Customers as $Customer) {
427 1
                $data[] = [
428 1
                    'id' => $Customer->getId(),
429 1
                    'name' => sprintf($formatName, $Customer->getName01(), $Customer->getName02(),
430 1
                        $Customer->getKana01(),
431 1
                        $Customer->getKana02()),
432 1
                    'phone_number' => $Customer->getPhoneNumber(),
433 1
                    'email' => $Customer->getEmail(),
434
                ];
435
            }
436
437 1
            $event = new EventArgs(
438
                [
439 1
                    'data' => $data,
440 1
                    'Customers' => $pagination,
441
                ],
442 1
                $request
443
            );
444 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_COMPLETE, $event);
445 1
            $data = $event->getArgument('data');
446
447
            return [
448 1
                'data' => $data,
449 1
                'pagination' => $pagination,
450
            ];
451
        }
452
    }
453
454
    /**
455
     * 顧客情報を検索する.
456
     *
457
     * @Method("POST")
458
     * @Route("/%eccube_admin_route%/order/search/customer/id", name="admin_order_search_customer_by_id")
459
     *
460
     * @param Request $request
461
     *
462
     * @return \Symfony\Component\HttpFoundation\JsonResponse
463
     */
464 1
    public function searchCustomerById(Request $request)
465
    {
466 1
        if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
467 1
            log_debug('search customer by id start.');
468
469
            /** @var $Customer \Eccube\Entity\Customer */
470 1
            $Customer = $this->customerRepository
471 1
                ->find($request->get('id'));
472
473 1
            $event = new EventArgs(
474
                [
475 1
                    'Customer' => $Customer,
476
                ],
477 1
                $request
478
            );
479 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_INITIALIZE, $event);
480
481 1
            if (is_null($Customer)) {
482
                log_debug('search customer by id not found.');
483
484
                return $this->json([], 404);
485
            }
486
487 1
            log_debug('search customer by id found.');
488
489
            $data = [
490 1
                'id' => $Customer->getId(),
491 1
                'name01' => $Customer->getName01(),
492 1
                'name02' => $Customer->getName02(),
493 1
                'kana01' => $Customer->getKana01(),
494 1
                'kana02' => $Customer->getKana02(),
495 1
                'postal_code' => $Customer->getPostalCode(),
496 1
                'pref' => is_null($Customer->getPref()) ? null : $Customer->getPref()->getId(),
497 1
                'addr01' => $Customer->getAddr01(),
498 1
                'addr02' => $Customer->getAddr02(),
499 1
                'email' => $Customer->getEmail(),
500 1
                'phone_number' => $Customer->getPhoneNumber(),
501 1
                'company_name' => $Customer->getCompanyName(),
502
            ];
503
504 1
            $event = new EventArgs(
505
                [
506 1
                    'data' => $data,
507 1
                    'Customer' => $Customer,
508
                ],
509 1
                $request
510
            );
511 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE, $event);
512 1
            $data = $event->getArgument('data');
513
514 1
            return $this->json($data);
515
        }
516
    }
517
518
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$page_no" missing
Loading history...
introduced by
Doc comment for parameter "$paginator" missing
Loading history...
519
     * @Route("/%eccube_admin_route%/order/search/product", name="admin_order_search_product")
520
     * @Route("/%eccube_admin_route%/order/search/product/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_search_product_page")
521
     * @Template("@admin/Order/search_product.twig")
522
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
523 1
    public function searchProduct(Request $request, $page_no = null, Paginator $paginator)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
524
    {
525 1
        if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
526 1
            log_debug('search product start.');
527 1
            $page_count = $this->eccubeConfig['eccube_default_page_count'];
528 1
            $session = $this->session;
529
530 1
            if ('POST' === $request->getMethod()) {
531 1
                $page_no = 1;
532
533
                $searchData = [
534 1
                    'id' => $request->get('id'),
535
                ];
536
537 1
                if ($categoryId = $request->get('category_id')) {
538
                    $Category = $this->categoryRepository->find($categoryId);
539
                    $searchData['category_id'] = $Category;
540
                }
541
542 1
                $session->set('eccube.admin.order.product.search', $searchData);
543 1
                $session->set('eccube.admin.order.product.search.page_no', $page_no);
544
            } else {
545
                $searchData = (array) $session->get('eccube.admin.order.product.search');
546
                if (is_null($page_no)) {
547
                    $page_no = intval($session->get('eccube.admin.order.product.search.page_no'));
548
                } else {
549
                    $session->set('eccube.admin.order.product.search.page_no', $page_no);
550
                }
551
            }
552
553 1
            $qb = $this->productRepository
554 1
                ->getQueryBuilderBySearchDataForAdmin($searchData);
555
556 1
            $event = new EventArgs(
557
                [
558 1
                    'qb' => $qb,
559 1
                    'searchData' => $searchData,
560
                ],
561 1
                $request
562
            );
563 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_SEARCH, $event);
564
565
            /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
566 1
            $pagination = $paginator->paginate(
567 1
                $qb,
568 1
                $page_no,
569 1
                $page_count,
570 1
                ['wrap-queries' => true]
571
            );
572
573
            /** @var $Products \Eccube\Entity\Product[] */
574 1
            $Products = $pagination->getItems();
575
576 1
            if (empty($Products)) {
577
                log_debug('search product not found.');
578
            }
579
580 1
            $forms = [];
581 1 View Code Duplication
            foreach ($Products as $Product) {
582
                /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
583 1
                $builder = $this->formFactory->createNamedBuilder('', AddCartType::class, null, [
584 1
                    'product' => $Product,
585
                ]);
586 1
                $addCartForm = $builder->getForm();
587 1
                $forms[$Product->getId()] = $addCartForm->createView();
588
            }
589
590 1
            $event = new EventArgs(
591
                [
592 1
                    'forms' => $forms,
593 1
                    'Products' => $Products,
594 1
                    'pagination' => $pagination,
595
                ],
596 1
                $request
597
            );
598 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE, $event);
599
600
            return [
601 1
                'forms' => $forms,
602 1
                'Products' => $Products,
603 1
                'pagination' => $pagination,
604
            ];
605
        }
606
    }
607
608
    /**
609
     * その他明細情報を取得
610
     *
611
     * @Route("/%eccube_admin_route%/order/search/order_item_type", name="admin_order_search_order_item_type")
612
     * @Template("@admin/Order/order_item_type.twig")
613
     *
614
     * @param Request $request
615
     *
616
     * @return array
617
     */
618
    public function searchOrderItemType(Request $request)
619
    {
620
        if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
621
            log_debug('search order item type start.');
622
623
            $criteria = Criteria::create();
624
            $criteria
625
                ->where($criteria->expr()->andX(
626
                    $criteria->expr()->neq('id', OrderItemType::PRODUCT),
627
                    $criteria->expr()->neq('id', OrderItemType::TAX)
628
                ))
629
                ->orderBy(['sort_no' => 'ASC']);
630
631
            $OrderItemTypes = $this->orderItemTypeRepository->matching($criteria);
632
633
            $forms = [];
634
            foreach ($OrderItemTypes as $OrderItemType) {
635
                /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
636
                $builder = $this->formFactory->createBuilder();
637
                $form = $builder->getForm();
638
                $forms[$OrderItemType->getId()] = $form->createView();
639
            }
640
641
            return [
642
                'forms' => $forms,
643
                'OrderItemTypes' => $OrderItemTypes,
644
            ];
645
        }
646
    }
647
}
648