Failed Conditions
Push — experimental/sf ( 069d05...1f2df0 )
by chihiro
157:56 queued 150:35
created

ShippingController::previewShippingNotifyMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
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\Service\OrderStateMachine;
27
use Eccube\Util\FormUtil;
28
use Knp\Component\Pager\PaginatorInterface;
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
31
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
32
use Symfony\Component\HttpFoundation\JsonResponse;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\Response;
35
36
class ShippingController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38
    /**
39
     * @var ShippingRepository
40
     */
41
    protected $shippingRepository;
42
43
    /**
44
     * @var PageMaxRepository
45
     */
46
    protected $pageMaxRepository;
47
48
    /**
49
     * @var MailService
50
     */
51
    protected $mailService;
52
53
    /**
54
     * @var OrderStateMachine
55
     */
56
    protected $orderStateMachine;
57
58
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$orderStateMachine" missing
Loading history...
59
     * ShippingController constructor.
60
     *
61
     * @param ShippingRepository $shippingRepository
62
     * @param PageMaxRepository $pageMaxRepository
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
63
     * @param MailService $mailService
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
64
     * @param OrderStateMachine $orderStateMachine;
0 ignored issues
show
Documentation introduced by
There is no parameter named $orderStateMachine;. Did you maybe mean $orderStateMachine?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Doc comment for parameter $orderStateMachine; does not match actual variable name $orderStateMachine
Loading history...
65
     */
66 3
    public function __construct(
67
        ShippingRepository $shippingRepository,
68
        PageMaxRepository $pageMaxRepository,
69
        MailService $mailService,
70
        OrderStateMachine $orderStateMachine
71
    ) {
72 3
        $this->shippingRepository = $shippingRepository;
73 3
        $this->pageMaxRepository = $pageMaxRepository;
74 3
        $this->mailService = $mailService;
75 3
        $this->orderStateMachine = $orderStateMachine;
76
    }
77
78
    /**
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...
79
     * @Route("/%eccube_admin_route%/shipping", name="admin_shipping")
80
     * @Route("/%eccube_admin_route%/shipping/page/{page_no}", name="admin_shipping_page")
81
     * @Template("@admin/Shipping/index.twig")
82
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
83
    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...
84
    {
85
        $builder = $this->formFactory
86
            ->createBuilder(SearchShippingType::class);
87
88
        $event = new EventArgs(
89
            [
90
                'builder' => $builder,
91
            ],
92
            $request
93
        );
94
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SHIPPING_INDEX_INITIALIZE, $event);
95
96
        $searchForm = $builder->getForm();
97
98
        /**
99
         * ページの表示件数は, 以下の順に優先される.
100
         * - リクエストパラメータ
101
         * - セッション
102
         * - デフォルト値
103
         * また, セッションに保存する際は mtb_page_maxと照合し, 一致した場合のみ保存する.
104
         **/
105
        $page_count = $this->session->get('eccube.admin.shipping.search.page_count',
106
            $this->eccubeConfig->get('eccube_default_page_count'));
107
108
        $page_count_param = (int) $request->get('page_count');
109
        $pageMaxis = $this->pageMaxRepository->findAll();
110
111
        if ($page_count_param) {
112
            foreach ($pageMaxis as $pageMax) {
113
                if ($page_count_param == $pageMax->getName()) {
114
                    $page_count = $pageMax->getName();
115
                    $this->session->set('eccube.admin.shipping.search.page_count', $page_count);
116
                    break;
117
                }
118
            }
119
        }
120
121
        if ('POST' === $request->getMethod()) {
122
            $searchForm->handleRequest($request);
123
124
            if ($searchForm->isValid()) {
125
                /**
126
                 * 検索が実行された場合は, セッションに検索条件を保存する.
127
                 * ページ番号は最初のページ番号に初期化する.
128
                 */
129
                $page_no = 1;
130
                $searchData = $searchForm->getData();
131
132
                // 検索条件, ページ番号をセッションに保持.
133
                $this->session->set('eccube.admin.shipping.search', FormUtil::getViewData($searchForm));
134
                $this->session->set('eccube.admin.shipping.search.page_no', $page_no);
135
            } else {
136
                return [
137
                    'searchForm' => $searchForm->createView(),
138
                    'pagination' => [],
139
                    'pageMaxis' => $pageMaxis,
140
                    'page_no' => $page_no,
141
                    'page_count' => $page_count,
142
                    'has_errors' => true,
143
                ];
144
            }
145
        } else {
146
            if (null !== $page_no || $request->get('resume')) {
147
                /*
148
                 * ページ送りの場合または、他画面から戻ってきた場合は, セッションから検索条件を復旧する.
149
                 */
150
                if ($page_no) {
151
                    // ページ送りで遷移した場合.
152
                    $this->session->set('eccube.admin.shipping.search.page_no', (int) $page_no);
153
                } else {
154
                    // 他画面から遷移した場合.
155
                    $page_no = $this->session->get('eccube.admin.shipping.search.page_no', 1);
156
                }
157
                $viewData = $this->session->get('eccube.admin.shipping.search', []);
158
                $searchData = FormUtil::submitAndGetData($searchForm, $viewData);
159
            } else {
160
                /**
161
                 * 初期表示の場合.
162
                 */
163
                $page_no = 1;
164
                $searchData = [];
165
166
                // セッション中の検索条件, ページ番号を初期化.
167
                $this->session->set('eccube.admin.shipping.search', $searchData);
168
                $this->session->set('eccube.admin.shipping.search.page_no', $page_no);
169
            }
170
        }
171
172
        $qb = $this->shippingRepository->getQueryBuilderBySearchDataForAdmin($searchData);
173
174
        $event = new EventArgs(
175
            [
176
                'qb' => $qb,
177
                'searchData' => $searchData,
178
            ],
179
            $request
180
        );
181
182
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SHIPPING_INDEX_SEARCH, $event);
183
184
        $pagination = $paginator->paginate(
185
            $qb,
186
            $page_no,
187
            $page_count
188
        );
189
190
        return [
191
            'searchForm' => $searchForm->createView(),
192
            'pagination' => $pagination,
193
            'pageMaxis' => $pageMaxis,
194
            'page_no' => $page_no,
195
            'page_count' => $page_count,
196
            'has_errors' => false,
197
        ];
198
    }
199
200
    /**
201
     * @Route("/%eccube_admin_route%/shipping/preview_notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_preview_notify_mail")
202
     *
203
     * @param Shipping $Shipping
204
     *
205
     * @return Response
206
     *
207
     * @throws \Twig_Error
208
     */
209
    public function previewShippingNotifyMail(Shipping $Shipping)
210
    {
211
        return new Response($this->mailService->getShippingNotifyMailBody($Shipping, $Shipping->getOrder()));
212
    }
213
214
    /**
215
     * @Method("PUT")
216
     * @Route("/%eccube_admin_route%/shipping/notify_mail/{id}", requirements={"id" = "\d+"}, name="admin_shipping_notify_mail")
217
     *
218
     * @param Shipping $Shipping
219
     *
220
     * @return JsonResponse
221
     *
222
     * @throws \Twig_Error
223
     */
224 2
    public function notifyMail(Shipping $Shipping)
225
    {
226 2
        $this->isTokenValid();
227
228 2
        $this->mailService->sendShippingNotifyMail($Shipping);
229
230 2
        $Shipping->setMailSendDate(new \DateTime());
231 2
        $this->shippingRepository->save($Shipping);
232 2
        $this->entityManager->flush();
233
234 2
        return $this->json([
235 2
            'mail' => true,
236
            'shipped' => false,
237
        ]);
238
    }
239
240
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
241
     * @Method("POST")
242
     * @Route("/%eccube_admin_route%/shipping/bulk_delete", name="admin_shipping_bulk_delete")
243
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
244 1
    public function bulkDelete(Request $request)
245
    {
246 1
        $this->isTokenValid();
247 1
        $ids = $request->get('ids');
248
249 1
        foreach ($ids as $shipping_id) {
250
            /** @var Shipping $Shipping */
251 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...
252 1
            if ($Shipping) {
253 1
                $OrderItems = $Shipping->getOrderItems();
254
                /** @var OrderItem $OrderItem */
255 1
                foreach ($OrderItems as $OrderItem) {
256 1
                    $OrderItem->setShipping(null);
257
                }
258 1
                $this->entityManager->remove($Shipping);
259 1
                log_info('出荷削除', [$Shipping->getId()]);
260
            }
261
        }
262 1
        $this->entityManager->flush();
263
264 1
        $this->addSuccess('admin.shipping.delete.complete', 'admin');
265
266 1
        return $this->redirect($this->generateUrl('admin_shipping', ['resume' => Constant::ENABLED]));
267
    }
268
}
269