Completed
Push — master ( f7b119...9e4c7b )
by Yangsin
353:17 queued 347:09
created

CategoryRepository   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 268
Duplicated Lines 32.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.82%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 86
loc 268
ccs 152
cts 157
cp 0.9682
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setApplication() 0 4 1
A getTotalCount() 0 11 1
B getList() 0 36 4
B up() 43 43 3
B down() 43 43 3
B save() 0 42 5
B delete() 0 32 4

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 Doctrine\ORM\EntityRepository;
28
use Eccube\Application;
29
use Eccube\Entity\Category;
30
31
/**
32
 * CategoryRepository
33
 *
34
 * This class was generated by the Doctrine ORM. Add your own custom
35
 * repository methods below.
36
 */
37
class CategoryRepository extends EntityRepository
38
{
39
    /**
40
     * @var \Eccube\Application
41
     */
42
    protected $app;
43
44 318
    public function setApplication(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
45
    {
46 318
        $this->app = $app;
47 318
    }
48
49
    /**
50
     * 全カテゴリの合計を取得する.
51
     *
52
     * @return int 全カテゴリの合計数
53
     */
54 1
    public function getTotalCount()
55
    {
56 1
        $qb = $this
57 1
            ->createQueryBuilder('c')
58 1
            ->select('count(c.id)')
59 1
            ->where('c.del_flg = 0');
60 1
        $count = $qb->getQuery()
61 1
            ->getSingleScalarResult();
62
63 1
        return $count;
64
    }
65
66
    /**
67
     * カテゴリ一覧を取得する.
68
     *
69
     * 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する.
70
     *
71
     * @param \Eccube\Entity\Category|null $Parent 指定の親カテゴリ
72
     * @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる
0 ignored issues
show
introduced by
Expected 25 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
73
     *
74
     * @return \Eccube\Entity\Category[] カテゴリの配列
75
     */
76 81
    public function getList(Category $Parent = null, $flat = false)
77
    {
78 81
        $options = $this->app['config']['doctrine_cache'];
79 81
        $lifetime = $options['result_cache']['lifetime'];
80
81 81
        $qb = $this->createQueryBuilder('c1')
82 81
            ->select('c1, c2, c3, c4, c5')
83 81
            ->leftJoin('c1.Children', 'c2')
84 81
            ->leftJoin('c2.Children', 'c3')
85 81
            ->leftJoin('c3.Children', 'c4')
86 81
            ->leftJoin('c4.Children', 'c5')
87 81
            ->orderBy('c1.rank', 'DESC')
88 81
            ->addOrderBy('c2.rank', 'DESC')
89 81
            ->addOrderBy('c3.rank', 'DESC')
90 81
            ->addOrderBy('c4.rank', 'DESC')
91 81
            ->addOrderBy('c5.rank', 'DESC');
92
93 81
        if ($Parent) {
94 4
            $qb->where('c1.Parent = :Parent')->setParameter('Parent', $Parent);
95 4
        } else {
96 78
            $qb->where('c1.Parent IS NULL');
97
        }
98 81
        $Categories = $qb->getQuery()
99 81
            ->useResultCache(true, $lifetime)
100 81
            ->getResult();
101
102 81
        if ($flat) {
103 73
            $array = array();
104 73
            foreach ($Categories as $Category) {
105 73
                $array = array_merge($array, $Category->getSelfAndDescendants());
106 73
            }
107 73
            $Categories = $array;
108 73
        }
109
110 81
        return $Categories;
111
    }
112
113
    /**
114
     * カテゴリの順位を1上げる.
115
     *
116
     * @param  \Eccube\Entity\Category $Category カテゴリ
117
     * @return boolean 成功した場合 true
118
     *
119
     * @deprecated since 3.0.0, to be removed in 3.1
120
     */
121 2 View Code Duplication
    public function up(\Eccube\Entity\Category $Category)
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...
122
    {
123 2
        $em = $this->getEntityManager();
124 2
        $em->getConnection()->beginTransaction();
125
        try {
126 2
            $rank = $Category->getRank();
127 2
            $Parent = $Category->getParent();
128
129 2
            if ($Parent) {
130 1
                $CategoryUp = $this->createQueryBuilder('c')
131 1
                    ->where('c.rank > :rank AND c.Parent = :Parent')
132 1
                    ->setParameter('rank', $rank)
133 1
                    ->setParameter('Parent', $Parent)
134 1
                    ->orderBy('c.rank', 'ASC')
135 1
                    ->setMaxResults(1)
136 1
                    ->getQuery()
137 1
                    ->getSingleResult();
138 1
            } else {
139 1
                $CategoryUp = $this->createQueryBuilder('c')
140 1
                    ->where('c.rank > :rank AND c.Parent IS NULL')
141 1
                    ->setParameter('rank', $rank)
142 1
                    ->orderBy('c.rank', 'ASC')
143 1
                    ->setMaxResults(1)
144 1
                    ->getQuery()
145 1
                    ->getSingleResult();
146
            }
147
148 1
            $this_count = $Category->countBranches();
149 1
            $up_count = $CategoryUp->countBranches();
150
151 1
            $Category->calcChildrenRank($em, $up_count);
152 1
            $CategoryUp->calcChildrenRank($em, $this_count * -1);
153 1
            $em->flush();
154
155 1
            $em->getConnection()->commit();
156 2
        } catch (\Exception $e) {
157 1
            $em->getConnection()->rollback();
158
159 1
            return false;
160
        }
161
162 1
        return true;
163
    }
164
165
    /**
166
     * カテゴリの順位を1下げる.
167
     *
168
     * @param  \Eccube\Entity\Category $Category カテゴリ
169
     * @return boolean 成功した場合 true
170
     *
171
     * @deprecated since 3.0.0, to be removed in 3.1
172
     */
173 2 View Code Duplication
    public function down(\Eccube\Entity\Category $Category)
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...
174
    {
175 2
        $em = $this->getEntityManager();
176 2
        $em->getConnection()->beginTransaction();
177
        try {
178 2
            $rank = $Category->getRank();
179 2
            $Parent = $Category->getParent();
180
181 2
            if ($Parent) {
182 1
                $CategoryDown = $this->createQueryBuilder('c')
183 1
                    ->where('c.rank < :rank AND c.Parent = :Parent')
184 1
                    ->setParameter('rank', $rank)
185 1
                    ->setParameter('Parent', $Parent)
186 1
                    ->orderBy('c.rank', 'DESC')
187 1
                    ->setMaxResults(1)
188 1
                    ->getQuery()
189 1
                    ->getSingleResult();
190 1
            } else {
191 1
                $CategoryDown = $this->createQueryBuilder('c')
192 1
                    ->where('c.rank < :rank AND c.Parent IS NULL')
193 1
                    ->setParameter('rank', $rank)
194 1
                    ->orderBy('c.rank', 'DESC')
195 1
                    ->setMaxResults(1)
196 1
                    ->getQuery()
197 1
                    ->getSingleResult();
198
            }
199
200 1
            $this_count = $Category->countBranches();
201 1
            $down_count = $CategoryDown->countBranches();
202
203 1
            $Category->calcChildrenRank($em, $down_count * -1);
204 1
            $CategoryDown->calcChildrenRank($em, $this_count);
205 1
            $em->flush();
206
207 1
            $em->getConnection()->commit();
208 2
        } catch (\Exception $e) {
209 1
            $em->getConnection()->rollback();
210
211 1
            return false;
212
        }
213
214 1
        return true;
215
    }
216
217
    /**
218
     * カテゴリを保存する.
219
     *
220
     * @param  \Eccube\Entity\Category $Category カテゴリ
221
     * @return boolean 成功した場合 true
222
     */
223 9
    public function save(\Eccube\Entity\Category $Category)
224
    {
225 9
        $em = $this->getEntityManager();
226 9
        $em->getConnection()->beginTransaction();
227
        try {
228 9
            if (!$Category->getId()) {
229 7
                $Parent = $Category->getParent();
230 7
                if ($Parent) {
231 2
                    $rank = $Parent->getRank() - 1;
232 2
                } else {
233 5
                    $rank = $this->createQueryBuilder('c')
234 5
                        ->select('MAX(c.rank)')
235 5
                        ->getQuery()
236 5
                        ->getSingleScalarResult();
237
                }
238 7
                if (!$rank) {
239
                    $rank = 0;
240
                }
241 7
                $Category->setRank($rank + 1);
242 7
                $Category->setDelFlg(0);
243
244 7
                $em->createQueryBuilder()
245 7
                    ->update('Eccube\Entity\Category', 'c')
246 7
                    ->set('c.rank', 'c.rank + 1')
247 7
                    ->where('c.rank > :rank')
248 7
                    ->setParameter('rank', $rank)
249 7
                    ->getQuery()
250 7
                    ->execute();
251 7
            }
252
253 9
            $em->persist($Category);
254 9
            $em->flush();
255
256 8
            $em->getConnection()->commit();
257 9
        } catch (\Exception $e) {
258 1
            $em->getConnection()->rollback();
259
260 1
            return false;
261
        }
262
263 8
        return true;
264
    }
265
266
    /**
267
     * カテゴリを削除する.
268
     *
269
     * @param  \Eccube\Entity\Category $Category 削除対象のカテゴリ
270
     * @return boolean 成功した場合 true, 子カテゴリが存在する場合, 商品カテゴリが紐づいている場合は false
271
     */
272 3
    public function delete(\Eccube\Entity\Category $Category)
273
    {
274 3
        $em = $this->getEntityManager();
275 3
        $em->getConnection()->beginTransaction();
276
        try {
277 3
            if ($Category->getChildren()->count() > 0 || $Category->getProductCategories()->count() > 0) {
278
                throw new \Exception();
279
            }
280
281 3
            $rank = $Category->getRank();
282
283 3
            $em->createQueryBuilder()
284 3
                ->update('Eccube\Entity\Category', 'c')
285 3
                ->set('c.rank', 'c.rank - 1')
286 3
                ->where('c.rank > :rank')
287 3
                ->setParameter('rank', $rank)
288 3
                ->getQuery()
289 3
                ->execute();
290
291 3
            $Category->setDelFlg(1);
292 3
            $em->persist($Category);
293 3
            $em->flush();
294
295 3
            $em->getConnection()->commit();
296 3
        } catch (\Exception $e) {
297
            $em->getConnection()->rollback();
298
299
            return false;
300
        }
301
302 3
        return true;
303
    }
304
}
305