Completed
Pull Request — master (#2094)
by
unknown
52:53 queued 16:17
created

CustomerRepository::encryptPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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\Customer;
30
use Eccube\Entity\Master\CustomerStatus;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Eccube\Util\Str;
34
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
35
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
36
use Symfony\Component\Security\Core\User\UserInterface;
37
use Symfony\Component\Security\Core\User\UserProviderInterface;
38
use Symfony\Component\Security\Core\Util\SecureRandom;
39
40
/**
41
 * CustomerRepository
42
 *
43
 * This class was generated by the Doctrine ORM. Add your own custom
44
 * repository methods below.
45
 */
46
class CustomerRepository extends EntityRepository implements UserProviderInterface
47
{
48
    protected $app;
49
50
    public function setApplication($app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
51
    {
52
        $this->app = $app;
53
    }
54
55 11
    public function newCustomer()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
56
    {
57 11
        $Customer = new \Eccube\Entity\Customer();
58 11
        $Status = $this->getEntityManager()
59 11
            ->getRepository('Eccube\Entity\Master\CustomerStatus')
60 11
            ->find(1);
61
62
        $Customer
63 11
            ->setStatus($Status)
64 11
            ->setDelFlg(0);
65
66 11
        return $Customer;
67
    }
68
69
    /**
70
     * Loads the user for the given username.
71
     *
72
     * This method must throw UsernameNotFoundException if the user is not
73
     * found.
74
     *
75
     * @param string $username The username
76
     *
77
     * @return UserInterface
78
     *
79
     * @see UsernameNotFoundException
80
     *
81
     * @throws UsernameNotFoundException if the user is not found
82
     */
83 87
    public function loadUserByUsername($username)
84
    {
85
        // 本会員ステータスの会員のみ有効.
86
        $CustomerStatus = $this
87 87
            ->getEntityManager()
88 87
            ->getRepository('Eccube\Entity\Master\CustomerStatus')
89 87
            ->find(CustomerStatus::ACTIVE);
90
91 87
        $query = $this->createQueryBuilder('c')
92 87
            ->where('c.email = :email')
93 87
            ->andWhere('c.del_flg = :delFlg')
94 87
            ->andWhere('c.Status =:CustomerStatus')
95 87
            ->setParameters(array(
96 87
                'email' => $username,
97 87
                'delFlg' => Constant::DISABLED,
98 87
                'CustomerStatus' => $CustomerStatus,
99
            ))
100 87
            ->setMaxResults(1)
101 87
            ->getQuery();
102 87
        $Customer = $query->getOneOrNullResult();
103 87
        if (!$Customer) {
104 1
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
105
        }
106
107 86
        return $Customer;
108
    }
109
110
    /**
111
     * Refreshes the user for the account interface.
112
     *
113
     * It is up to the implementation to decide if the user data should be
114
     * totally reloaded (e.g. from the database), or if the UserInterface
115
     * object can just be merged into some internal array of users / identity
116
     * map.
117
     *
118
     * @param UserInterface $user
119
     *
120
     * @return UserInterface
121
     *
122
     * @throws UnsupportedUserException if the account is not supported
123
     */
124 84 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...
125
    {
126 84
        if (!$user instanceof Customer) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\Customer 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...
127 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
128
        }
129
130 83
        return $this->loadUserByUsername($user->getUsername());
131
    }
132
133
    /**
134
     * Whether this provider supports the given user class.
135
     *
136
     * @param string $class
137
     *
138
     * @return bool
139
     */
140 1
    public function supportsClass($class)
141
    {
142 1
        return $class === 'Eccube\Entity\Customer';
143
    }
144
145 42
    public function getQueryBuilderBySearchData($searchData)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
146
    {
147 42
        $qb = $this->createQueryBuilder('c')
148 42
            ->select('c')
149 42
            ->andWhere('c.del_flg = 0');
150
151 42
        if (isset($searchData['multi']) && Str::isNotBlank($searchData['multi'])) {
152
            //スペース除去
153 18
            $clean_key_multi = preg_replace('/\s+|[ ]+/u', '', $searchData['multi']);
154 18
            $id = preg_match('/^\d+$/', $clean_key_multi) ? $clean_key_multi : null;
155
            $qb
156 18
                ->andWhere('c.id = :customer_id OR CONCAT(c.name01, c.name02) LIKE :name OR CONCAT(c.kana01, c.kana02) LIKE :kana OR c.email LIKE :email')
157 18
                ->setParameter('customer_id', $id)
158 18
                ->setParameter('name', '%' . $clean_key_multi . '%')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
159 18
                ->setParameter('kana', '%' . $clean_key_multi . '%')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
160 18
                ->setParameter('email', '%' . $clean_key_multi . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
161
        }
162
163
        // Pref
164 42 View Code Duplication
        if (!empty($searchData['pref']) && $searchData['pref']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
165
            $qb
166 1
                ->andWhere('c.Pref = :pref')
167 1
                ->setParameter('pref', $searchData['pref']->getId());
168
        }
169
170
        // sex
171 42
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
172 2
            $sexs = array();
173 2
            foreach ($searchData['sex'] as $sex) {
174 2
                $sexs[] = $sex->getId();
175
            }
176
177
            $qb
178 2
                ->andWhere($qb->expr()->in('c.Sex', ':sexs'))
179 2
                ->setParameter('sexs', $sexs);
180
        }
181
182 42 View Code Duplication
        if (!empty($searchData['birth_month']) && $searchData['birth_month']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
183
            $qb
184 1
                ->andWhere('EXTRACT(MONTH FROM c.birth) = :birth_month')
185 1
                ->setParameter('birth_month', $searchData['birth_month']);
186
        }
187
188
        // birth
189 42 View Code Duplication
        if (!empty($searchData['birth_start']) && $searchData['birth_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
190 2
            $date = $searchData['birth_start']
191 2
                ->format('Y-m-d H:i:s');
192
            $qb
193 2
                ->andWhere('c.birth >= :birth_start')
194 2
                ->setParameter('birth_start', $date);
195
        }
196 42 View Code Duplication
        if (!empty($searchData['birth_end']) && $searchData['birth_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
197 2
            $date = clone $searchData['birth_end'];
198
            $date = $date
199 2
                ->modify('+1 days')
200 2
                ->format('Y-m-d H:i:s');
201
            $qb
202 2
                ->andWhere('c.birth < :birth_end')
203 2
                ->setParameter('birth_end', $date);
204
        }
205
206
        // tel
207 42 View Code Duplication
        if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
208
            $qb
209 1
                ->andWhere('CONCAT(c.tel01, c.tel02, c.tel03) LIKE :tel')
210 1
                ->setParameter('tel', '%' . $searchData['tel'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
211
        }
212
213
        // buy_total
214 42 View Code Duplication
        if (isset($searchData['buy_total_start']) && Str::isNotBlank($searchData['buy_total_start'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
215
            $qb
216 1
                ->andWhere('c.buy_total >= :buy_total_start')
217 1
                ->setParameter('buy_total_start', $searchData['buy_total_start']);
218
        }
219 42 View Code Duplication
        if (isset($searchData['buy_total_end']) && Str::isNotBlank($searchData['buy_total_end'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
220
            $qb
221 1
                ->andWhere('c.buy_total <= :buy_total_end')
222 1
                ->setParameter('buy_total_end', $searchData['buy_total_end']);
223
        }
224
225
        // buy_times
226 42
        if (!empty($searchData['buy_times_start']) && $searchData['buy_times_start']) {
227
            $qb
228 1
                ->andWhere('c.buy_times >= :buy_times_start')
229 1
                ->setParameter('buy_times_start', $searchData['buy_times_start']);
230
        }
231 42
        if (!empty($searchData['buy_times_end']) && $searchData['buy_times_end']) {
232
            $qb
233 1
                ->andWhere('c.buy_times <= :buy_times_end')
234 1
                ->setParameter('buy_times_end', $searchData['buy_times_end']);
235
        }
236
237
        // create_date
238 42 View Code Duplication
        if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
239 1
            $date = $searchData['create_date_start']
240 1
                ->format('Y-m-d H:i:s');
241
            $qb
242 1
                ->andWhere('c.create_date >= :create_date_start')
243 1
                ->setParameter('create_date_start', $date);
244
        }
245 42 View Code Duplication
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
246 1
            $date = clone $searchData['create_date_end'];
247
            $date = $date
248 1
                ->modify('+1 days')
249 1
                ->format('Y-m-d H:i:s');
250
            $qb
251 1
                ->andWhere('c.create_date < :create_date_end')
252 1
                ->setParameter('create_date_end', $date);
253
        }
254
255
        // update_date
256 42 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
257 1
            $date = $searchData['update_date_start']
258 1
                ->format('Y-m-d H:i:s');
259
            $qb
260 1
                ->andWhere('c.update_date >= :update_date_start')
261 1
                ->setParameter('update_date_start', $date);
262
        }
263 42 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
264 1
            $date = clone $searchData['update_date_end'];
265
            $date = $date
266 1
                ->modify('+1 days')
267 1
                ->format('Y-m-d H:i:s');
268
            $qb
269 1
                ->andWhere('c.update_date < :update_date_end')
270 1
                ->setParameter('update_date_end', $date);
271
        }
272
273
        // last_buy
274 42 View Code Duplication
        if (!empty($searchData['last_buy_start']) && $searchData['last_buy_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
275 1
            $date = $searchData['last_buy_start']
276 1
                ->format('Y-m-d H:i:s');
277
            $qb
278 1
                ->andWhere('c.last_buy_date >= :last_buy_start')
279 1
                ->setParameter('last_buy_start', $date);
280
        }
281 42 View Code Duplication
        if (!empty($searchData['last_buy_end']) && $searchData['last_buy_end']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
282 1
            $date = clone $searchData['last_buy_end'];
283
            $date = $date
284 1
                ->modify('+1 days')
285 1
                ->format('Y-m-d H:i:s');
286
            $qb
287 1
                ->andWhere('c.last_buy_date < :last_buy_end')
288 1
                ->setParameter('last_buy_end', $date);
289
        }
290
291
        // status
292 42
        if (!empty($searchData['customer_status']) && count($searchData['customer_status']) > 0) {
293
            $qb
294 2
                ->andWhere($qb->expr()->in('c.Status', ':statuses'))
295 2
                ->setParameter('statuses', $searchData['customer_status']);
296
        }
297
298
        // buy_product_name、buy_product_code
299 42 View Code Duplication
        if (isset($searchData['buy_product_code']) && Str::isNotBlank($searchData['buy_product_code'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
300
            $qb
301 1
                ->leftJoin('c.Orders', 'o')
302 1
                ->leftJoin('o.OrderDetails', 'od')
303 1
                ->andWhere('od.product_name LIKE :buy_product_name OR od.product_code LIKE :buy_product_name')
304 1
                ->setParameter('buy_product_name', '%' . $searchData['buy_product_code'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
305
        }
306
307
        // Order By
308 42
        $qb->addOrderBy('c.update_date', 'DESC');
309
310 42
        return $qb;
311
    }
312
313
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
314
     * ユニークなシークレットキーを返す
315
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
316
     * @return string
317
     */
318 322 View Code Duplication
    public function getUniqueSecretKey($app)
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...
319
    {
320 322
        $unique = Str::random(32);
321 322
        $Customer = $app['eccube.repository.customer']->findBy(array(
322 322
            'secret_key' => $unique,
323
        ));
324 322
        if (count($Customer) == 0) {
325 322
            return $unique;
326
        } else {
327
            return $this->getUniqueSecretKey($app);
328
        }
329
    }
330
331
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
332
     * ユニークなパスワードリセットキーを返す
333
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
334
     * @return string
335
     */
336 2 View Code Duplication
    public function getUniqueResetKey($app)
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...
337
    {
338 2
        $unique = Str::random(32);
339 2
        $Customer = $app['eccube.repository.customer']->findBy(array(
340 2
                        'reset_key' => $unique,
341
        ));
342 2
        if (count($Customer) == 0) {
343 2
            return $unique;
344
        } else {
345
            return $this->getUniqueResetKey($app);
346
        }
347
    }
348
349
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$byte" missing
Loading history...
350
     * saltを生成する
351
     *
352
     * @param $byte
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
353
     * @return string
354
     */
355 322
    public function createSalt($byte)
356
    {
357 322
        $generator = new SecureRandom();
358
359 322
        return bin2hex($generator->nextBytes($byte));
360
    }
361
362
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
363
     * 入力されたパスワードをSaltと暗号化する
364
     *
365
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
366
     * @param  Customer $Customer
367
     * @return mixed
368
     */
369 322
    public function encryptPassword($app, \Eccube\Entity\Customer $Customer)
370
    {
371 322
        $encoder = $app['security.encoder_factory']->getEncoder($Customer);
372
373 322
        return $encoder->encodePassword($Customer->getPassword(), $Customer->getSalt());
374
    }
375
376 5
    public function getNonActiveCustomerBySecretKey($secret_key)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
377
    {
378 5
        $qb = $this->createQueryBuilder('c')
379 5
            ->where('c.del_flg = 0 AND c.secret_key = :secret_key')
380 5
            ->leftJoin('c.Status', 's')
381 5
            ->andWhere('s.id = :status')
382 5
            ->setParameter('secret_key', $secret_key)
383 5
            ->setParameter('status', CustomerStatus::NONACTIVE);
384 5
        $query = $qb->getQuery();
385
386 5
        return $query->getSingleResult();
387
    }
388
389 3
    public function getActiveCustomerByEmail($email)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
390
    {
391 3
        $query = $this->createQueryBuilder('c')
392 3
            ->where('c.email = :email AND c.Status = :status')
393 3
            ->setParameter('email', $email)
394 3
            ->setParameter('status', CustomerStatus::ACTIVE)
395 3
            ->setMaxResults(1)
396 3
            ->getQuery();
397
398 3
        $Customer = $query->getOneOrNullResult();
399
400 3
        return $Customer;
401
    }
402
403 5
    public function getActiveCustomerByResetKey($reset_key)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
404
    {
405 5
        $query = $this->createQueryBuilder('c')
406 5
            ->where('c.reset_key = :reset_key AND c.Status = :status AND c.reset_expire >= :reset_expire')
407 5
            ->setParameter('reset_key', $reset_key)
408 5
            ->setParameter('status', CustomerStatus::ACTIVE)
409 5
            ->setParameter('reset_expire', new \DateTime())
410 5
            ->getQuery();
411
412 5
        $Customer = $query->getSingleResult();
413
414 3
        return $Customer;
415
    }
416
417 4
    public function getResetPassword()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
418
    {
419 4
        return Str::random(8);
420
    }
421
422
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$orderStatusId" missing
Loading history...
introduced by
Doc comment for parameter "$app" missing
Loading history...
423
     * 会員の初回購入時間、購入時間、購入回数、購入金額を更新する
424
     *
425
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
426
     * @param  Customer $Customer
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
427
     * @param  $orderStatusId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
428
     */
429 16
    public function updateBuyData($app, Customer $Customer, $orderStatusId)
430
    {
431
        // 会員の場合、初回購入時間・購入時間・購入回数・購入金額を更新
432
433
        $countOrderStatuses = array(
434 16
            $app['config']['order_new'],
435 16
            $app['config']['order_pay_wait'],
436 16
            $app['config']['order_back_order'],
437 16
            $app['config']['order_deliv'],
438 16
            $app['config']['order_pre_end'],
439
        );
440
441
        $excludeOrderStatuses = array(
442 16
            $app['config']['order_cancel'],
443 16
            $app['config']['order_pending'],
444 16
            $app['config']['order_processing'],
445
        );
446
447 16
        $event = new EventArgs(compact('countOrderStatuses', 'excludeOrderStatuses'));
448 16
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::UPDATE_BUY_CUSTOMER_INITIALIZE, $event);
449 16
        $countOrderStatuses = $event->getArgument('countOrderStatuses');
450 16
        $excludeOrderStatuses = $event->getArgument('excludeOrderStatuses');
451
452 16
        $result = $app['eccube.repository.order']->getCustomerCount($Customer, $countOrderStatuses);
453
454 16
        if (!empty($result)) {
455 12
            $data = $result[0];
456
457 12
            $now = new \DateTime();
458
459 12
            $firstBuyDate = $Customer->getFirstBuyDate();
460 12
            if (empty($firstBuyDate)) {
461 12
                $Customer->setFirstBuyDate($now);
462
            }
463
464 12
            $lastBuyDate = $Customer->getLastBuyDate();
465 12
            $Customer->setLastBuyDate($now);
466 12
            foreach ($excludeOrderStatuses as $excludeOrderStatus) {
467 12
                if ((int)$orderStatusId === (int)$excludeOrderStatus) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
468 12
                    $Customer->setLastBuyDate($lastBuyDate);
469
                }
470
            }
471
472 12
            $Customer->setBuyTimes($data['buy_times']);
473 12
            $Customer->setBuyTotal($data['buy_total']);
474
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
475
        } else {
476
            // 受注データが存在しなければ初期化
477 5
            $Customer->setFirstBuyDate(null);
478 5
            $Customer->setLastBuyDate(null);
479 5
            $Customer->setBuyTimes(0);
480 5
            $Customer->setBuyTotal(0);
481
        }
482
483 16
        $app['orm.em']->persist($Customer);
484 16
        $app['orm.em']->flush();
485
    }
486
}
487