Failed Conditions
Pull Request — experimental/3.1 (#2304)
by Kiyotaka
39:27
created

CategoryRepository::setApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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