Completed
Pull Request — master (#1376)
by Kentaro
19:58
created

BlockController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 84.21%
Metric Value
wmc 13
lcom 0
cbo 5
dl 0
loc 122
ccs 16
cts 19
cp 0.8421
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 12 1
B delete() 0 36 4
C edit() 0 69 8
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 Symfony\Component\Finder\Finder;
31
use Symfony\Component\Filesystem\Filesystem;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
class BlockController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36 1
    public function index(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        $DeviceType = $app['eccube.repository.master.device_type']
39
            ->find(DeviceType::DEVICE_TYPE_PC);
40
41
        // 登録されているブロック一覧の取得
42
        $Blocks = $app['eccube.repository.block']->getList($DeviceType);
43
44 1
        return $app->render('Content/block.twig', array(
45
            'Blocks' => $Blocks,
46
        ));
47 1
    }
48
49 2
    public function edit(Application $app, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
50
    {
51
        $DeviceType = $app['eccube.repository.master.device_type']
52
            ->find(DeviceType::DEVICE_TYPE_PC);
53
54
        $Block = $app['eccube.repository.block']
55
            ->findOrCreate($id, $DeviceType);
56
57 2
        if (!$Block) {
58
            throw new NotFoundHttpException();
59
        }
60
61
        $form = $app['form.factory']
62
            ->createBuilder('block', $Block)
63
            ->getForm();
64
65 2
        $html = '';
66 2
        $previous_filename = null;
67
        $deletable = $Block->getDeletableFlg();
68
69 2
        if ($id) {
70
            // テンプレートファイルの取得
71
            $previous_filename = $Block->getFileName();
72
            $file = $app['eccube.repository.block']
73
                ->getReadTemplateFile($previous_filename, $deletable);
74 2
            $html = $file['tpl_data'];
75
        }
76
77
        $form->get('block_html')->setData($html);
78
79
        if ($app['request']->getMethod() === 'POST') {
80
            $form->handleRequest($app['request']);
81
            if ($form->isValid()) {
82
                $Block = $form->getData();
83
84
                // DB登録
85
                $app['orm.em']->persist($Block);
86
                $app['orm.em']->flush();
87
88
                // ファイル生成・更新
89
                $tplDir = $app['config']['block_realdir'];
90
91
                $filePath = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
92
93
                $fs = new Filesystem();
94
                $fs->dumpFile($filePath, $form->get('block_html')->getData());
95
                // 更新でファイル名を変更した場合、以前のファイルを削除
96
                if ($Block->getFileName() != $previous_filename && !is_null($previous_filename)) {
97 1
                    $oldFilePath = $tplDir . $previous_filename;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
98
                    if ($fs->exists($oldFilePath)) {
99
                        $fs->remove($oldFilePath);
100
                    }
101
                }
102
103
                \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...
introduced by
Add a single space after each comma delimiter
Loading history...
104
105
                $app->addSuccess('admin.register.complete', 'admin');
106
107
                return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId())));
108
            }
109
        }
110
111
112 1
        return $app->render('Content/block_edit.twig', array(
113 1
            'form' => $form->createView(),
114
            'block_id' => $id,
115
            'deletable' => $deletable,
116
        ));
117 2
    }
118
119 1
    public function delete(Application $app, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
120
    {
121
        $this->isTokenValid($app);
122
123
        $DeviceType = $app['eccube.repository.master.device_type']
124
            ->find(DeviceType::DEVICE_TYPE_PC);
125
126
        $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...
127
                'id' => $id,
128
                'DeviceType' => $DeviceType
129
            ));
130
131 1
        if (!$Block) {
132
            $app->deleteMessage();
133
            return $app->redirect($app->url('admin_content_block'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
134
        }
135
136
        // ユーザーが作ったブロックのみ削除する
137
        // テンプレートが変更されていた場合、DBからはブロック削除されるがtwigファイルは残る
138
        if ($Block->getDeletableFlg() > 0) {
139
            $tplDir = $app['config']['block_realdir'];
140
            $file = $tplDir . '/' . $Block->getFileName() . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
141
            $fs = new Filesystem();
142
            if ($fs->exists($file)) {
143
                $fs->remove($file);
144
            }
145
            $app['orm.em']->remove($Block);
146
            $app['orm.em']->flush();
147
148
            $app->addSuccess('admin.delete.complete', 'admin');
149
            \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...
introduced by
Add a single space after each comma delimiter
Loading history...
150
        }
151
152
153
        return $app->redirect($app->url('admin_content_block'));
154 1
    }
155
}
156