Completed
Pull Request — experimental/3.1 (#2308)
by chihiro
39:48
created

BlockRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 166
Duplicated Lines 18.67 %

Coupling/Cohesion

Components 3
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 31
loc 166
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 3
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewBlockId() 0 12 1
A findOrCreate() 0 10 2
A newBlock() 0 10 1
A getBlock() 0 9 1
A getList() 0 13 1
A getPageList() 21 21 3
A getWriteTemplatePath() 0 4 1
A getReadTemplateFile() 10 17 3

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\Repository;
26
27
use Eccube\Entity\Block;
28
use Symfony\Component\Filesystem\Filesystem;
29
30
/**
31
 * BlocRepository
32
 *
33
 * This class was generated by the Doctrine ORM. Add your own custom
34
 * repository methods below.
35
 */
36
class BlockRepository extends AbstractRepository
37
{
38
39
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$block_id" missing
Loading history...
introduced by
Doc comment for parameter "$DeviceType" missing
Loading history...
40 14
     * @deprecated 呼び出し元で制御する
41
     * @param $block_id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
42 14
     * @param $DeviceType
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
43
     * @return array|\Eccube\Entity\Block
44
     */
45 5
    public function findOrCreate($block_id, $DeviceType)
46
    {
47
48 5
        if ($block_id == null) {
49 1
            return $this->newBlock($DeviceType);
50
        } else {
51 5
            return $this->getBlock($block_id, $DeviceType);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Repository\BlockRepository::getBlock() has been deprecated with message: Use magic finder methods. BlockRepository::findOneByIdAndDeviceType()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52
        }
53
54
    }
55
56 1
    public function newBlock($DeviceType)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
57
    {
58 1
        $Block = new \Eccube\Entity\Block();
59
        $Block
60 1
            ->setDeviceType($DeviceType)
61 1
            ->setLogicFlg(0)
62 1
            ->setDeletableFlg(1);
63
64 1
        return $Block;
65
    }
66
67
    /**
68
     * @deprecated since 3.0.0, to be removed in 3.1
69
     */
70
    private function getNewBlockId($DeviceType)
71
    {
72
73
        $qb = $this->createQueryBuilder('b')
74
            ->select('max(b.id) +1 as block_id')
75
            ->where('b.DeviceType = :DeviceType')
76
            ->setParameter('DeviceType', $DeviceType);
77
        $result = $qb->getQuery()->getSingleResult();
78
79
        return $result['block_id'];
80
81
    }
82
83
    /**
84
     * ブロックの情報を取得.
85
     *
86
     * @deprecated Use magic finder methods. BlockRepository::findOneByIdAndDeviceType()
87
     * @param  integer $block_id ブロックID
0 ignored issues
show
introduced by
Expected 26 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
88
     * @param  \Eccube\Entity\Master\DeviceType $DeviceType
89
     * @return Block
90 6
     */
91
    public function getBlock($block_id, $DeviceType)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
92 6
    {
93 6
        $Block = $this->findOneBy(array(
94 6
            'id' => $block_id,
95
            'DeviceType' => $DeviceType,
96
        ));
97 6
98
        return $Block;
99
    }
100
101
    /**
102
     * ブロック一覧の取得.
103
     *
104
     * @param  \Eccube\Entity\Master\DeviceType $DeviceType
105
     * @return array
106 3
     */
107
    public function getList($DeviceType)
108 3
    {
109 3
        $qb = $this->createQueryBuilder('b')
110 3
            ->orderBy('b.id', 'DESC')
111 3
            ->where('b.DeviceType = :DeviceType')
112
            ->setParameter('DeviceType', $DeviceType);
113
114 3
        $Blocks = $qb
115 3
            ->getQuery()
116
            ->getResult();
117 3
118
        return $Blocks;
119
    }
120
121
    /**
122
     * ページの属性を取得する.
123
     *
124
     * この関数は, dtb_pagelayout の情報を検索する.
125
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
126
     *
127
     * @access public
128
     * @param  DeviceType $DeviceType 端末種別ID
129
     * @param  string $where 追加の検索条件
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
130
     * @param  string[] $parameters 追加の検索パラメーター
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
131
     * @return array                             ページ属性の配列
132
     * @deprecated since 3.0.0, to be removed in 3.1
133
     */
134 View Code Duplication
    public function getPageList(DeviceType $DeviceType, $where = null, $parameters = array())
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...
135
    {
136
        $qb = $this->createQueryBuilder('l')
137
            ->orderBy('l.id', 'DESC')
138
            ->where('l.DeviceType = :DeviceType')
139
            ->setParameter('DeviceType', $DeviceType)
140
            ->andWhere('l.id <> 0')
141
            ->orderBy('l.id', 'ASC');
142
        if (!is_null($where)) {
143
            $qb->andWhere($where);
144
            foreach ($parameters as $key => $val) {
145
                $qb->setParameter($key, $val);
146
            }
147
        }
148
149
        $PageLayouts = $qb
150
            ->getQuery()
151
            ->getResult();
152
153
        return $PageLayouts;
154
    }
155
156
    /**
157
     * 書き込みパスの取得
158
     * User定義の場合: /html/user_data
159
     * そうでない場合: /app/template/{template_code}
160
     *
161
     * @param  boolean $isUser
162
     * @return string
163
     *
164
     * @deprecated since 3.0.0, to be removed in 3.1
165
     */
166
    public function getWriteTemplatePath($isUser = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isUser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        return $this->app['config']['block_realdir'];
169
    }
170
171
    /**
172
     * 読み込みファイルの取得
173
     *
174
     * 1. block_realdir
175
     *      app/template/{template_code}/block
176
     * 2. block_default_readldir
177
     *      src/Eccube/Resource/template/default/block
178
     *
179
     * @param string $fileName
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
180
     * @param  boolean $isUser
181
     *
182
     * @return array
183 6
     */
184
    public function getReadTemplateFile($fileName, $isUser = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isUser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
    {
186 6
        $readPaths = array(
187 6
            $this->app['config']['block_realdir'],
188
            $this->app['config']['block_default_realdir'],
189 6
        );
190 6 View Code Duplication
        foreach ($readPaths as $readPath) {
191 6
            $filePath = $readPath . '/' . $fileName . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
192 6
            $fs = new Filesystem();
193
            if ($fs->exists($filePath)) {
194 6
                return array(
195 6
                    'file_name' => $fileName,
196
                    'tpl_data' => file_get_contents($filePath),
197
                );
198
            }
199
        }
200
    }
201
}
202