Failed Conditions
Push — experimental/sf ( 267a6e...28d741 )
by Ryo
1408:20 queued 1400:20
created

ShippingController::index()   C

Complexity

Conditions 9
Paths 20

Size

Total Lines 116

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 9.0329

Importance

Changes 0
Metric Value
cc 9
nc 20
nop 3
dl 0
loc 116
rs 6.4444
c 0
b 0
f 0
ccs 50
cts 54
cp 0.9259
crap 9.0329

How to fix   Long Method   

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\Shipping;
15
16
use Eccube\Common\Constant;
17
use Eccube\Controller\AbstractController;
18
use Eccube\Entity\Shipping;
19
use Eccube\Entity\OrderItem;
20
use Eccube\Event\EccubeEvents;
21
use Eccube\Event\EventArgs;
22
use Eccube\Form\Type\Admin\SearchShippingType;
23
use Eccube\Repository\Master\PageMaxRepository;
24
use Eccube\Repository\ShippingRepository;
25
use Eccube\Service\MailService;
26
use Eccube\Util\FormUtil;
27
use Knp\Component\Pager\PaginatorInterface;
28
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
31
use Symfony\Component\HttpFoundation\JsonResponse;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpFoundation\Response;
34
35
class ShippingController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
    /**
38
     * @var ShippingRepository
39
     */
40
    protected $shippingRepository;
41
42
    /**
43
     * @var PageMaxRepository
44
     */
45
    protected $pageMaxRepository;
46
47
    /**
48
     * @var MailService
49
     */
50
    protected $mailService;
51
52
    /**
53
     * ShippingController constructor.
54
     *
55
     * @param ShippingRepository $shippingRepository
56
     * @param PageMaxRepository $pageMaxRepository
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
57
     * @param MailService $mailService
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
58
     */
59 10
    public function __construct(
60
        ShippingRepository $shippingRepository,
61
        PageMaxRepository $pageMaxRepository,
62
        MailService $mailService
63
    ) {
64 10
        $this->shippingRepository = $shippingRepository;
65 10
        $this->pageMaxRepository = $pageMaxRepository;
66 10
        $this->mailService = $mailService;
67
    }
68
69
    /**
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...
70
     * @Route("/%eccube_admin_route%/shipping", name="admin_shipping")
71
     * @Route("/%eccube_admin_route%/shipping/page/{page_no}", name="admin_shipping_page")
72
     * @Template("@admin/Shipping/index.twig")
73
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
74 4
    public function index(Request $request, $page_no = null, PaginatorInterface $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...
75
    {
76 4
        $builder = $this->formFactory
77 4
            ->createBuilder(SearchShippingType::class);
78
79 4
        $event = new EventArgs(
80
            [
81 4
                'builder' => $builder,
82
            ],
83 4
            $request
84
        );
85 4
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SHIPPING_INDEX_INITIALIZE, $event);
86
87 4
        $searchForm = $builder->getForm();
88
89
        /**
90
         * ページの表示件数は, 以下の順に優先される.
91
         * - リクエストパラメータ
92
         * - セッション
93
         * - デフォルト値
94
         * また, セッションに保存する際は mtb_page_maxと照合し, 一致した場合のみ保存する.
95
         **/
96 4
        $page_count = $this->session->get('eccube.admin.shipping.search.page_count',
97 4
            $this->eccubeConfig->get('eccube_default_page_count'));
98
99 4
        $page_count_param = (int) $request->get('page_count');
100 4
        $pageMaxis = $this->pageMaxRepository->findAll();
101
102 4
        if ($page_count_param) {
103 1
            foreach ($pageMaxis as $pageMax) {
104 1
                if ($page_count_param == $pageMax->getName()) {
105 1
                    $page_count = $pageMax->getName();
106 1
                    $this->session->set('eccube.admin.shipping.search.page_count', $page_count);
107 1
                    break;
108
                }
109
            }
110
        }
111
112 4
        if ('POST' === $request->getMethod()) {
113 2
            $searchForm->handleRequest($request);
114
115 2
            if ($searchForm->isValid()) {
116
                /**
117
                 * 検索が実行された場合は, セッションに検索条件を保存する.
118
                 * ページ番号は最初のページ番号に初期化する.
119
                 */
120 2
                $page_no = 1;
121 2
                $searchData = $searchForm->getData();
122
123
                // 検索条件, ページ番号をセッションに保持.
124 2
                $this->session->set('eccube.admin.shipping.search', FormUtil::getViewData($searchForm));
125 2
                $this->session->set('eccube.admin.shipping.search.page_no', $page_no);
126
            } else {
127
                return [
128 2
                    'searchForm' => $searchForm->createView(),
129
                    'pagination' => [],
130
                    'pageMaxis' => $pageMaxis,
131
                    'page_no' => $page_no,
132
                    'page_count' => $page_count,
133
                    'has_errors' => true,
134
                ];
135
            }
136
        } else {
137 3
            if (null !== $page_no || $request->get('resume')) {
138
                /*
139
                 * ページ送りの場合または、他画面から戻ってきた場合は, セッションから検索条件を復旧する.
140
                 */
141 1
                if ($page_no) {
142
                    // ページ送りで遷移した場合.
143 1
                    $this->session->set('eccube.admin.shipping.search.page_no', (int) $page_no);
144
                } else {
145
                    // 他画面から遷移した場合.
146
                    $page_no = $this->session->get('eccube.admin.shipping.search.page_no', 1);
147
                }
148 1
                $viewData = $this->session->get('eccube.admin.shipping.search', []);
149 1
                $searchData = FormUtil::submitAndGetData($searchForm, $viewData);
150
            } else {
151
                /**
152
                 * 初期表示の場合.
153
                 */
154 2
                $page_no = 1;
155 2
                $searchData = [];
156
157
                // セッション中の検索条件, ページ番号を初期化.
158 2
                $this->session->set('eccube.admin.shipping.search', $searchData);
159 2
                $this->session->set('eccube.admin.shipping.search.page_no', $page_no);
160
            }
161
        }
162
163 4
        $qb = $this->shippingRepository->getQueryBuilderBySearchDataForAdmin($searchData);
164
165 4
        $event = new EventArgs(
166
            [
167 4
                'qb' => $qb,
168 4
                'searchData' => $searchData,
169
            ],
170 4
            $request
171
        );
172
173 4
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SHIPPING_INDEX_SEARCH, $event);
174
175 4
        $pagination = $paginator->paginate(
176 4
            $qb,
177 4
            $page_no,
178 4
            $page_count
179
        );
180
181
        return [
182 4
            'searchForm' => $searchForm->createView(),
183 4
            'pagination' => $pagination,
184 4
            'pageMaxis' => $pageMaxis,
185 4
            'page_no' => $page_no,
186 4
            'page_count' => $page_count,
187
            'has_errors' => false,
188
        ];
189
    }
190
191
    /**
192
     * 出荷済み処理
193
     * 未出荷の出荷のみ出荷処理をする
194
     * 出荷処理をした場合でリクエストで 'notificationMail' が送信されていた場合のみ出荷完了メールを送信する
195
     *
196
     * @Method("PUT")
197
     * @Route("/%eccube_admin_route%/shipping/mark_as_shipped/{id}", requirements={"id" = "\d+"}, name="admin_shipping_mark_as_shipped")
198
     *
199
     * @param Request $request
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
200
     * @param Shipping $Shipping
201
     *
202
     * @return JsonResponse
203
     *
204
     * @throws \Twig_Error
205
     */
206 3
    public function markAsShipped(Request $request, Shipping $Shipping)
207
    {
208 3
        $this->isTokenValid();
209
210 3
        $result = [];
211 3
        if ($Shipping->isShipped() == false) {
212 2
            $Shipping->setShippingDate(new \DateTime());
213 2
            $this->shippingRepository->save($Shipping);
214
215 2
            if ($request->get('notificationMail')) {
216 1
                $this->mailService->sendShippingNotifyMail($Shipping);
217 1
                $result['mail'] = true;
218
            } else {
219 1
                $result['mail'] = false;
220
            }
221
222 2
            $this->entityManager->flush();
223 2
            $result['shipped'] = true;
224
225 2
            return $this->json($result);
226
        }
227
228 1
        return $this->json([
229 1
            'shipped' => false,
230
            'mail' => false,
231
        ]);
232
    }
233
234
    /**
235
     * @Route("/%eccube_admin_route%/shipping/preview_notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_preview_notify_mail")
236
     *
237
     * @param Shipping $Shipping
238
     *
239
     * @return Response
240
     *
241
     * @throws \Twig_Error
242
     */
243
    public function previewShippingNotifyMail(Shipping $Shipping)
244
    {
245
        return new Response($this->mailService->getShippingNotifyMailBody($Shipping, $Shipping->getOrder()));
246
    }
247
248
    /**
249
     * @Method("PUT")
250
     * @Route("/%eccube_admin_route%/shipping/notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail")
251
     *
252
     * @param Shipping $Shipping
253
     *
254
     * @return JsonResponse
255
     *
256
     * @throws \Twig_Error
257
     */
258 2
    public function notifyMail(Shipping $Shipping)
259
    {
260 2
        $this->isTokenValid();
261
262 2
        $this->mailService->sendShippingNotifyMail($Shipping);
263
264 2
        $Shipping->setMailSendDate(new \DateTime());
265 2
        $this->shippingRepository->save($Shipping);
266 2
        $this->entityManager->flush();
267
268 2
        return $this->json([
269 2
            'mail' => true,
270
            'shipped' => false,
271
        ]);
272
    }
273
274
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
275
     * @Method("POST")
276
     * @Route("/%eccube_admin_route%/shipping/bulk_delete", name="admin_shipping_bulk_delete")
277
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
278 1
    public function bulkDelete(Request $request)
279
    {
280 1
        $this->isTokenValid();
281 1
        $ids = $request->get('ids');
282
283 1
        foreach ($ids as $shipping_id) {
284
            /** @var Shipping $Shipping */
285 1
            $Shipping = $this->shippingRepository->find($shipping_id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $Shipping is correct as $this->shippingRepository->find($shipping_id) (which targets Doctrine\ORM\EntityRepository::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
286 1
            if ($Shipping) {
287 1
                $OrderItems = $Shipping->getOrderItems();
288
                /** @var OrderItem $OrderItem */
289 1
                foreach ($OrderItems as $OrderItem) {
290 1
                    $OrderItem->setShipping(null);
291
                }
292 1
                $this->entityManager->remove($Shipping);
293 1
                log_info('出荷削除', [$Shipping->getId()]);
294
            }
295
        }
296 1
        $this->entityManager->flush();
297
298 1
        $this->addSuccess('admin.shipping.delete.complete', 'admin');
299
300 1
        return $this->redirect($this->generateUrl('admin_shipping', ['resume' => Constant::ENABLED]));
301
    }
302
}
303