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

ClassCategoryRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 19
ccs 14
cts 14
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\ClassCategory;
31
32
/**
33
 * ClasscategoryRepository
34
 *
35
 * This class was generated by the Doctrine ORM. Add your own custom
36
 * repository methods below.
37
 *
38
 * @Repository
39
 */
40
class ClassCategoryRepository extends AbstractRepository
41
{
42
    /**
43
     * 規格カテゴリの一覧を取得します.
44
     *
45
     * @param \Eccube\Entity\ClassName $ClassName 検索対象の規格名オブジェクト. 指定しない場合は、すべての規格を対象に取得します.
46
     * @return array 規格カテゴリの配列
47
     */
48 4
    public function getList(\Eccube\Entity\ClassName $ClassName = null)
49
    {
50 4
        $qb = $this->createQueryBuilder('cc')
51 4
            ->orderBy('cc.rank', 'DESC'); // TODO ClassName ごとにソートした方が良いかも
52 4
        if ($ClassName) {
53 3
            $qb->where('cc.ClassName = :ClassName')->setParameter('ClassName', $ClassName);
54
        }
55 4
        $ClassCategories = $qb->getQuery()
56 4
            ->getResult();
57
58 4
        return $ClassCategories;
59
    }
60
61
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$ClassCategory" missing
Loading history...
62
     * 規格カテゴリを登録します.
63
     *
64
     * @param $ClassCategory
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
65
     */
66 2
    public function save($ClassCategory)
67
    {
68 2
        if (!$ClassCategory->getId()) {
69 2
            $ClassName = $ClassCategory->getClassName();
70 2
            $rank = $this->createQueryBuilder('cc')
71 2
                ->select('COALESCE(MAX(cc.rank), 0)')
72 2
                ->where('cc.ClassName = :ClassName')
73 2
                ->setParameter('ClassName', $ClassName)
74 2
                ->getQuery()
75 2
                ->getSingleScalarResult();
76
77 2
            $ClassCategory->setRank($rank + 1);
78 2
            $ClassCategory->setVisible(true);
79
        }
80
81 2
        $em = $this->getEntityManager();
82 2
        $em->persist($ClassCategory);
83 2
        $em->flush($ClassCategory);
84
    }
85
86
    /**
87
     * 規格カテゴリを削除する.
88
     *
89
     * @param ClassCategory $ClassCategory
90
     *
91
     * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
92
     * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
93
     */
94 3
    public function delete($ClassCategory)
95
    {
96 3
        $this->createQueryBuilder('cc')
97 3
            ->update()
98 3
            ->set('cc.rank', 'cc.rank - 1')
99 3
            ->where('cc.rank > :rank AND cc.ClassName = :ClassName')
100 3
            ->setParameter('rank', $ClassCategory->getRank())
101 3
            ->setParameter('ClassName', $ClassCategory->getClassName())
102 3
            ->getQuery()
103 3
            ->execute();
104
105 3
        $em = $this->getEntityManager();
106 3
        $em->remove($ClassCategory);
107 3
        $em->flush($ClassCategory);
108
    }
109
110
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$ClassCategory" missing
Loading history...
111
     * 規格カテゴリの表示/非表示を切り替える.
112
     *
113
     * @param $ClassCategory
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
114
     */
115 3
    public function toggleVisibility($ClassCategory)
116
    {
117 3
        if ($ClassCategory->isVisible()) {
118 2
            $ClassCategory->setVisible(false);
119
        } else {
120 1
            $ClassCategory->setVisible(true);
121
        }
122
123 3
        $em = $this->getEntityManager();
124 3
        $em->persist($ClassCategory);
125 3
        $em->flush($ClassCategory);
126
    }
127
}
128