Failed Conditions
Push — master ( fc54b8...947180 )
by Yangsin
124:44 queued 119:37
created

Eccube/Controller/Admin/Order/MailController.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Order;
26
27
use Eccube\Application;
28
use Eccube\Entity\MailHistory;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
class MailController
35
{
36 6
    public function index(Application $app, Request $request, $id)
37
    {
38 6
        $Order = $app['eccube.repository.order']->find($id);
39
40 6
        if (is_null($Order)) {
41
            throw new NotFoundHttpException('order not found.');
42
        }
43
44 6
        $MailHistories = $app['eccube.repository.mail_history']->findBy(array('Order' => $id));
45
46 6
        $builder = $app['form.factory']->createBuilder('mail');
47
48 6
        $event = new EventArgs(
49
            array(
50 6
                'builder' => $builder,
51 6
                'Order' => $Order,
52 6
                'MailHistories' => $MailHistories,
53
            ),
54
            $request
55
        );
56 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE, $event);
57
58 6
        $form = $builder->getForm();
59
60 6
        if ('POST' === $request->getMethod()) {
61
62 4
            $form->handleRequest($request);
63
64 4
            $mode = $request->get('mode');
65
66
            // テンプレート変更の場合は. バリデーション前に内容差し替え.
67 4
            if ($mode == 'change') {
68 View Code Duplication
                if ($form->get('template')->isValid()) {
69
                    /** @var $data \Eccube\Entity\MailTemplate */
70
                    $MailTemplate = $form->get('template')->getData();
71
                    $form = $builder->getForm();
72
                    $event = new EventArgs(
73
                        array(
74
                            'form' => $form,
75
                            'Order' => $Order,
76
                            'MailTemplate' => $MailTemplate,
77
                        ),
78
                        $request
79
                    );
80
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE, $event);
81
                    $form->get('template')->setData($MailTemplate);
82
                    $form->get('subject')->setData($MailTemplate->getSubject());
83
                    $form->get('header')->setData($MailTemplate->getHeader());
84
                    $form->get('footer')->setData($MailTemplate->getFooter());
85
                }
86 4
            } else if ($form->isValid()) {
87
                switch ($mode) {
88 4
                    case 'confirm':
89
                        // フォームをFreezeして再生成.
90
91 2
                        $builder->setAttribute('freeze', true);
92 2
                        $builder->setAttribute('freeze_display_text', true);
93
94 2
                        $data = $form->getData();
95 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
96
97 2
                        $MailTemplate = $form->get('template')->getData();
98
99 2
                        $form = $builder->getForm();
100
101 2
                        $event = new EventArgs(
102
                            array(
103 2
                                'form' => $form,
104 2
                                'Order' => $Order,
105 2
                                'MailTemplate' => $MailTemplate,
106
                            ),
107
                            $request
108
                        );
109 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CONFIRM, $event);
110
111 2
                        $form->setData($data);
112 2
                        $form->get('template')->setData($MailTemplate);
113
114
115 2
                        return $app->render('Order/mail_confirm.twig', array(
116 2
                            'form' => $form->createView(),
117 2
                            'body' => $body,
118 2
                            'Order' => $Order,
119
                        ));
120
                        break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
121 2
                    case 'complete':
122
123 2
                        $data = $form->getData();
124 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
125
126
                        // メール送信
127 2
                        $app['eccube.service.mail']->sendAdminOrderMail($Order, $data);
128
129
                        // 送信履歴を保存.
130 2
                        $MailTemplate = $form->get('template')->getData();
131 2
                        $MailHistory = new MailHistory();
132
                        $MailHistory
133 2
                            ->setSubject($data['subject'])
134 2
                            ->setMailBody($body)
135 2
                            ->setMailTemplate($MailTemplate)
136 2
                            ->setSendDate(new \DateTime())
137 2
                            ->setOrder($Order);
138
139 2
                        $app['orm.em']->persist($MailHistory);
140 2
                        $app['orm.em']->flush($MailHistory);
141
142 2
                        $event = new EventArgs(
143
                            array(
144 2
                                'form' => $form,
145 2
                                'Order' => $Order,
146 2
                                'MailTemplate' => $MailTemplate,
147 2
                                'MailHistory' => $MailHistory,
148
                            ),
149
                            $request
150
                        );
151 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE, $event);
152
153
154 2
                        return $app->redirect($app->url('admin_order_mail_complete'));
155
                        break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
156
                    default:
157
                        break;
158
                }
159
            }
160
        }
161
162 2
        return $app->render('Order/mail.twig', array(
163 2
            'form' => $form->createView(),
164 2
            'Order' => $Order,
165 2
            'MailHistories' => $MailHistories
166
        ));
167
    }
168
169
170 1
    public function complete(Application $app)
171
    {
172 1
        return $app->render('Order/mail_complete.twig');
173
    }
174
175
176 2
    public function view(Application $app, Request $request)
177
    {
178
179 2
        if ($request->isXmlHttpRequest()) {
180 2
            $id = $request->get('id');
181 2
            $MailHistory = $app['eccube.repository.mail_history']->find($id);
182
183 2
            if (is_null($MailHistory)) {
184
                throw new NotFoundHttpException('history not found.');
185
            }
186
187 2
            $event = new EventArgs(
188
                array(
189 2
                    'MailHistory' => $MailHistory,
190
                ),
191
                $request
192
            );
193 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_VIEW_COMPLETE, $event);
194
195 2
            return $app->render('Order/mail_view.twig', array(
196 2
                'subject' => $MailHistory->getSubject(),
197 2
                'body' => $MailHistory->getMailBody()
198
            ));
199
        }
200
201
    }
202
203
204
205 6
    public function mailAll(Application $app, Request $request)
206
    {
207
208 6
        $builder = $app['form.factory']->createBuilder('mail');
209
210 6
        $event = new EventArgs(
211
            array(
212 6
                'builder' => $builder,
213
            ),
214
            $request
215
        );
216 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_INITIALIZE, $event);
217
218 6
        $form = $builder->getForm();
219
220 6
        $ids = '';
0 ignored issues
show
$ids 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...
221 6
        if ('POST' === $request->getMethod()) {
222
223 4
            $form->handleRequest($request);
224
225 4
            $mode = $request->get('mode');
226
227 4
            $ids = $request->get('ids');
228
229
            // テンプレート変更の場合は. バリデーション前に内容差し替え.
230 4
            if ($mode == 'change') {
231 View Code Duplication
                if ($form->get('template')->isValid()) {
232
                    /** @var $data \Eccube\Entity\MailTemplate */
233
                    $MailTemplate = $form->get('template')->getData();
234
                    $form = $builder->getForm();
235
236
                    $event = new EventArgs(
237
                        array(
238
                            'form' => $form,
239
                            'MailTemplate' => $MailTemplate,
240
                        ),
241
                        $request
242
                    );
243
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CHANGE, $event);
244
245
                    $form->get('template')->setData($MailTemplate);
246
                    $form->get('subject')->setData($MailTemplate->getSubject());
247
                    $form->get('header')->setData($MailTemplate->getHeader());
248
                    $form->get('footer')->setData($MailTemplate->getFooter());
249
                }
250 4
            } else if ($form->isValid()) {
251
                switch ($mode) {
252 4
                    case 'confirm':
253
                        // フォームをFreezeして再生成.
254
255 2
                        $builder->setAttribute('freeze', true);
256 2
                        $builder->setAttribute('freeze_display_text', true);
257
258 2
                        $data = $form->getData();
259
260 2
                        $tmp = explode(',', $ids);
261
262 2
                        $Order = $app['eccube.repository.order']->find($tmp[0]);
263
264 2
                        if (is_null($Order)) {
265
                            throw new NotFoundHttpException('order not found.');
266
                        }
267
268 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
269
270 2
                        $MailTemplate = $form->get('template')->getData();
271
272 2
                        $form = $builder->getForm();
273
274 2
                        $event = new EventArgs(
275
                            array(
276 2
                                'form' => $form,
277 2
                                'MailTemplate' => $MailTemplate,
278 2
                                'Order' => $Order,
279
                            ),
280
                            $request
281
                        );
282 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CONFIRM, $event);
283
284 2
                        $form->setData($data);
285 2
                        $form->get('template')->setData($MailTemplate);
286
287 2
                        return $app->render('Order/mail_all_confirm.twig', array(
288 2
                            'form' => $form->createView(),
289 2
                            'body' => $body,
290 2
                            'ids' => $ids,
291
                        ));
292
                        break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
293
294 2
                    case 'complete':
295
296 2
                        $data = $form->getData();
297
298 2
                        $ids = explode(',', $ids);
299
300 2
                        foreach ($ids as $value) {
301
302 2
                            $Order = $app['eccube.repository.order']->find($value);
303
304 2
                            $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
305
306
                            // メール送信
307 2
                            $app['eccube.service.mail']->sendAdminOrderMail($Order, $data);
308
309
                            // 送信履歴を保存.
310 2
                            $MailTemplate = $form->get('template')->getData();
311 2
                            $MailHistory = new MailHistory();
312
                            $MailHistory
313 2
                                ->setSubject($data['subject'])
314 2
                                ->setMailBody($body)
315 2
                                ->setMailTemplate($MailTemplate)
316 2
                                ->setSendDate(new \DateTime())
317 2
                                ->setOrder($Order);
318 2
                            $app['orm.em']->persist($MailHistory);
319
                        }
320
321 2
                        $app['orm.em']->flush($MailHistory);
0 ignored issues
show
The variable $MailHistory does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
322
323 2
                        $event = new EventArgs(
324
                            array(
325 2
                                'form' => $form,
326 2
                                'MailHistory' => $MailHistory,
327
                            ),
328
                            $request
329
                        );
330 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_COMPLETE, $event);
331
332 2
                        return $app->redirect($app->url('admin_order_mail_complete'));
333
                        break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
334
                    default:
335
                        break;
336
                }
337
            }
338
        } else {
339
            $filter = function ($v) {
340
                return preg_match('/^ids\d+$/', $v);
341 2
            };
342 2
            $map = function ($v) {
343
                return preg_replace('/[^\d+]/', '', $v);
344 2
            };
345 2
            $keys = array_keys($request->query->all());
346 2
            $idArray = array_map($map, array_filter($keys, $filter));
347 2
            $ids = implode(',', $idArray);
348
        }
349
350 2
        return $app->render('Order/mail_all.twig', array(
351 2
            'form' => $form->createView(),
352 2
            'ids' => $ids,
353
        ));
354
    }
355
356
357 8
    private function createBody($app, $header, $footer, $Order)
358
    {
359 8
        return $app->renderView('Mail/order.twig', array(
360 8
            'header' => $header,
361 8
            'footer' => $footer,
362 8
            'Order' => $Order,
363
        ));
364
    }
365
}
366