Failed Conditions
Pull Request — master (#1913)
by k-yamamura
36:36
created

MailTemplateController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 2
dl 0
loc 21
ccs 8
cts 11
cp 0.7272
crap 3.1825
rs 9.3142
c 0
b 0
f 0
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\Content;
26
27
use Eccube\Application;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Util\Cache;
30
use Eccube\Util\Str;
31
use Symfony\Component\Filesystem\Filesystem;
32
use Symfony\Component\Finder\Finder;
33
use Symfony\Component\HttpFoundation\Request;
34
35
class MailTemplateController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
38 1
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing function doc comment
Loading history...
39
    {
40
        // Mailディレクトリ(app/template、Resource/template)からメールファイルを取得
41 1
        $finder = Finder::create()->depth(0);
42 1
        $mailDir = $app['config']['template_default_realdir'].'/Mail';
43
44 1
        $files = array();
45 1
        foreach ($finder->in($mailDir) as $file) {
46 1
            $files[$file->getFilename()] = $file->getFilename();
47
        }
48
49 1
        $mailDir = $app['config']['template_realdir'].'/Mail';
50 1
        foreach ($finder->in($mailDir) as $file) {
51
            $files[$file->getFilename()] = $file->getFilename();
52
        }
53
54
        return $app->render('Content/mail.twig', array(
55
            'files' => $files,
56
        ));
57
58
    }
59
60 2
    public function edit(Application $app, Request $request, $name)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
61
    {
62
63
        $readPaths = array(
64 2
            $app['config']['template_realdir'],
65 2
            $app['config']['template_default_realdir'],
66
        );
67
68 2
        $fs = new Filesystem();
69 2
        $tplData = null;
70 2 View Code Duplication
        foreach ($readPaths as $readPath) {
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...
71 2
            $filePath = $readPath.'/Mail/'.$name;
72 2
            if ($fs->exists($filePath)) {
73 2
                $tplData = file_get_contents($filePath);
74 2
                break;
75
            }
76
        }
77
78 2 View Code Duplication
        if (!$tplData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tplData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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...
79
            log_info("対象ファイルが存在しません");
80
            $app->addError('admin.content.mail.edit.error', 'admin');
81
82
            return $app->redirect($app->url('admin_content_mail'));
83
        }
84
85 2
        $builder = $app['form.factory']->createBuilder('admin_mail_template');
86
87 2
        $form = $builder->getForm();
88
89 2
        $form->get('tpl_data')->setData($tplData);
90
91 2
        $form->handleRequest($request);
92
93 2
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
94
95
            // ファイル生成・更新
96 1
            $filePath = $app['config']['template_realdir'].'/Mail/'.$name;
97
98 1
            $fs = new Filesystem();
99 1
            $pageData = $form->get('tpl_data')->getData();
100 1
            $pageData = Str::convertLineFeed($pageData);
101 1
            $fs->dumpFile($filePath, $pageData);
102
103 1
            $app->addSuccess('admin.register.complete', 'admin');
104
105
            // twig キャッシュの削除.
106 1
            Cache::clear($app, false, true);
107
108 1
            return $app->redirect($app->url('admin_content_mail_edit', array(
109 1
                'name' => $name,
110
            )));
111
        }
112
113 1
        return $app->render('Content/mail_edit.twig', array(
114 1
            'name' => $name,
115 1
            'form' => $form->createView(),
116
        ));
117
    }
118
119 1
    public function reedit(Application $app, Request $request, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing function doc comment
Loading history...
120
    {
121
122 1
        $this->isTokenValid($app);
123
124
        $readPaths = array(
125 1
            $app['config']['template_default_realdir'],
126
        );
127
128 1
        $fs = new Filesystem();
129 1
        $tplData = null;
130 1 View Code Duplication
        foreach ($readPaths as $readPath) {
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...
131 1
            $filePath = $readPath.'/Mail/'.$name;
132 1
            if ($fs->exists($filePath)) {
133 1
                $tplData = file_get_contents($filePath);
134 1
                break;
135
            }
136
        }
137
138 1 View Code Duplication
        if (!$tplData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tplData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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...
139
            log_info("対象ファイルが存在しません");
140
            $app->addError('admin.content.mail.edit.error', 'admin');
141
142
            return $app->redirect($app->url('admin_content_mail'));
143
        }
144
145 1
        $builder = $app['form.factory']->createBuilder('admin_mail_template');
146
147 1
        $form = $builder->getForm();
148
149 1
        $form->get('tpl_data')->setData($tplData);
150
151
        // ファイル生成・更新
152 1
        $filePath = $app['config']['template_realdir'].'/Mail/'.$name;
153
154 1
        $fs = new Filesystem();
155 1
        $fs->dumpFile($filePath, $tplData);
156
157 1
        $app->addSuccess('admin.content.mail.init.complete', 'admin');
158
159 1
        return $app->render('Content/mail_edit.twig', array(
160 1
            'name' => $name,
161 1
            'form' => $form->createView(),
162
        ));
163
    }
164
165
}
166