Completed
Push — sf/improvement-coverage ( b3937e...01a837 )
by Kiyotaka
51:20 queued 45:08
created

MailController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
ccs 41
cts 41
cp 1
wmc 7
lcom 1
cbo 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B index() 0 71 6
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\Setting\Shop;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\MailTemplate;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Form\Type\Admin\MailType;
21
use Eccube\Repository\MailTemplateRepository;
22
use Eccube\Util\StringUtil;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Symfony\Component\Filesystem\Filesystem;
26
use Symfony\Component\HttpFoundation\Request;
27
use Twig\Environment;
28
29
/**
30
 * Class MailController
31
 */
32
class MailController extends AbstractController
33
{
34
    /**
35
     * @var MailTemplateRepository
36
     */
37
    protected $mailTemplateRepository;
38
39
    /**
40
     * MailController constructor.
41
     *
42
     * @param MailTemplateRepository $mailTemplateRepository
43
     */
44 5
    public function __construct(MailTemplateRepository $mailTemplateRepository)
45
    {
46 5
        $this->mailTemplateRepository = $mailTemplateRepository;
47
    }
48
49
    /**
50
     * @Route("/%eccube_admin_route%/setting/shop/mail", name="admin_setting_shop_mail")
51
     * @Route("/%eccube_admin_route%/setting/shop/mail/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit")
52
     * @Template("@admin/Setting/Shop/mail.twig")
53
     */
54 5
    public function index(Request $request, MailTemplate $Mail = null, Environment $twig)
55
    {
56 5
        $builder = $this->formFactory
57 5
            ->createBuilder(MailType::class, $Mail);
58
59 5
        $event = new EventArgs(
60
            [
61 5
                'builder' => $builder,
62 5
                'Mail' => $Mail,
63
            ],
64 5
            $request
65
        );
66 5
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event);
67
68 5
        $form = $builder->getForm();
69 5
        $form['template']->setData($Mail);
70
71
        // 更新時
72 5
        if (!is_null($Mail)) {
73
            // テンプレートファイルの取得
74 2
            $source = $twig->getLoader()
75 2
                ->getSourceContext($Mail->getFileName())
76 2
                ->getCode();
77
78 2
            $form->get('tpl_data')->setData($source);
79
        }
80
81 5
        if ('POST' === $request->getMethod()) {
82 3
            $form->handleRequest($request);
83
84
            // 新規登録は現時点では未実装とする.
85 3
            if (is_null($Mail)) {
86 2
                $this->addError('admin.shop.mail.save.error', 'admin');
87
88 2
                return $this->redirectToRoute('admin_setting_shop_mail');
89
            }
90
91 1
            if ($form->isValid()) {
92 1
                $this->entityManager->flush();
93
94
                // ファイル生成・更新
95 1
                $templatePath = $this->getParameter('eccube_theme_front_dir');
96 1
                $filePath = $templatePath.'/'.$Mail->getFileName();
97
98 1
                $fs = new Filesystem();
99 1
                $mailData = $form->get('tpl_data')->getData();
100 1
                $mailData = StringUtil::convertLineFeed($mailData);
101 1
                $fs->dumpFile($filePath, $mailData);
102
103 1
                $event = new EventArgs(
104
                    [
105 1
                        'form' => $form,
106 1
                        'Mail' => $Mail,
107 1
                        'templatePath' => $templatePath,
108 1
                        'filePath' => $filePath,
109
                    ],
110 1
                    $request
111
                );
112 1
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event);
113
114 1
                $this->addSuccess('admin.shop.mail.save.complete', 'admin');
115
116 1
                return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
117
            }
118
        }
119
120
        return [
121 2
            'form' => $form->createView(),
122 2
            'id' => is_null($Mail) ? null : $Mail->getId(),
123
        ];
124
    }
125
}
126