Completed
Pull Request — master (#2007)
by Kentaro
38:32
created

BlockController::delete()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 26

Duplication

Lines 45
Ratio 100 %

Code Coverage

Tests 21
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 26
c 1
b 0
f 0
nc 4
nop 3
dl 45
loc 45
ccs 21
cts 24
cp 0.875
crap 4.0312
rs 8.5806
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\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Eccube\Util\Str;
33
use Symfony\Component\Filesystem\Filesystem;
34
use Symfony\Component\Finder\Finder;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37
38
class BlockController 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
        // 登録されているブロック一覧の取得
46 2
        $Blocks = $app['eccube.repository.block']->getList($DeviceType);
47
48 2
        $event = new EventArgs(
49
            array(
50 2
                'DeviceType' => $DeviceType,
51 2
                'Blocks' => $Blocks,
52
            ),
53
            $request
54
        );
55 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_INDEX_COMPLETE, $event);
56
57 2
        return $app->render('Content/block.twig', array(
58 2
            'Blocks' => $Blocks,
59
        ));
60
    }
61
62 4
    public function edit(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
63
    {
64 4
        $DeviceType = $app['eccube.repository.master.device_type']
65 4
            ->find(DeviceType::DEVICE_TYPE_PC);
66
67 4
        $Block = $app['eccube.repository.block']
68 4
            ->findOrCreate($id, $DeviceType);
69
70 4
        if (!$Block) {
71
            throw new NotFoundHttpException();
72
        }
73
74 4
        $builder = $app['form.factory']
75 4
            ->createBuilder('block', $Block);
76
77 4
        $html = '';
78 4
        $previous_filename = null;
79 4
        $deletable = $Block->getDeletableFlg();
80
81 4
        if ($id) {
82
            // テンプレートファイルの取得
83 4
            $previous_filename = $Block->getFileName();
84 4
            $file = $app['eccube.repository.block']
85 4
                ->getReadTemplateFile($previous_filename, $deletable);
86 4
            $html = $file['tpl_data'];
87
        }
88
89 4
        $event = new EventArgs(
90
            array(
91 4
                'builder' => $builder,
92 4
                'DeviceType' => $DeviceType,
93 4
                'Block' => $Block,
94 4
                'html' => $html,
95
            ),
96
            $request
97
        );
98 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_INITIALIZE, $event);
99 4
        $html = $event->getArgument('html');
100
101 4
        $form = $builder->getForm();
102
103 4
        $form->get('block_html')->setData($html);
104
105 4
        if ($app['request']->getMethod() === 'POST') {
106 2
            $form->handleRequest($app['request']);
107 2
            if ($form->isValid()) {
108 2
                $Block = $form->getData();
109
110
                // DB登録
111 2
                $app['orm.em']->persist($Block);
112 2
                $app['orm.em']->flush();
113
114
                // ファイル生成・更新
115 2
                $tplDir = $app['config']['block_realdir'];
116
117 2
                $filePath = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
118
119 2
                $fs = new Filesystem();
120 2
                $blockData = $form->get('block_html')->getData();
121 2
                $blockData = Str::convertLineFeed($blockData);
122 2
                $fs->dumpFile($filePath, $blockData);
123
                // 更新でファイル名を変更した場合、以前のファイルを削除
124 2 View Code Duplication
                if ($Block->getFileName() != $previous_filename && !is_null($previous_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...
125 2
                    $oldFilePath = $tplDir . '/' . $previous_filename . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
126 2
                    if ($fs->exists($oldFilePath)) {
127
                        $fs->remove($oldFilePath);
128
                    }
129
                }
130
131
                //twigテンプレートのみ削除
132 2
                \Eccube\Util\Cache::clear($app, false, true);
133
134 2
                $event = new EventArgs(
135
                    array(
136 2
                        'form' => $form,
137 2
                        'Block' => $Block,
138
                    ),
139
                    $request
140
                );
141 2
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE, $event);
142
143 2
                $app->addSuccess('admin.register.complete', 'admin');
144
145 2
                return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId())));
146
            }
147
        }
148
149
150 2
        return $app->render('Content/block_edit.twig', array(
151 2
            'form' => $form->createView(),
152 2
            'block_id' => $id,
153 2
            'deletable' => $deletable,
154
        ));
155
    }
156
157 2 View Code Duplication
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
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...
158
    {
159 2
        $this->isTokenValid($app);
160
161 2
        $DeviceType = $app['eccube.repository.master.device_type']
162 2
            ->find(DeviceType::DEVICE_TYPE_PC);
163
164 2
        $Block = $app['eccube.repository.block']->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
165 2
            'id' => $id,
166 2
            'DeviceType' => $DeviceType
167
        ));
168
169 2
        if (!$Block) {
170
            $app->deleteMessage();
171
            return $app->redirect($app->url('admin_content_block'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
172
        }
173
174
        // ユーザーが作ったブロックのみ削除する
175
        // テンプレートが変更されていた場合、DBからはブロック削除されるがtwigファイルは残る
176 2
        if ($Block->getDeletableFlg() > 0) {
177 1
            $tplDir = $app['config']['block_realdir'];
178 1
            $file = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
179 1
            $fs = new Filesystem();
180 1
            if ($fs->exists($file)) {
181
                $fs->remove($file);
182
            }
183 1
            $app['orm.em']->remove($Block);
184 1
            $app['orm.em']->flush();
185
186 1
            $event = new EventArgs(
187
                array(
188 1
                    'Block' => $Block,
189
                ),
190
                $request
191
            );
192 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE, $event);
193
194 1
            $app->addSuccess('admin.delete.complete', 'admin');
195
            //twigテンプレートのみ削除
196 1
            \Eccube\Util\Cache::clear($app, false, true);
197
        }
198
199
200 2
        return $app->redirect($app->url('admin_content_block'));
201
    }
202
}
203