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

MemberRepository::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\Master\Work;
31
use Eccube\Entity\Member;
32
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
33
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
34
use Symfony\Component\Security\Core\User\UserInterface;
35
use Symfony\Component\Security\Core\User\UserProviderInterface;
36
37
/**
38
 * MemberRepository
39
 *
40
 * This class was generated by the Doctrine ORM. Add your own custom
41
 * repository methods below.
42
 *
43
 * @Repository
44
 */
45
class MemberRepository extends AbstractRepository implements UserProviderInterface
46
{
47
    /**
48
     * Loads the user for the given username.
49
     *
50
     * This method must throw UsernameNotFoundException if the user is not
51
     * found.
52
     *
53
     * @param string $username The username
54
     *
55
     * @return UserInterface
56
     *
57
     * @see UsernameNotFoundException
58
     *
59
     * @throws UsernameNotFoundException if the user is not found
60
     */
61 218
    public function loadUserByUsername($username)
62
    {
63 218
        $Member = $this->findOneBy(['login_id' => $username, 'Work' => Work::WORK_ACTIVE_ID]);
64
65 218
        if (!$Member) {
66 1
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
67
        }
68
69 217
        return $Member;
70
    }
71
72
    /**
73
     * Refreshes the user for the account interface.
74
     *
75
     * It is up to the implementation to decide if the user data should be
76
     * totally reloaded (e.g. from the database), or if the UserInterface
77
     * object can just be merged into some internal array of users / identity
78
     * map.
79
     *
80
     * @param UserInterface $user
81
     *
82
     * @return UserInterface
83
     *
84
     * @throws UnsupportedUserException if the account is not supported
85
     */
86 217 View Code Duplication
    public function refreshUser(UserInterface $user)
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...
87
    {
88 217
        if (!$user instanceof Member) {
89 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
90
        }
91
92 216
        return $this->loadUserByUsername($user->getUsername());
93
    }
94
95
    /**
96
     * Whether this provider supports the given user class.
97
     *
98
     * @param string $class
99
     *
100
     * @return bool
101
     */
102 1
    public function supportsClass($class)
103
    {
104 1
        return $class === Member::class;
105
    }
106
107
    /**
108
     * 管理ユーザの表示順を一つ上げる.
109
     *
110
     * @param Member $Member
111
     * @throws \Exception 更新対象のユーザより上位のユーザが存在しない場合.
112
     */
113 4
    public function up(Member $Member)
114
    {
115 4
        $rank = $Member->getRank();
116 4
        $Member2 = $this->findOneBy(array('rank' => $rank + 1));
117
118 4
        if (!$Member2) {
119 2
            throw new \Exception(sprintf('%s より上位の管理ユーザが存在しません.', $Member->getId()));
120
        }
121
122 2
        $Member->setRank($rank + 1);
123 2
        $Member2->setRank($rank);
124
125 2
        $em = $this->getEntityManager();
126 2
        $em->flush([$Member, $Member2]);
127
    }
128
129
    /**
130
     * 管理ユーザの表示順を一つ下げる.
131
     *
132
     * @param Member $Member
133
     * @throws \Exception 更新対象のユーザより下位のユーザが存在しない場合.
134
     */
135 5 View Code Duplication
    public function down(Member $Member)
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...
136
    {
137 5
        $rank = $Member->getRank();
138 5
        $Member2 = $this->findOneBy(array('rank' => $rank - 1));
139
140 5
        if (!$Member2) {
141 2
            throw new \Exception(sprintf('%s より下位の管理ユーザが存在しません.', $Member->getId()));
142
        }
143
144 3
        $Member->setRank($rank - 1);
145 3
        $Member2->setRank($rank);
146
147 3
        $em = $this->getEntityManager();
148 3
        $em->flush([$Member, $Member2]);
149
    }
150
151
    /**
152
     * 管理ユーザを登録します.
153
     *
154
     * @param Member $Member
155
     */
156 230
    public function save($Member)
157
    {
158 230
        if (!$Member->getId()) {
159 230
            $rank = $this->createQueryBuilder('m')
160 230
                ->select('COALESCE(MAX(m.rank), 0)')
161 230
                ->getQuery()
162 230
                ->getSingleScalarResult();
163
            $Member
164 230
                ->setRank($rank + 1);
165
        }
166
167 230
        $em = $this->getEntityManager();
168 230
        $em->persist($Member);
169 230
        $em->flush($Member);
170
    }
171
172
    /**
173
     * 管理ユーザを削除します.
174
     *
175
     * @param Member $Member
176
     *
177
     * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
178
     * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
179
     */
180 3
    public function delete($Member)
181
    {
182 3
        $this->createQueryBuilder('m')
183 3
            ->update()
184 3
            ->set('m.rank', 'm.rank - 1')
185 3
            ->where('m.rank > :rank')
186 3
            ->setParameter('rank', $Member->getRank())
187 3
            ->getQuery()
188 3
            ->execute();
189
190 3
        $em = $this->getEntityManager();
191 3
        $em->remove($Member);
192 3
        $em->flush($Member);
193
    }
194
}
195