Failed Conditions
Pull Request — experimental/3.1 (#2512)
by chihiro
12:14
created

ClassCategoryRepository::toggleVisibility()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 8
nop 1
dl 0
loc 22
ccs 10
cts 13
cp 0.7692
crap 3.1105
rs 9.2
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\Annotation\Repository;
28
29
/**
30
 * ClasscategoryRepository
31
 *
32
 * This class was generated by the Doctrine ORM. Add your own custom
33
 * repository methods below.
34
 *
35
 * @Repository
36
 */
37
class ClassCategoryRepository extends AbstractRepository
38
{
39
    /**
40
     * 規格カテゴリの一覧を取得します.
41
     *
42
     * @param \Eccube\Entity\ClassName $ClassName 検索対象の規格名オブジェクト. 指定しない場合は、すべての規格を対象に取得します.
43
     * @return array 規格カテゴリの配列
44
     */
45 4
    public function getList(\Eccube\Entity\ClassName $ClassName = null)
46
    {
47 4
        $qb = $this->createQueryBuilder('cc')
48 4
            ->orderBy('cc.rank', 'DESC'); // TODO ClassName ごとにソートした方が良いかも
49 4
        if ($ClassName) {
50 3
            $qb->where('cc.ClassName = :ClassName')->setParameter('ClassName', $ClassName);
51
        }
52 4
        $ClassCategories = $qb->getQuery()
53 4
            ->getResult();
54
55 4
        return $ClassCategories;
56
    }
57
58
    /**
59
     * 規格カテゴリの順位を1上げる.
60
     *
61
     * @param \Eccube\Entity\ClassCategory $ClassCategory
62
     * @return boolean 成功した場合 true
63
     */
64 2 View Code Duplication
    public function up(\Eccube\Entity\ClassCategory $ClassCategory)
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...
65
    {
66 2
        $em = $this->getEntityManager();
67 2
        $em->getConnection()->beginTransaction();
68
        try {
69 2
            $rank = $ClassCategory->getRank();
70 2
            $ClassName = $ClassCategory->getClassName();
71
72 2
            $ClassCategory2 = $this->findOneBy(array('rank' => $rank + 1, 'ClassName' => $ClassName));
73 2
            if (!$ClassCategory2) {
74 1
                throw new \Exception();
75
            }
76 1
            $ClassCategory2->setRank($rank);
77 1
            $em->persist($ClassCategory);
78
79
            // ClassCategory更新
80 1
            $ClassCategory->setRank($rank + 1);
81
82 1
            $em->persist($ClassCategory);
83 1
            $em->flush();
84
85 1
            $em->getConnection()->commit();
86 1
        } catch (\Exception $e) {
87 1
            $em->getConnection()->rollback();
88
89 1
            return false;
90
        }
91
92 1
        return true;
93
    }
94
95
    /**
96
     * 規格カテゴリの順位を1下げる.
97
     *
98
     * @param  \Eccube\Entity\ClassCategory $ClassCategory
99
     * @return boolean 成功した場合 true
100
     */
101 2 View Code Duplication
    public function down(\Eccube\Entity\ClassCategory $ClassCategory)
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...
102
    {
103 2
        $em = $this->getEntityManager();
104 2
        $em->getConnection()->beginTransaction();
105
        try {
106 2
            $rank = $ClassCategory->getRank();
107 2
            $ClassName = $ClassCategory->getClassName();
108
109
            //
110 2
            $ClassCategory2 = $this->findOneBy(array('rank' => $rank - 1, 'ClassName' => $ClassName));
111 2
            if (!$ClassCategory2) {
112 1
                throw new \Exception();
113
            }
114 1
            $ClassCategory2->setRank($rank);
115 1
            $em->persist($ClassCategory);
116
117
            // ClassCategory更新
118 1
            $ClassCategory->setRank($rank - 1);
119
120 1
            $em->persist($ClassCategory);
121 1
            $em->flush();
122
123 1
            $em->getConnection()->commit();
124 1
        } catch (\Exception $e) {
125 1
            $em->getConnection()->rollback();
126
127 1
            return false;
128
        }
129
130 1
        return true;
131
    }
132
133
    /**
134
     * 規格カテゴリを保存する.
135
     *
136
     * @param  \Eccube\Entity\ClassCategory $ClassCategory
137
     * @return boolean 成功した場合 true
138
     */
139 3
    public function save($ClassCategory)
140
    {
141 3
        $em = $this->getEntityManager();
142 3
        $em->getConnection()->beginTransaction();
143
        try {
144 3
            if (!$ClassCategory->getId()) {
145 3
                $ClassName = $ClassCategory->getClassName();
146 3
                $rank = $this->createQueryBuilder('cc')
147 3
                    ->select('MAX(cc.rank)')
148 3
                    ->where('cc.ClassName = :ClassName')->setParameter('ClassName', $ClassName)
149 3
                    ->getQuery()
150 3
                    ->getSingleScalarResult();
151 3
                if (!$rank) {
152 2
                    $rank = 0;
153
                }
154 3
                $ClassCategory->setRank($rank + 1);
155 3
                $ClassCategory->setVisible(true);
156
            }
157
158 3
            $em->persist($ClassCategory);
159 3
            $em->flush();
160
161 2
            $em->getConnection()->commit();
162 1
        } catch (\Exception $e) {
163 1
            $em->getConnection()->rollback();
164
165 1
            return false;
166
        }
167
168 2
        return true;
169
    }
170
171
    /**
172
     * 規格カテゴリを削除する.
173
     *
174
     * @param \Eccube\Entity\ClassCategory $ClassCategory
175
     * @return boolean 成功した場合 true
176
     */
177 3
    public function delete($ClassCategory)
178
    {
179 3
        if (is_null($ClassCategory->getId())) {
180
            // 存在しない場合は何もしない
181 1
            return false;
182
        }
183 2
        $em = $this->getEntityManager();
184 2
        $em->getConnection()->beginTransaction();
185
        try {
186 2
            $rank = $ClassCategory->getRank();
187 2
            $ClassName = $ClassCategory->getClassName();
188
189 2
            $em->createQueryBuilder()
190 2
                ->update('Eccube\Entity\ClassCategory', 'cc')
191 2
                ->set('cc.rank', 'cc.rank - 1')
192 2
                ->where('cc.rank > :rank AND cc.ClassName = :ClassName')
193 2
                ->setParameter('rank', $rank)
194 2
                ->setParameter('ClassName', $ClassName)
195 2
                ->getQuery()
196 2
                ->execute();
197
198 2
            $em->remove($ClassCategory);
199 2
            $em->flush();
200
201 2
            $em->getConnection()->commit();
202
        } catch (\Exception $e) {
203
            $em->getConnection()->rollback();
204
205
            return false;
206
        }
207
208 2
        return true;
209
    }
210
211
    /**
212
     * 規格カテゴリの表示/非表示を切り替える.
213
     *
214
     * @param \Eccube\Entity\ClassCategory $ClassCategory
215
     * @return boolean 成功した場合 true
216
     */
217 3
    public function toggleVisibility($ClassCategory)
218
    {
219 3
        $em = $this->getEntityManager();
220 3
        $em->getConnection()->beginTransaction();
221 3
        if ($ClassCategory->isVisible()) {
222 2
            $ClassCategory->setVisible(false);
223
        } else {
224 1
            $ClassCategory->setVisible(true);
225
        }
226
        try {
227 3
            $em->persist($ClassCategory);
228 3
            $em->flush($ClassCategory);
229
230 3
            $em->getConnection()->commit();
231
        } catch (\Exception $e) {
232
            $em->getConnection()->rollback();
233
234
            return false;
235
        }
236
237 3
        return true;
238
    }
239
}
240