Failed Conditions
Pull Request — master (#1535)
by k-yamamura
72:48 queued 36:28
created

PageController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 172
Duplicated Lines 15.7 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 82.35%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 14
c 4
b 0
f 0
lcom 0
cbo 6
dl 27
loc 172
ccs 14
cts 17
cp 0.8235
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D edit() 6 104 9
B delete() 21 43 4
A index() 0 20 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\Content;
26
27
use Eccube\Application;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Entity\Master\DeviceType;
30
use Eccube\Entity\PageLayout;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Eccube\Util\Str;
34
use Symfony\Component\Filesystem\Filesystem;
35
use Symfony\Component\Finder\Finder;
36 1
use Symfony\Component\HttpFoundation\Request;
37
38
class PageController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
39
{
40
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
41
    {
42
        $DeviceType = $app['eccube.repository.master.device_type']
43 1
            ->find(DeviceType::DEVICE_TYPE_PC);
44
45
        $PageLayouts = $app['eccube.repository.page_layout']->getPageList($DeviceType);
46 1
47
        $event = new EventArgs(
48 1
            array(
49
                'DeviceType' => $DeviceType,
50
                'PageLayouts' => $PageLayouts,
51
            ),
52
            $request
53
        );
54
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_INDEX_COMPLETE, $event);
55
56 1
        return $app->render('Content/page.twig', array(
57
            'PageLayouts' => $PageLayouts,
58
        ));
59
    }
60
61
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
62 1
    {
63
        $DeviceType = $app['eccube.repository.master.device_type']
64
            ->find(DeviceType::DEVICE_TYPE_PC);
65 1
66 1
        $PageLayout = $app['eccube.repository.page_layout']
67
            ->findOrCreate($id, $DeviceType);
68
69
        $editable = true;
70
71
        $builder = $app['form.factory']
72
            ->createBuilder('main_edit', $PageLayout);
73
74
        $event = new EventArgs(
75
            array(
76
                'builder' => $builder,
77
                'DeviceType' => $DeviceType,
78
                'PageLayout' => $PageLayout,
79
            ),
80
            $request
81
        );
82
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_INITIALIZE, $event);
83
84
        $form = $builder->getForm();
85
86
        // 更新時
87
        $fileName = null;
88
        if ($id) {
89
            // 編集不可ページはURL、ページ名、ファイル名を保持
90
            if ($PageLayout->getEditFlg() == PageLayout::EDIT_FLG_DEFAULT) {
91
                $editable = false;
92
                $PrevPageLayout = clone $PageLayout;
93
            }
94
            // テンプレートファイルの取得
95
            $file = $app['eccube.repository.page_layout']
96
                ->getReadTemplateFile($PageLayout->getFileName(), $editable);
97
98
            $form->get('tpl_data')->setData($file['tpl_data']);
99
100
            $fileName = $PageLayout->getFileName();
101
        }
102
103
        if ('POST' === $app['request']->getMethod()) {
104
            $form->handleRequest($app['request']);
105
            if ($form->isValid()) {
106
                $PageLayout = $form->getData();
107
108
                if (!$editable) {
109 1
                    $PageLayout
110 1
                        ->setUrl($PrevPageLayout->getUrl())
0 ignored issues
show
Bug introduced by
The variable $PrevPageLayout 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...
111 1
                        ->setFileName($PrevPageLayout->getFileName())
112
                        ->setName($PrevPageLayout->getName());
113
                }
114
                // DB登録
115 1
                $app['orm.em']->persist($PageLayout);
116
                $app['orm.em']->flush();
117 1
118
                // ファイル生成・更新
119
                $templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
120
                $filePath = $templatePath.'/'.$PageLayout->getFileName().'.twig';
121
122
                $fs = new Filesystem();
123
                $pageData = $form->get('tpl_data')->getData();
124
                $pageData = Str::convertLineFeed($pageData);
125
                $fs->dumpFile($filePath, $pageData);
126
127
                // 更新でファイル名を変更した場合、以前のファイルを削除
128 View Code Duplication
                if ($PageLayout->getFileName() != $fileName && !is_null($fileName)) {
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...
129
                    $oldFilePath = $templatePath.'/'.$fileName.'.twig';
130 1
                    if ($fs->exists($oldFilePath)) {
131
                        $fs->remove($oldFilePath);
132
                    }
133
                }
134
135
                $event = new EventArgs(
136
                    array(
137
                        'form' => $form,
138
                        'PageLayout' => $PageLayout,
139
                        'templatePath' => $templatePath,
140
                        'filePath' => $filePath,
141
                    ),
142
                    $request
143
                );
144
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_COMPLETE, $event);
145
146
                $app->addSuccess('admin.register.complete', 'admin');
147
148
                // twig キャッシュの削除.
149
                $finder = Finder::create()->in($app['config']['root_dir'].'/app/cache/twig');
150
                $fs->remove($finder);
151 1
152
                return $app->redirect($app->url('admin_content_page_edit', array('id' => $PageLayout->getId())));
153
            }
154
        }
155
156
        $templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
157
158
        return $app->render('Content/page_edit.twig', array(
159
            'form' => $form->createView(),
160
            'page_id' => $PageLayout->getId(),
161
            'editable' => $editable,
162
            'template_path' => $templatePath,
163
        ));
164
    }
165
166
    public function delete(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
167
    {
168
        $this->isTokenValid($app);
169
170
        $DeviceType = $app['eccube.repository.master.device_type']
171
            ->find(DeviceType::DEVICE_TYPE_PC);
172
173
        $PageLayout = $app['eccube.repository.page_layout']
174
            ->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
175
                'id' => $id,
176
                'DeviceType' => $DeviceType
177
            ));
178
179
        if (!$PageLayout) {
180
            $app->deleteMessage();
181
            return $app->redirect($app->url('admin_content_page'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
182
        }
183
184
        // ユーザーが作ったページのみ削除する
185 View Code Duplication
        if ($PageLayout->getEditFlg() == PageLayout::EDIT_FLG_USER) {
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...
186
            $templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath(true);
187
            $file = $templatePath.'/'.$PageLayout->getFileName().'.twig';
188
            $fs = new Filesystem();
189
            if ($fs->exists($file)) {
190
                $fs->remove($file);
191
            }
192
            $app['orm.em']->remove($PageLayout);
193
            $app['orm.em']->flush();
194
195
            $event = new EventArgs(
196
                array(
197
                    'DeviceType' => $DeviceType,
198
                    'PageLayout' => $PageLayout,
199
                ),
200
                $request
201
            );
202
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_PAGE_DELETE_COMPLETE, $event);
203
204
            $app->addSuccess('admin.delete.complete', 'admin');
205
        }
206
207
        return $app->redirect($app->url('admin_content_page'));
208
    }
209
}
210