Failed Conditions
Pull Request — experimental/3.1 (#2512)
by chihiro
12:14
created

MemberRepository::save()   B

Complexity

Conditions 4
Paths 16

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 16
nop 1
dl 0
loc 29
ccs 17
cts 18
cp 0.9444
crap 4.0027
rs 8.5806
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 Eccube\Annotation\Repository;
28
use Eccube\Annotation\Inject;
29
use Eccube\Common\Constant;
30
use Eccube\Entity\Member;
31
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
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
     * @var EncoderFactoryInterface
49
     * @Inject("security.encoder_factory")
50
     */
51
    private $encoder_factory;
52
53
    /**
54
     * @param EncoderFactoryInterface $encoder_factory
55
     */
56
    public function setEncoderFactorty(EncoderFactoryInterface $encoder_factory)
57
    {
58
        $this->encoder_factory = $encoder_factory;
59
    }
60
61
    /**
62
     * Loads the user for the given username.
63
     *
64
     * This method must throw UsernameNotFoundException if the user is not
65
     * found.
66
     *
67
     * @param string $username The username
68
     *
69
     * @return UserInterface
70
     *
71
     * @see UsernameNotFoundException
72
     *
73
     * @throws UsernameNotFoundException if the user is not found
74
     */
75 223
    public function loadUserByUsername($username)
76
    {
77
        $Work = $this
78 223
            ->getEntityManager()
79 223
            ->getRepository('Eccube\Entity\Master\Work')
80 223
            ->find(\Eccube\Entity\Master\Work::WORK_ACTIVE_ID);
81
82 223
        $query = $this->createQueryBuilder('m')
83 223
            ->where('m.login_id = :login_id')
84 223
            ->andWhere('m.Work = :Work')
85 223
            ->setParameters(array(
86 223
                    'login_id' => $username,
87 223
                    'Work' => $Work,
88
            ))
89 223
            ->setMaxResults(1)
90 223
            ->getQuery();
91 223
        $Member = $query->getOneOrNullResult();
92 223
        if (!$Member) {
93 1
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
94
        }
95
96 222
        return $Member;
97
    }
98
99
    /**
100
     * Refreshes the user for the account interface.
101
     *
102
     * It is up to the implementation to decide if the user data should be
103
     * totally reloaded (e.g. from the database), or if the UserInterface
104
     * object can just be merged into some internal array of users / identity
105
     * map.
106
     *
107
     * @param UserInterface $user
108
     *
109
     * @return UserInterface
110
     *
111
     * @throws UnsupportedUserException if the account is not supported
112
     */
113 220 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...
114
    {
115 220
        if (!$user instanceof Member) {
116 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
117
        }
118
119 219
        return $this->loadUserByUsername($user->getUsername());
120
    }
121
122
    /**
123
     * Whether this provider supports the given user class.
124
     *
125
     * @param string $class
126
     *
127
     * @return bool
128
     */
129 1
    public function supportsClass($class)
130
    {
131 1
        return $class === 'Eccube\Entity\Member';
132
    }
133
134
    /**
135
     * @param  \Eccube\Entity\Member $Member
136
     *
137
     * @return void
138
     */
139 4 View Code Duplication
    public function up(\Eccube\Entity\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...
140
    {
141 4
        $em = $this->getEntityManager();
142 4
        $em->getConnection()->beginTransaction();
143
        try {
144 4
            $rank = $Member->getRank();
145
146 4
            $Member2 = $this->findOneBy(array('rank' => $rank + 1));
147 4
            if (!$Member2) {
148 2
                throw new \Exception();
149
            }
150 2
            $Member2->setRank($rank);
151 2
            $em->persist($Member2);
152
153
            // Member更新
154 2
            $Member->setRank($rank + 1);
155
156 2
            $em->persist($Member);
157 2
            $em->flush();
158
159 2
            $em->getConnection()->commit();
160 2
        } catch (\Exception $e) {
161 2
            $em->getConnection()->rollback();
162
163 2
            return false;
164
        }
165
166 2
        return true;
167
    }
168
169
    /**
170
     * @param  \Eccube\Entity\Member $Member
171
     * @return bool
172
     */
173 5 View Code Duplication
    public function down(\Eccube\Entity\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...
174
    {
175 5
        $em = $this->getEntityManager();
176 5
        $em->getConnection()->beginTransaction();
177
        try {
178 5
            $rank = $Member->getRank();
179
180
            //
181 5
            $Member2 = $this->findOneBy(array('rank' => $rank - 1));
182 5
            if (!$Member2) {
183 2
                throw new \Exception();
184
            }
185 3
            $Member2->setRank($rank);
186 3
            $em->persist($Member2);
187
188
            // Member更新
189 3
            $Member->setRank($rank - 1);
190
191 3
            $em->persist($Member);
192 3
            $em->flush();
193
194 3
            $em->getConnection()->commit();
195 2
        } catch (\Exception $e) {
196 2
            $em->getConnection()->rollback();
197
198 2
            return false;
199
        }
200
201 3
        return true;
202
    }
203
204
    /**
205
     * @param  \Eccube\Entity\Member $Member
206
     * @return bool
207
     */
208 237
    public function save($Member)
209
    {
210 237
        $em = $this->getEntityManager();
211 237
        $em->getConnection()->beginTransaction();
212
        try {
213 237
            if (!$Member->getId()) {
214 237
                $rank = $this->createQueryBuilder('m')
215 237
                    ->select('MAX(m.rank)')
216 237
                    ->getQuery()
217 237
                    ->getSingleScalarResult();
218 237
                if (!$rank) {
219
                    $rank = 0;
220
                }
221
                $Member
222 237
                    ->setRank($rank + 1);
223
            }
224
225 237
            $em->persist($Member);
226 237
            $em->flush();
227
228 237
            $em->getConnection()->commit();
229 1
        } catch (\Exception $e) {
230 1
            $em->getConnection()->rollback();
231
232 1
            return false;
233
        }
234
235 237
        return true;
236
    }
237
238
    /**
239
     * @param  \Eccube\Entity\Member $Member
240
     * @return bool
241
     */
242 3 View Code Duplication
    public function delete($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...
243
    {
244 3
        $em = $this->getEntityManager();
245 3
        $em->getConnection()->beginTransaction();
246
        try {
247 3
            $rank = $Member->getRank();
248 3
            $em->createQueryBuilder()
249 3
                ->update('Eccube\Entity\Member', 'm')
250 3
                ->set('m.rank', 'm.rank - 1')
251 3
                ->where('m.rank > :rank')->setParameter('rank', $rank)
252 3
                ->getQuery()
253 3
                ->execute();
254
255 3
            $em->remove($Member);
256 3
            $em->flush();
257
258 2
            $em->getConnection()->commit();
259 1
        } catch (\Exception $e) {
260 1
            $em->getConnection()->rollback();
261
262 1
            return false;
263
        }
264
265 2
        return true;
266
    }
267
268
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$byte" missing
Loading history...
269
     * saltを生成する
270
     *
271
     * @param $byte
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
272
     * @return string
273
     */
274 237
    public function createSalt($byte)
275
    {
276 237
        return bin2hex(openssl_random_pseudo_bytes($byte));
277
    }
278
279
    /**
280
     * 入力されたパスワードをSaltと暗号化する
281
     *
282
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
283
     * @param  \Eccube\Entity\Member $Member
0 ignored issues
show
introduced by
Superfluous parameter comment
Loading history...
284
     * @return mixed
285
     */
286 237
    public function encryptPassword(\Eccube\Entity\Member $Member)
287
    {
288 237
        $encoder = $this->encoder_factory->getEncoder($Member);
289
290 237
        return $encoder->encodePassword($Member->getPassword(), $Member->getSalt());
291
    }
292
}
293