Completed
Push — master ( 078f7b...6ad7bc )
by Kentaro
30:09
created

MemberRepository::up()   B

Complexity

Conditions 3
Paths 10

Size

Total Lines 29
Code Lines 18

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
dl 29
loc 29
ccs 6
cts 6
cp 1
rs 8.8571
cc 3
eloc 18
nc 10
nop 1
crap 3
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\ORM\EntityRepository;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Member;
30
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
31
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
32
use Symfony\Component\Security\Core\User\UserInterface;
33
use Symfony\Component\Security\Core\User\UserProviderInterface;
34
use Symfony\Component\Security\Core\Util\SecureRandom;
35
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
36
37
/**
38
 * MemberRepository
39
 *
40
 * This class was generated by the Doctrine ORM. Add your own custom
41
 * repository methods below.
42
 */
43
class MemberRepository extends EntityRepository implements UserProviderInterface
44
{
45
    /**
46
     * @var EncoderFactoryInterface
47
     */
48
    private $encoder_factory;
49
50
    /**
51
     * @param EncoderFactoryInterface $encoder_factory
52
     */
53 240
    public function setEncoderFactorty(EncoderFactoryInterface $encoder_factory)
54
    {
55 240
        $this->encoder_factory = $encoder_factory;
56
    }
57
58
    /**
59
     * Loads the user for the given username.
60
     *
61
     * This method must throw UsernameNotFoundException if the user is not
62
     * found.
63
     *
64
     * @param string $username The username
65
     *
66
     * @return UserInterface
67
     *
68
     * @see UsernameNotFoundException
69
     *
70
     * @throws UsernameNotFoundException if the user is not found
71
     */
72 53
    public function loadUserByUsername($username)
73
    {
74 53
        $Work = $this
75 53
            ->getEntityManager()
76 53
            ->getRepository('Eccube\Entity\Master\Work')
77
            ->find(\Eccube\Entity\Master\Work::WORK_ACTIVE_ID);
78
79 53
        $query = $this->createQueryBuilder('m')
80 53
            ->where('m.login_id = :login_id')
81 53
            ->andWhere('m.Work = :Work')
82
            ->setParameters(array(
83
                    'login_id' => $username,
84
                    'Work' => $Work,
85 53
            ))
86
            ->getQuery();
87
        $Member = $query->getOneOrNullResult();
88 53
        if (!$Member) {
89
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
90
        }
91
92 52
        return $Member;
93
    }
94
95
    /**
96
     * Refreshes the user for the account interface.
97
     *
98
     * It is up to the implementation to decide if the user data should be
99
     * totally reloaded (e.g. from the database), or if the UserInterface
100
     * object can just be merged into some internal array of users / identity
101
     * map.
102
     *
103
     * @param UserInterface $user
104
     *
105
     * @return UserInterface
106
     *
107
     * @throws UnsupportedUserException if the account is not supported
108
     */
109 51 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...
110
    {
111 51
        if (!$user instanceof Member) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\Member does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
112
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
113
        }
114
115
        return $this->loadUserByUsername($user->getUsername());
116 51
    }
117
118
    /**
119
     * Whether this provider supports the given user class.
120
     *
121
     * @param string $class
122
     *
123
     * @return bool
124
     */
125 1
    public function supportsClass($class)
126
    {
127 1
        return $class === 'Eccube\Entity\Member';
128
    }
129
130
    /**
131
     * @param  \Eccube\Entity\Member $Member
132
     *
133
     * @return void
134
     */
135 3 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...
136
    {
137
        $em = $this->getEntityManager();
138
        $em->getConnection()->beginTransaction();
139
        try {
140
            $rank = $Member->getRank();
141
142
            $Member2 = $this->findOneBy(array('rank' => $rank + 1));
143 3
            if (!$Member2) {
144
                throw new \Exception();
145
            }
146
            $Member2->setRank($rank);
147
            $em->persist($Member2);
148
149
            // Member更新
150
            $Member->setRank($rank + 1);
151
152
            $em->persist($Member);
153
            $em->flush();
154
155
            $em->getConnection()->commit();
156
        } catch (\Exception $e) {
157
            $em->getConnection()->rollback();
158
159 2
            return false;
160 3
        }
161
162 1
        return true;
163 3
    }
164
165
    /**
166
     * @param  \Eccube\Entity\Member $Member
167
     * @return bool
168
     */
169 3 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...
170
    {
171
        $em = $this->getEntityManager();
172
        $em->getConnection()->beginTransaction();
173
        try {
174
            $rank = $Member->getRank();
175
176
            //
177
            $Member2 = $this->findOneBy(array('rank' => $rank - 1));
178 3
            if (!$Member2) {
179
                throw new \Exception();
180
            }
181
            $Member2->setRank($rank);
182
            $em->persist($Member2);
183
184
            // Member更新
185
            $Member->setRank($rank - 1);
186
187
            $em->persist($Member);
188
            $em->flush();
189
190
            $em->getConnection()->commit();
191
        } catch (\Exception $e) {
192
            $em->getConnection()->rollback();
193
194 2
            return false;
195 3
        }
196
197 1
        return true;
198 3
    }
199
200
    /**
201
     * @param  \Eccube\Entity\Member $Member
202
     * @return bool
203
     */
204 3 View Code Duplication
    public function save(\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...
205
    {
206
        $em = $this->getEntityManager();
207
        $em->getConnection()->beginTransaction();
208
        try {
209
            if (!$Member->getId()) {
210 3
                $rank = $this->createQueryBuilder('m')
211 3
                    ->select('MAX(m.rank)')
212 3
                    ->getQuery()
213
                    ->getSingleScalarResult();
214 3
                if (!$rank) {
215 1
                    $rank = 0;
216
                }
217
                $Member
218 3
                    ->setRank($rank + 1)
219
                    ->setDelFlg(Constant::DISABLED);
220
            }
221
222
            $em->persist($Member);
223 1
            $em->flush();
224
225
            $em->getConnection()->commit();
226
        } catch (\Exception $e) {
227
            $em->getConnection()->rollback();
228
229 1
            return false;
230 3
        }
231
232 2
        return true;
233 3
    }
234
235
    /**
236
     * @param  \Eccube\Entity\Member $Member
237
     * @return bool
238
     */
239 3 View Code Duplication
    public function delete(\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...
240
    {
241
        $em = $this->getEntityManager();
242
        $em->getConnection()->beginTransaction();
243
        try {
244
            $rank = $Member->getRank();
245 3
            $em->createQueryBuilder()
246 3
                ->update('Eccube\Entity\Member', 'm')
247 3
                ->set('m.rank', 'm.rank - 1')
248
                ->where('m.rank > :rank')->setParameter('rank', $rank)
249 3
                ->getQuery()
250
                ->execute();
251
252
            $Member
253 3
                ->setDelFlg(Constant::ENABLED)
254
                ->setRank(0);
255
256
            $em->persist($Member);
257 1
            $em->flush();
258
259
            $em->getConnection()->commit();
260
        } catch (\Exception $e) {
261
            $em->getConnection()->rollback();
262
263 1
            return false;
264 3
        }
265
266 2
        return true;
267 3
    }
268
269
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$byte" missing
Loading history...
270
     * saltを生成する
271
     *
272
     * @param $byte
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
273
     * @return string
274
     */
275
    public function createSalt($byte)
276
    {
277
        $generator = new SecureRandom();
278
279
        return bin2hex($generator->nextBytes($byte));
280
    }
281
282
    /**
283
     * 入力されたパスワードをSaltと暗号化する
284
     *
285
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
286
     * @param  \Eccube\Entity\Member $Member
0 ignored issues
show
introduced by
Superfluous parameter comment
Loading history...
287
     * @return mixed
288
     */
289
    public function encryptPassword(\Eccube\Entity\Member $Member)
290
    {
291
        $encoder = $this->encoder_factory->getEncoder($Member);
292
293
        return $encoder->encodePassword($Member->getPassword(), $Member->getSalt());
294
    }
295
}
296