Failed Conditions
Push — experimental/3.1 ( 0c67c0...62371a )
by chihiro
51:52
created

ShippingController::index()   D

Complexity

Conditions 17
Paths 197

Size

Total Lines 145
Code Lines 93

Duplication

Lines 145
Ratio 100 %

Importance

Changes 0
Metric Value
cc 17
eloc 93
nc 197
nop 3
dl 145
loc 145
rs 4.5364
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
namespace Eccube\Controller\Admin\Shipping;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Eccube\Application;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\HttpFoundation\Request;
12
13
use Eccube\Common\Constant;
14
use Eccube\Controller\AbstractController;
15
use Eccube\Entity\Master\CsvType;
16
use Eccube\Event\EccubeEvents;
17
use Eccube\Event\EventArgs;
18
use Eccube\Form\Type\AddCartType;
19
use Eccube\Form\Type\Admin\SearchOrderType;
20
use Eccube\Form\Type\Admin\ShippingType;
21
use Eccube\Form\Type\Admin\SearchCustomerType;
22
use Eccube\Form\Type\Admin\SearchProductType;
23
use Eccube\Form\Type\Admin\ShipmentItemType;
24
use Symfony\Component\HttpFoundation\StreamedResponse;
25
26
/**
27
 * // FIXME UrlGenerator で {_admin} を認識しない問題あり
28
 * @Route("/admin/shipping")
29
 */
30
class ShippingController
31
{
32
    /**
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...
33
     * @Route("/", name="admin/shipping")
34
     * @Route("/page/{page_no}", name="admin/shipping/page")
35
     *
36
     * @Security("has_role('ROLE_ADMIN')")
37
     * @Template("shipping/index.twig")
38
     *
39
     * @param Application $app
40
     * @return \Symfony\Component\HttpFoundation\Response
41
     */
42 View Code Duplication
    public function index(Application $app, Request $request, $page_no = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $session = $request->getSession();
45
46
        $builder = $app['form.factory']
47
            ->createBuilder(SearchOrderType::class);
48
49
        $event = new EventArgs(
50
            array(
51
                'builder' => $builder,
52
            ),
53
            $request
54
        );
55
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event);
56
57
        $searchForm = $builder->getForm();
58
59
        $pagination = array();
60
61
        $disps = $app['eccube.repository.master.disp']->findAll();
62
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
63
        $page_count = $app['config']['default_page_count'];
64
        $page_status = null;
65
        $active = false;
66
67
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
68
69
            $searchForm->handleRequest($request);
70
71
            if ($searchForm->isValid()) {
72
                $searchData = $searchForm->getData();
73
74
                // paginator
75
                $qb = $app['eccube.repository.shipping']->getQueryBuilderBySearchDataForAdmin($searchData);
76
77
                $event = new EventArgs(
78
                    array(
79
                        'form' => $searchForm,
80
                        'qb' => $qb,
81
                    ),
82
                    $request
83
                );
84
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
85
86
                $page_no = 1;
87
                $pagination = $app['paginator']()->paginate(
88
                    $qb,
89
                    $page_no,
90
                    $page_count
91
                );
92
93
                // sessionのデータ保持
94
                $session->set('eccube.admin.shipping.search', $searchData);
95
                $session->set('eccube.admin.shipping.search.page_no', $page_no);
96
            }
97
        } else {
98
            if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) {
99
                // sessionを削除
100
                $session->remove('eccube.admin.shipping.search');
101
                $session->remove('eccube.admin.shipping.search.page_no');
102
            } else {
103
                // pagingなどの処理
104
                $searchData = $session->get('eccube.admin.shipping.search');
105
                if (is_null($page_no)) {
106
                    $page_no = intval($session->get('eccube.admin.shipping.search.page_no'));
107
                } else {
108
                    $session->set('eccube.admin.shipping.search.page_no', $page_no);
109
                }
110
111
                if (!is_null($searchData)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
112
113
                    // 公開ステータス
114
                    $status = $request->get('status');
115
                    if (!empty($status)) {
116
                        if ($status != $app['config']['admin_product_stock_status']) {
117
                            $searchData['status']->clear();
118
                            $searchData['status']->add($status);
119
                        } else {
120
                            $searchData['stock_status'] = $app['config']['disabled'];
121
                        }
122
                        $page_status = $status;
123
                    }
124
                    // 表示件数
125
                    $pcount = $request->get('page_count');
126
127
                    $page_count = empty($pcount) ? $page_count : $pcount;
128
129
                    $qb = $app['eccube.repository.shipping']->getQueryBuilderBySearchDataForAdmin($searchData);
130
131
                    $event = new EventArgs(
132
                        array(
133
                            'form' => $searchForm,
134
                            'qb' => $qb,
135
                        ),
136
                        $request
137
                    );
138
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
139
140
                    $pagination = $app['paginator']()->paginate(
141
                        $qb,
142
                        $page_no,
143
                        $page_count
144
                    );
145
146
                    // セッションから検索条件を復元
147
                    if (!empty($searchData['status'])) {
148
                        $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']);
149
                    }
150
                    if (count($searchData['multi_status']) > 0) {
151
                        $statusIds = array();
152
                        foreach ($searchData['multi_status'] as $Status) {
153
                            $statusIds[] = $Status->getId();
154
                        }
155
                        $searchData['multi_status'] = $app['eccube.repository.master.order_status']->findBy(array('id' => $statusIds));
156
                    }
157
                    if (count($searchData['sex']) > 0) {
158
                        $sex_ids = array();
159
                        foreach ($searchData['sex'] as $Sex) {
160
                            $sex_ids[] = $Sex->getId();
161
                        }
162
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
163
                    }
164
                    if (count($searchData['payment']) > 0) {
165
                        $payment_ids = array();
166
                        foreach ($searchData['payment'] as $Payment) {
167
                            $payment_ids[] = $Payment->getId();
168
                        }
169
                        $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids));
170
                    }
171
                    $searchForm->setData($searchData);
172
                }
173
            }
174
        }
175
176
        return [
177
            'searchForm' => $searchForm->createView(),
178
            'pagination' => $pagination,
179
            'disps' => $disps,
180
            'pageMaxis' => $pageMaxis,
181
            'page_no' => $page_no,
182
            'page_status' => $page_status,
183
            'page_count' => $page_count,
184
            'active' => $active,
185
        ];
186
    }
187
}
188