Failed Conditions
Push — master ( 36ccc8...f8bd2f )
by Kentaro
34:47
created

BlockController::delete()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 26

Duplication

Lines 21
Ratio 47.73 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 21
loc 44
ccs 1
cts 1
cp 1
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 26
nc 4
nop 3
crap 4
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 Symfony\Component\Filesystem\Filesystem;
33
use Symfony\Component\Finder\Finder;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36 1
37
class BlockController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39 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...
40
    {
41
        $DeviceType = $app['eccube.repository.master.device_type']
42
            ->find(DeviceType::DEVICE_TYPE_PC);
43
44 1
        // 登録されているブロック一覧の取得
45
        $Blocks = $app['eccube.repository.block']->getList($DeviceType);
46
47 1
        $event = new EventArgs(
48
            array(
49 2
                'DeviceType' => $DeviceType,
50
                'Blocks' => $Blocks,
51
            ),
52
            $request
53
        );
54
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_INDEX_COMPLETE, $event);
55
56
        return $app->render('Content/block.twig', array(
57 2
            'Blocks' => $Blocks,
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
    {
63
        $DeviceType = $app['eccube.repository.master.device_type']
64
            ->find(DeviceType::DEVICE_TYPE_PC);
65 2
66 2
        $Block = $app['eccube.repository.block']
67
            ->findOrCreate($id, $DeviceType);
68
69 2
        if (!$Block) {
70
            throw new NotFoundHttpException();
71
        }
72
73
        $builder = $app['form.factory']
74 2
            ->createBuilder('block', $Block);
75
76
        $html = '';
77
        $previous_filename = null;
78
        $deletable = $Block->getDeletableFlg();
79
80
        if ($id) {
81
            // テンプレートファイルの取得
82
            $previous_filename = $Block->getFileName();
83
            $file = $app['eccube.repository.block']
84
                ->getReadTemplateFile($previous_filename, $deletable);
85
            $html = $file['tpl_data'];
86
        }
87
88
        $event = new EventArgs(
89
            array(
90
                'builder' => $builder,
91
                'DeviceType' => $DeviceType,
92
                'Block' => $Block,
93
                'html' => $html,
94
            ),
95
            $request
96
        );
97 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_INITIALIZE, $event);
98
        $html = $event->getArgument('html');
99
100
        $form = $builder->getForm();
101
102
        $form->get('block_html')->setData($html);
103
104
        if ($app['request']->getMethod() === 'POST') {
105
            $form->handleRequest($app['request']);
106
            if ($form->isValid()) {
107
                $Block = $form->getData();
108
109
                // DB登録
110
                $app['orm.em']->persist($Block);
111
                $app['orm.em']->flush();
112 1
113 1
                // ファイル生成・更新
114
                $tplDir = $app['config']['block_realdir'];
115
116
                $filePath = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
117 2
118
                $fs = new Filesystem();
119 1
                $fs->dumpFile($filePath, $form->get('block_html')->getData());
120
                // 更新でファイル名を変更した場合、以前のファイルを削除
121
                if ($Block->getFileName() != $previous_filename && !is_null($previous_filename)) {
122
                    $oldFilePath = $tplDir . $previous_filename;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123
                    if ($fs->exists($oldFilePath)) {
124
                        $fs->remove($oldFilePath);
125
                    }
126
                }
127
128
                \Eccube\Util\Cache::clear($app, false);
0 ignored issues
show
Documentation introduced by
$app is of type object<Eccube\Application>, but the function expects a object<Eccube\Util\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
130
                $event = new EventArgs(
131 1
                    array(
132
                        'form' => $form,
133
                        'Block' => $Block,
134
                    ),
135
                    $request
136
                );
137
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE, $event);
138
139
                $app->addSuccess('admin.register.complete', 'admin');
140
141
                return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId())));
142
            }
143
        }
144
145
146
        return $app->render('Content/block_edit.twig', array(
147
            'form' => $form->createView(),
148
            'block_id' => $id,
149
            'deletable' => $deletable,
150
        ));
151
    }
152
153
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
154 1
    {
155
        $this->isTokenValid($app);
156
157
        $DeviceType = $app['eccube.repository.master.device_type']
158
            ->find(DeviceType::DEVICE_TYPE_PC);
159
160
        $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...
161
            'id' => $id,
162
            'DeviceType' => $DeviceType
163
        ));
164
165
        if (!$Block) {
166
            $app->deleteMessage();
167
            return $app->redirect($app->url('admin_content_block'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
168
        }
169
170
        // ユーザーが作ったブロックのみ削除する
171
        // テンプレートが変更されていた場合、DBからはブロック削除されるがtwigファイルは残る
172 View Code Duplication
        if ($Block->getDeletableFlg() > 0) {
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...
173
            $tplDir = $app['config']['block_realdir'];
174
            $file = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
175
            $fs = new Filesystem();
176
            if ($fs->exists($file)) {
177
                $fs->remove($file);
178
            }
179
            $app['orm.em']->remove($Block);
180
            $app['orm.em']->flush();
181
182
            $event = new EventArgs(
183
                array(
184
                    'Block' => $Block,
185
                ),
186
                $request
187
            );
188
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE, $event);
189
190
            $app->addSuccess('admin.delete.complete', 'admin');
191
            \Eccube\Util\Cache::clear($app, false);
0 ignored issues
show
Documentation introduced by
$app is of type object<Eccube\Application>, but the function expects a object<Eccube\Util\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
192
        }
193
194
195
        return $app->redirect($app->url('admin_content_block'));
196
    }
197
}
198