Failed Conditions
Push — improve/performance ( 85882a...af114f )
by Ryo
414:05 queued 405:58
created

BlockController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 163
Duplicated Lines 29.45 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 94.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 0
cbo 7
dl 48
loc 163
ccs 82
cts 87
cp 0.9425
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C edit() 6 93 8
B delete() 21 44 4
A index() 21 21 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\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 2
                \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...
132
133 2
                $event = new EventArgs(
134
                    array(
135 2
                        'form' => $form,
136 2
                        'Block' => $Block,
137
                    ),
138
                    $request
139
                );
140 2
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE, $event);
141
142 2
                $app->addSuccess('admin.register.complete', 'admin');
143
144 2
                return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId())));
145
            }
146
        }
147
148
149 2
        return $app->render('Content/block_edit.twig', array(
150 2
            'form' => $form->createView(),
151 2
            'block_id' => $id,
152 2
            'deletable' => $deletable,
153
        ));
154
    }
155
156 2
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
157
    {
158 2
        $this->isTokenValid($app);
159
160 2
        $DeviceType = $app['eccube.repository.master.device_type']
161 2
            ->find(DeviceType::DEVICE_TYPE_PC);
162
163 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...
164 2
            'id' => $id,
165 2
            'DeviceType' => $DeviceType
166
        ));
167
168 2
        if (!$Block) {
169
            $app->deleteMessage();
170
            return $app->redirect($app->url('admin_content_block'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
171
        }
172
173
        // ユーザーが作ったブロックのみ削除する
174
        // テンプレートが変更されていた場合、DBからはブロック削除されるがtwigファイルは残る
175 2 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...
176 1
            $tplDir = $app['config']['block_realdir'];
177 1
            $file = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
178 1
            $fs = new Filesystem();
179 1
            if ($fs->exists($file)) {
180
                $fs->remove($file);
181
            }
182 1
            $app['orm.em']->remove($Block);
183 1
            $app['orm.em']->flush();
184
185 1
            $event = new EventArgs(
186
                array(
187 1
                    'Block' => $Block,
188
                ),
189
                $request
190
            );
191 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE, $event);
192
193 1
            $app->addSuccess('admin.delete.complete', 'admin');
194 1
            \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...
195
        }
196
197
198 2
        return $app->redirect($app->url('admin_content_block'));
199
    }
200
}
201