Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

NewsRepository::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
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 Doctrine\DBAL\Exception\DriverException;
28
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
29
use Eccube\Annotation\Repository;
30
use Eccube\Entity\News;
31
32
/**
33
 * NewsRepository
34
 *
35
 * This class was generated by the Doctrine ORM. Add your own custom
36
 * repository methods below.
37
 *
38
 * @Repository
39
 */
40
class NewsRepository extends AbstractRepository
41
{
42
    /**
43
     * 新着情報の表示順を1上げる.
44
     *
45
     * @param News $News
46
     * @throws \Exception 更新対象の新着情報より上位の新着情報が存在しない場合.
47
     */
48 3
    public function up(News $News)
49
    {
50 3
        $rank = $News->getRank();
51 3
        $News2 = $this->findOneBy(array('rank' => $rank + 1));
52
53 3
        if (!$News2) {
54 1
            throw new \Exception(sprintf('%s より上位の新着情報が存在しません.', $News->getId()));
55
        }
56
57 2
        $News->setRank($rank + 1);
58 2
        $News2->setRank($rank);
59
60 2
        $em = $this->getEntityManager();
61 2
        $em->flush([$News, $News2]);
62
    }
63
64
    /**
65
     * 新着情報の表示順を1下げる.
66
     *
67
     * @param News $News
68
     * @throws \Exception \Exception 更新対象の新着情報より上位の新着情報が存在しない場合.
69
     */
70 3 View Code Duplication
    public function down(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...
71
    {
72 3
        $rank = $News->getRank();
73 3
        $News2 = $this->findOneBy(array('rank' => $rank - 1));
74
75 3
        if (!$News2) {
76 1
            throw new \Exception();
77
        }
78
79 2
        $News->setRank($rank - 1);
80 2
        $News2->setRank($rank);
81
82 2
        $em = $this->getEntityManager();
83 2
        $em->flush([$News, $News2]);
84
    }
85
86
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$News" missing
Loading history...
87
     * 新着情報を登録します.
88
     *
89
     * @param $News
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
90
     */
91 2
    public function save($News)
92
    {
93 2
        if (!$News->getId()) {
94 2
            $rank = $this->createQueryBuilder('n')
95 2
                ->select('COALESCE(MAX(n.rank), 0)')
96 2
                ->getQuery()
97 2
                ->getSingleScalarResult();
98
            $News
99 2
                ->setRank($rank + 1);
100
        }
101
102 2
        $em = $this->getEntityManager();
103 2
        $em->persist($News);
104 2
        $em->flush($News);
105
    }
106
107
    /**
108
     * 新着情報を削除します.
109
     *
110
     * @param News $News
111
     *
112
     * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
113
     * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
114
     */
115 2
    public function delete($News)
116
    {
117 2
       $this->createQueryBuilder('n')
118 2
            ->update()
119 2
            ->set('n.rank', 'n.rank - 1')
120 2
            ->where('n.rank > :rank')
121 2
            ->setParameter('rank', $News->getRank())
122 2
            ->getQuery()
123 2
            ->execute();
124
125 2
        $em = $this->getEntityManager();
126 2
        $em->remove($News);
127 2
        $em->flush($News);
128
    }
129
}
130