Completed
Push — master ( d417ec...9a0123 )
by Yangsin
36:05
created

PageController::delete()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 27

Duplication

Lines 21
Ratio 47.73 %

Code Coverage

Tests 22
CRAP Score 4.0275

Importance

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