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

NewsRepository::delete()   B

Complexity

Conditions 2
Paths 6

Size

Total Lines 24
Code Lines 18

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 2.0219

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 6
nop 1
dl 24
loc 24
ccs 14
cts 17
cp 0.8235
crap 2.0219
rs 8.9713
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
 * NewsRepository
31
 *
32
 * This class was generated by the Doctrine ORM. Add your own custom
33
 * repository methods below.
34
 *
35
 * @Repository
36
 */
37
class NewsRepository extends AbstractRepository
38
{
39
40
    /**
41
     * News の順位を1上げる.
42
     *
43
     * @param  \Eccube\Entity\News $News
44
     * @return boolean 成功した場合 true
45
     */
46 3 View Code Duplication
    public function up(\Eccube\Entity\News $News)
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...
47
    {
48 3
        $em = $this->getEntityManager();
49 3
        $em->getConnection()->beginTransaction();
50
        try {
51 3
            $rank = $News->getRank();
52
53 3
            $News2 = $this->findOneBy(array('rank' => $rank + 1));
54 3
            if (!$News2) {
55 1
                throw new \Exception();
56
            }
57 2
            $News2->setRank($rank);
58 2
            $em->persist($News2);
59
60
            // News更新
61 2
            $News->setRank($rank + 1);
62
63 2
            $em->persist($News);
64 2
            $em->flush();
65
66 2
            $em->getConnection()->commit();
67 1
        } catch (\Exception $e) {
68 1
            $em->getConnection()->rollback();
69
70 1
            return false;
71
        }
72
73 2
        return true;
74
    }
75
76
    /**
77
     * News の順位を1下げる
78
     *
79
     * @param  \Eccube\Entity\News $News
80
     * @return boolean 成功した場合 true
81
     */
82 3 View Code Duplication
    public function down(\Eccube\Entity\News $News)
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...
83
    {
84 3
        $em = $this->getEntityManager();
85 3
        $em->getConnection()->beginTransaction();
86
        try {
87 3
            $rank = $News->getRank();
88 3
            $News2 = $this->findOneBy(array('rank' => $rank - 1));
89 3
            if (!$News2) {
90 1
                throw new \Exception();
91
            }
92 2
            $News2->setRank($rank);
93 2
            $em->persist($News2);
94
95
            // News更新
96 2
            $News->setRank($rank - 1);
97
98 2
            $em->persist($News);
99 2
            $em->flush();
100
101 2
            $em->getConnection()->commit();
102 1
        } catch (\Exception $e) {
103 1
            $em->getConnection()->rollback();
104
105 1
            return false;
106
        }
107
108 2
        return true;
109
    }
110
111
    /**
112
     * News を保存する.
113
     *
114
     * @param  \Eccube\Entity\News $News
115
     * @return boolean 成功した場合 true
116
     */
117 3
    public function save($News)
118
    {
119 3
        $em = $this->getEntityManager();
120 3
        $em->getConnection()->beginTransaction();
121
        try {
122 3
            if (!$News->getId()) {
123 3
                $rank = $this->createQueryBuilder('n')
124 3
                ->select('MAX(n.rank)')
125 3
                ->getQuery()
126 3
                ->getSingleScalarResult();
127 3
                if (!$rank) {
128 1
                    $rank = 0;
129
                }
130
                $News
131 3
                    ->setRank($rank + 1);
132
            }
133
134 3
            $em->persist($News);
135 3
            $em->flush();
136 2
            $em->getConnection()->commit();
137 1
        } catch (\Exception $e) {
138 1
            $em->getConnection()->rollback();
139 1
            return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
140
        }
141
142 2
        return true;
143
    }
144
145
    /**
146
     * News を削除する.
147
     *
148
     * @param  \Eccube\Entity\News $News
149
     * @return boolean 成功した場合 true
150
     */
151 2 View Code Duplication
    public function delete($News)
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...
152
    {
153 2
        $em = $this->getEntityManager();
154 2
        $em->getConnection()->beginTransaction();
155
        try {
156 2
            $rank = $News->getRank();
157 2
            $em->createQueryBuilder()
158 2
                ->update('Eccube\Entity\News', 'n')
159 2
                ->set('n.rank', 'n.rank - 1')
160 2
                ->where('n.rank > :rank')->setParameter('rank', $rank)
161 2
                ->getQuery()
162 2
                ->execute();
163
164 2
            $em->remove($News);
165 2
            $em->flush();
166
167 2
            $em->getConnection()->commit();
168
        } catch (\Exception $e) {
169
            $em->getConnection()->rollback();
170
            return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
171
        }
172
173 2
        return true;
174
    }
175
}
176