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

ClassNameRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 14
ccs 10
cts 10
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\ClassName;
31
32
/**
33
 * ClassNameRepository
34
 *
35
 * This class was generated by the Doctrine ORM. Add your own custom
36
 * repository methods below.
37
 *
38
 * @Repository
39
 */
40
class ClassNameRepository extends AbstractRepository
41
{
42
    /**
43
     * 規格一覧を取得する.
44
     *
45
     * @return array 規格の配列
46
     */
47 3
    public function getList()
48
    {
49 3
        $qb = $this->createQueryBuilder('cn')
50 3
            ->orderBy('cn.rank', 'DESC');
51 3
        $ClassNames = $qb->getQuery()
52 3
            ->getResult();
53
54 3
        return $ClassNames;
55
    }
56
57
    /**
58
     * 規格を保存する.
59
     *
60
     * @param ClassName $ClassName
61
     */
62 4
    public function save($ClassName)
63
    {
64 4
        if (!$ClassName->getId()) {
65 4
            $rank = $this->createQueryBuilder('cn')
66 4
                ->select('COALESCE(MAX(cn.rank), 0)')
67 4
                ->getQuery()
68 4
                ->getSingleScalarResult();
69 4
            $ClassName->setRank($rank + 1);
70
        }
71
72 4
        $em = $this->getEntityManager();
73 4
        $em->persist($ClassName);
74 4
        $em->flush([$ClassName]);
75
    }
76
77
    /**
78
     * 規格を削除する.
79
     *
80
     * @param ClassName $ClassName
81
     *
82
     * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
83
     * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
84
     */
85 3
    public function delete($ClassName)
86
    {
87 3
        $rank = $ClassName->getRank();
88 3
        $this->createQueryBuilder('cn')
89 3
            ->update()
90 3
            ->set('cn.rank', 'cn.rank - 1')
91 3
            ->where('cn.rank > :rank')
92 3
            ->setParameter('rank', $rank)
93 3
            ->getQuery()
94 3
            ->execute();
95
96 3
        $em = $this->getEntityManager();
97 3
        $em->remove($ClassName);
98 3
        $em->flush($ClassName);
99
    }
100
}
101