Completed
Pull Request — experimental/3.1 (#2154)
by Kentaro
448:33 queued 441:10
created

MailController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 332
Duplicated Lines 11.14 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 80.46%

Importance

Changes 0
Metric Value
dl 37
loc 332
ccs 140
cts 174
cp 0.8046
rs 10
c 0
b 0
f 0
wmc 22
lcom 0
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 18 132 8
A complete() 0 4 1
B view() 0 26 3
D mailAll() 19 150 9
A createBody() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Eccube\Form\Type\Admin\MailType;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
35
class MailController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37 6
    public function index(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
38
    {
39 6
        $Order = $app['eccube.repository.order']->find($id);
40
41 6
        if (is_null($Order)) {
42
            throw new NotFoundHttpException('order not found.');
43
        }
44
45 6
        $MailHistories = $app['eccube.repository.mail_history']->findBy(array('Order' => $id));
46
47 6
        $builder = $app['form.factory']->createBuilder(MailType::class);
48
49 6
        $event = new EventArgs(
50
            array(
51 6
                'builder' => $builder,
52 6
                'Order' => $Order,
53 6
                'MailHistories' => $MailHistories,
54
            ),
55
            $request
56
        );
57 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE, $event);
58
59 6
        $form = $builder->getForm();
60
61 6
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
62
63 4
            $form->handleRequest($request);
64
65 4
            $mode = $request->get('mode');
66
67
            // テンプレート変更の場合は. バリデーション前に内容差し替え.
68 4
            if ($mode == 'change') {
69 View Code Duplication
                if ($form->get('template')->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
                    /** @var $data \Eccube\Entity\MailTemplate */
71
                    $MailTemplate = $form->get('template')->getData();
72
                    $form = $builder->getForm();
73
                    $event = new EventArgs(
74
                        array(
75
                            'form' => $form,
76
                            'Order' => $Order,
77
                            'MailTemplate' => $MailTemplate,
78
                        ),
79
                        $request
80
                    );
81
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE, $event);
82
                    $form->get('template')->setData($MailTemplate);
83
                    $form->get('subject')->setData($MailTemplate->getSubject());
84
                    $form->get('header')->setData($MailTemplate->getHeader());
85
                    $form->get('footer')->setData($MailTemplate->getFooter());
86
                }
87 4
            } else if ($form->isValid()) {
88
                switch ($mode) {
89 4
                    case 'confirm':
90
                        // フォームをFreezeして再生成.
91
92 2
                        $builder->setAttribute('freeze', true);
93 2
                        $builder->setAttribute('freeze_display_text', true);
94
95 2
                        $data = $form->getData();
96 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
97
98 2
                        $MailTemplate = $form->get('template')->getData();
99
100 2
                        $form = $builder->getForm();
101
102 2
                        $event = new EventArgs(
103
                            array(
104 2
                                'form' => $form,
105 2
                                'Order' => $Order,
106 2
                                'MailTemplate' => $MailTemplate,
107
                            ),
108
                            $request
109
                        );
110 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CONFIRM, $event);
111
112 2
                        $form->setData($data);
113 2
                        $form->get('template')->setData($MailTemplate);
114
115
116 2
                        return $app->render('Order/mail_confirm.twig', array(
117 2
                            'form' => $form->createView(),
118 2
                            'body' => $body,
119 2
                            'Order' => $Order,
120
                        ));
121
                        break;
0 ignored issues
show
Unused Code introduced by
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...
122 2
                    case 'complete':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
123
124 2
                        $data = $form->getData();
125 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
126
127
                        // メール送信
128 2
                        $app['eccube.service.mail']->sendAdminOrderMail($Order, $data);
129
130
                        // 送信履歴を保存.
131 2
                        $MailTemplate = $form->get('template')->getData();
132 2
                        $MailHistory = new MailHistory();
133
                        $MailHistory
134 2
                            ->setSubject($data['subject'])
135 2
                            ->setMailBody($body)
136 2
                            ->setMailTemplate($MailTemplate)
137 2
                            ->setSendDate(new \DateTime())
138 2
                            ->setOrder($Order);
139
140 2
                        $app['orm.em']->persist($MailHistory);
141 2
                        $app['orm.em']->flush($MailHistory);
142
143 2
                        $event = new EventArgs(
144
                            array(
145 2
                                'form' => $form,
146 2
                                'Order' => $Order,
147 2
                                'MailTemplate' => $MailTemplate,
148 2
                                'MailHistory' => $MailHistory,
149
                            ),
150
                            $request
151
                        );
152 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE, $event);
153
154
155 2
                        return $app->redirect($app->url('admin_order_mail_complete'));
156
                        break;
0 ignored issues
show
Unused Code introduced by
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...
157
                    default:
158
                        break;
159
                }
160
            }
161
        }
162
163 2
        return $app->render('Order/mail.twig', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
164 2
            'form' => $form->createView(),
165 2
            'Order' => $Order,
166 2
            'MailHistories' => $MailHistories
167
        ));
168
    }
169
170
171 1
    public function complete(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
172
    {
173 1
        return $app->render('Order/mail_complete.twig');
174
    }
175
176
177 2
    public function view(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
178
    {
179
180 2
        if ($request->isXmlHttpRequest()) {
181 2
            $id = $request->get('id');
182 2
            $MailHistory = $app['eccube.repository.mail_history']->find($id);
183
184 2
            if (is_null($MailHistory)) {
185
                throw new NotFoundHttpException('history not found.');
186
            }
187
188 2
            $event = new EventArgs(
189
                array(
190 2
                    'MailHistory' => $MailHistory,
191
                ),
192
                $request
193
            );
194 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_VIEW_COMPLETE, $event);
195
196 2
            return $app->render('Order/mail_view.twig', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
197 2
                'subject' => $MailHistory->getSubject(),
198 2
                'body' => $MailHistory->getMailBody()
199
            ));
200
        }
201
202
    }
203
204
205
206 6
    public function mailAll(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
207
    {
208
209 6
        $builder = $app['form.factory']->createBuilder(MailType::class);
210
211 6
        $event = new EventArgs(
212
            array(
213 6
                'builder' => $builder,
214
            ),
215
            $request
216
        );
217 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_INITIALIZE, $event);
218
219 6
        $form = $builder->getForm();
220
221 6
        $ids = '';
0 ignored issues
show
Unused Code introduced by
$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...
222 6
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
223
224 4
            $form->handleRequest($request);
225
226 4
            $mode = $request->get('mode');
227
228 4
            $ids = $request->get('ids');
229
230
            // テンプレート変更の場合は. バリデーション前に内容差し替え.
231 4
            if ($mode == 'change') {
232 View Code Duplication
                if ($form->get('template')->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
233
                    /** @var $data \Eccube\Entity\MailTemplate */
234
                    $MailTemplate = $form->get('template')->getData();
235
                    $form = $builder->getForm();
236
237
                    $event = new EventArgs(
238
                        array(
239
                            'form' => $form,
240
                            'MailTemplate' => $MailTemplate,
241
                        ),
242
                        $request
243
                    );
244
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CHANGE, $event);
245
246
                    $form->get('template')->setData($MailTemplate);
247
                    $form->get('subject')->setData($MailTemplate->getSubject());
248
                    $form->get('header')->setData($MailTemplate->getHeader());
249
                    $form->get('footer')->setData($MailTemplate->getFooter());
250
                }
251 4
            } else if ($form->isValid()) {
252
                switch ($mode) {
253 4
                    case 'confirm':
254
                        // フォームをFreezeして再生成.
255
256 2
                        $builder->setAttribute('freeze', true);
257 2
                        $builder->setAttribute('freeze_display_text', true);
258
259 2
                        $data = $form->getData();
260
261 2
                        $tmp = explode(',', $ids);
262
263 2
                        $Order = $app['eccube.repository.order']->find($tmp[0]);
264
265 2
                        if (is_null($Order)) {
266
                            throw new NotFoundHttpException('order not found.');
267
                        }
268
269 2
                        $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
270
271 2
                        $MailTemplate = $form->get('template')->getData();
272
273 2
                        $form = $builder->getForm();
274
275 2
                        $event = new EventArgs(
276
                            array(
277 2
                                'form' => $form,
278 2
                                'MailTemplate' => $MailTemplate,
279 2
                                'Order' => $Order,
280
                            ),
281
                            $request
282
                        );
283 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CONFIRM, $event);
284
285 2
                        $form->setData($data);
286 2
                        $form->get('template')->setData($MailTemplate);
287
288 2
                        return $app->render('Order/mail_all_confirm.twig', array(
289 2
                            'form' => $form->createView(),
290 2
                            'body' => $body,
291 2
                            'ids' => $ids,
292
                        ));
293
                        break;
0 ignored issues
show
Unused Code introduced by
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...
294
295 2
                    case 'complete':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
296
297 2
                        $data = $form->getData();
298
299 2
                        $ids = explode(',', $ids);
300
301 2
                        foreach ($ids as $value) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
302
303 2
                            $Order = $app['eccube.repository.order']->find($value);
304
305 2
                            $body = $this->createBody($app, $data['header'], $data['footer'], $Order);
306
307
                            // メール送信
308 2
                            $app['eccube.service.mail']->sendAdminOrderMail($Order, $data);
309
310
                            // 送信履歴を保存.
311 2
                            $MailTemplate = $form->get('template')->getData();
312 2
                            $MailHistory = new MailHistory();
313
                            $MailHistory
314 2
                                ->setSubject($data['subject'])
315 2
                                ->setMailBody($body)
316 2
                                ->setMailTemplate($MailTemplate)
317 2
                                ->setSendDate(new \DateTime())
318 2
                                ->setOrder($Order);
319 2
                            $app['orm.em']->persist($MailHistory);
320
                        }
321
322 2
                        $app['orm.em']->flush($MailHistory);
0 ignored issues
show
Bug introduced by
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...
323
324 2
                        $event = new EventArgs(
325
                            array(
326 2
                                'form' => $form,
327 2
                                'MailHistory' => $MailHistory,
328
                            ),
329
                            $request
330
                        );
331 2
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_COMPLETE, $event);
332
333 2
                        return $app->redirect($app->url('admin_order_mail_complete'));
334
                        break;
0 ignored issues
show
Unused Code introduced by
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...
335
                    default:
336
                        break;
337
                }
338
            }
339
        } else {
340
            $filter = function ($v) {
341
                return preg_match('/^ids\d+$/', $v);
342 2
            };
343 2
            $map = function ($v) {
344
                return preg_replace('/[^\d+]/', '', $v);
345 2
            };
346 2
            $keys = array_keys($request->query->all());
347 2
            $idArray = array_map($map, array_filter($keys, $filter));
348 2
            $ids = implode(',', $idArray);
349
        }
350
351 2
        return $app->render('Order/mail_all.twig', array(
352 2
            'form' => $form->createView(),
353 2
            'ids' => $ids,
354
        ));
355
    }
356
357
358 8
    private function createBody($app, $header, $footer, $Order)
359
    {
360 8
        return $app->renderView('Mail/order.twig', array(
361 8
            'header' => $header,
362 8
            'footer' => $footer,
363 8
            'Order' => $Order,
364
        ));
365
    }
366
}
367