Failed Conditions
Push — experimental/3.1 ( 94d0cd...3ded70 )
by chihiro
23s
created

ClassNameRepository::delete()   B

Complexity

Conditions 4
Paths 9

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.1106

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 9
nop 1
dl 0
loc 33
ccs 17
cts 21
cp 0.8095
crap 4.1106
rs 8.5806
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
 * ClassNameRepository
31
 *
32
 * This class was generated by the Doctrine ORM. Add your own custom
33
 * repository methods below.
34
 *
35
 * @Repository
36
 */
37
class ClassNameRepository extends AbstractRepository
38
{
39
    /**
40
     * 規格一覧を取得する.
41
     *
42
     * @return array 規格の配列
43
     */
44 3
    public function getList()
45
    {
46 3
        $qb = $this->createQueryBuilder('cn')
47 3
            ->orderBy('cn.rank', 'DESC');
48 3
        $ClassNames = $qb->getQuery()
49 3
            ->getResult();
50
51 3
        return $ClassNames;
52
    }
53
54
    /**
55
     * 規格の順位を1上げる.
56
     *
57
     * @param  \Eccube\Entity\ClassName $ClassName
58
     * @return boolean 成功した場合 true
59
     */
60 2 View Code Duplication
    public function up(\Eccube\Entity\ClassName $ClassName)
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...
61
    {
62 2
        $em = $this->getEntityManager();
63 2
        $em->getConnection()->beginTransaction();
64
        try {
65 2
            $rank = $ClassName->getRank();
66
67
            //
68 2
            $ClassName2 = $this->findOneBy(array('rank' => $rank + 1));
69 2
            if (!$ClassName2) {
70 1
                throw new \Exception();
71
            }
72 1
            $ClassName2->setRank($rank);
73 1
            $em->persist($ClassName);
74
75
            // ClassName更新
76 1
            $ClassName->setRank($rank + 1);
77
78 1
            $em->persist($ClassName);
79 1
            $em->flush();
80
81 1
            $em->getConnection()->commit();
82 1
        } catch (\Exception $e) {
83 1
            $em->getConnection()->rollback();
84
85 1
            return false;
86
        }
87
88 1
        return true;
89
    }
90
91
    /**
92
     * 規格の順位を1下げる.
93
     *
94
     * @param \Eccube\Entity\ClassName $ClassName
95
     * @return boolean 成功した場合 true
96
     */
97 2 View Code Duplication
    public function down(\Eccube\Entity\ClassName $ClassName)
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...
98
    {
99 2
        $em = $this->getEntityManager();
100 2
        $em->getConnection()->beginTransaction();
101
        try {
102 2
            $rank = $ClassName->getRank();
103
104
            //
105 2
            $ClassName2 = $this->findOneBy(array('rank' => $rank - 1));
106 2
            if (!$ClassName2) {
107 1
                throw new \Exception();
108
            }
109 1
            $ClassName2->setRank($rank);
110 1
            $em->persist($ClassName);
111
112
            // ClassName更新
113 1
            $ClassName->setRank($rank - 1);
114
115 1
            $em->persist($ClassName);
116 1
            $em->flush();
117
118 1
            $em->getConnection()->commit();
119 1
        } catch (\Exception $e) {
120 1
            $em->getConnection()->rollback();
121
122 1
            return false;
123
        }
124
125 1
        return true;
126
    }
127
128
    /**
129
     * 規格を保存する.
130
     *
131
     * @param \Eccube\Entity\ClassName $ClassName
132
     * @return boolean 成功した場合 true
133
     */
134 5
    public function save($ClassName)
135
    {
136 5
        $em = $this->getEntityManager();
137 5
        $em->getConnection()->beginTransaction();
138
        try {
139 5
            if (!$ClassName->getId()) {
140 5
                $rank = $this->createQueryBuilder('cn')
141 5
                    ->select('MAX(cn.rank)')
142 5
                    ->getQuery()
143 5
                    ->getSingleScalarResult();
144 5
                if (!$rank) {
145 2
                    $rank = 0;
146
                }
147 5
                $ClassName->setRank($rank + 1);
148
            }
149
150 5
            $em->persist($ClassName);
151 5
            $em->flush();
152
153 4
            $em->getConnection()->commit();
154 1
        } catch (\Exception $e) {
155 1
            $em->getConnection()->rollback();
156
157 1
            return false;
158
        }
159
160 4
        return true;
161
    }
162
163
    /**
164
     * 規格を削除する.
165
     *
166
     * @param \Eccube\Entity\ClassName $ClassName
167
     * @return boolean 成功した場合 true
168
     */
169 3
    public function delete($ClassName)
170
    {
171 3
        if (is_null($ClassName->getId())) {
172
            // 存在しない場合は何もしない
173 1
            return false;
174
        }
175 2
        $em = $this->getEntityManager();
176 2
        $em->getConnection()->beginTransaction();
177
        try {
178 2
            if ($ClassName->getClassCategories()->count() > 0) {
179
                throw new \Exception();
180
            }
181
182 2
            $rank = $ClassName->getRank();
183 2
            $em->createQueryBuilder()
184 2
                ->update('Eccube\Entity\ClassName', 'cn')
185 2
                ->set('cn.rank', 'cn.rank - 1')
186 2
                ->where('cn.rank > :rank')->setParameter('rank', $rank)
187 2
                ->getQuery()
188 2
                ->execute();
189
190 2
            $em->remove($ClassName);
191 2
            $em->flush();
192
193 2
            $em->getConnection()->commit();
194
        } catch (\Exception $e) {
195
            $em->getConnection()->rollback();
196
197
            return false;
198
        }
199
200 2
        return true;
201
    }
202
}
203