Failed Conditions
Push — experimental/sf ( 37b3aa...cc45c1 )
by chihiro
921:05 queued 894:01
created

NewsRepository::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Doctrine\DBAL\Exception\DriverException;
17
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
18
use Eccube\Entity\News;
19
use Symfony\Bridge\Doctrine\RegistryInterface;
20
21
/**
22
 * NewsRepository
23
 *
24
 * This class was generated by the Doctrine ORM. Add your own custom
25
 * repository methods below.
26
 */
27
class NewsRepository extends AbstractRepository
28
{
29 17
    public function __construct(RegistryInterface $registry)
30
    {
31 17
        parent::__construct($registry, News::class);
32
    }
33
34
    /**
35
     * 新着情報の表示順を1上げる.
36
     *
37
     * @param News $News
38
     *
39
     * @throws \Exception 更新対象の新着情報より上位の新着情報が存在しない場合.
40
     */
41 3
    public function up(News $News)
42
    {
43 3
        $sortNo = $News->getSortNo();
44 3
        $News2 = $this->findOneBy(['sort_no' => $sortNo + 1]);
45
46 3
        if (!$News2) {
47 1
            throw new \Exception(sprintf('%s より上位の新着情報が存在しません.', $News->getId()));
48
        }
49
50 2
        $News->setSortNo($sortNo + 1);
51 2
        $News2->setSortNo($sortNo);
52
53 2
        $em = $this->getEntityManager();
54 2
        $em->flush([$News, $News2]);
55
    }
56
57
    /**
58
     * 新着情報の表示順を1下げる.
59
     *
60
     * @param News $News
61
     *
62
     * @throws \Exception \Exception 更新対象の新着情報より上位の新着情報が存在しない場合.
63
     */
64 3
    public function down(News $News)
65
    {
66 3
        $sortNo = $News->getSortNo();
67 3
        $News2 = $this->findOneBy(['sort_no' => $sortNo - 1]);
68
69 3
        if (!$News2) {
70 1
            throw new \Exception();
71
        }
72
73 2
        $News->setSortNo($sortNo - 1);
74 2
        $News2->setSortNo($sortNo);
75
76 2
        $em = $this->getEntityManager();
77 2
        $em->flush([$News, $News2]);
78
    }
79
80
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$News" missing
Loading history...
81
     * 新着情報を登録します.
82
     *
83
     * @param $News
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
84
     */
85 2
    public function save($News)
86
    {
87 2
        if (!$News->getId()) {
88 2
            $sortNo = $this->createQueryBuilder('n')
89 2
                ->select('COALESCE(MAX(n.sort_no), 0)')
90 2
                ->getQuery()
91 2
                ->getSingleScalarResult();
92
            $News
93 2
                ->setSortNo($sortNo + 1);
94
        }
95
96 2
        $em = $this->getEntityManager();
97 2
        $em->persist($News);
98 2
        $em->flush($News);
99
    }
100
101
    /**
102
     * 新着情報を削除します.
103
     *
104
     * @param News $News
105
     *
106
     * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
107
     * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
108
     */
109 2
    public function delete($News)
110
    {
111 2
        $this->createQueryBuilder('n')
112 2
            ->update()
113 2
            ->set('n.sort_no', 'n.sort_no - 1')
114 2
            ->where('n.sort_no > :sort_no')
115 2
            ->setParameter('sort_no', $News->getSortNo())
116 2
            ->getQuery()
117 2
            ->execute();
118
119 2
        $em = $this->getEntityManager();
120 2
        $em->remove($News);
121 2
        $em->flush($News);
122
    }
123
124
    /**
125
     * @return array
126
     */
127 4
    public function getList()
128
    {
129 4
        $qb = $this->createQueryBuilder('n');
130 4
        $qb->where('n.publish_date <= :date')
131 4
            ->setParameter('date', new \DateTime())
132 4
            ->orderBy('n.sort_no', 'DESC');
133
134 4
        return $qb->getQuery()->getResult();
135
    }
136
}
137