Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CustomerRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CustomerRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class CustomerRepository extends EntityRepository implements UserProviderInterface |
||
| 45 | { |
||
| 46 | protected $app; |
||
| 47 | |||
| 48 | public function setApplication($app) |
||
| 52 | |||
| 53 | 6 | public function newCustomer() |
|
| 54 | { |
||
| 55 | $Customer = new \Eccube\Entity\Customer(); |
||
| 56 | 6 | $Status = $this->getEntityManager() |
|
| 57 | 6 | ->getRepository('Eccube\Entity\Master\CustomerStatus') |
|
| 58 | ->find(1); |
||
| 59 | |||
| 60 | $Customer |
||
| 61 | 6 | ->setStatus($Status) |
|
| 62 | ->setDelFlg(0); |
||
| 63 | |||
| 64 | 6 | return $Customer; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Loads the user for the given username. |
||
| 69 | * |
||
| 70 | * This method must throw UsernameNotFoundException if the user is not |
||
| 71 | * found. |
||
| 72 | * |
||
| 73 | * @param string $username The username |
||
| 74 | * |
||
| 75 | * @return UserInterface |
||
| 76 | * |
||
| 77 | * @see UsernameNotFoundException |
||
| 78 | * |
||
| 79 | * @throws UsernameNotFoundException if the user is not found |
||
| 80 | */ |
||
| 81 | 27 | public function loadUserByUsername($username) |
|
| 82 | { |
||
| 83 | // 本会員ステータスの会員のみ有効. |
||
| 84 | 27 | $CustomerStatus = $this |
|
| 85 | 27 | ->getEntityManager() |
|
| 86 | 27 | ->getRepository('Eccube\Entity\Master\CustomerStatus') |
|
| 87 | ->find(CustomerStatus::ACTIVE); |
||
| 88 | |||
| 89 | 27 | $query = $this->createQueryBuilder('c') |
|
| 90 | 27 | ->where('c.email = :email') |
|
| 91 | 27 | ->andWhere('c.del_flg = :delFlg') |
|
| 92 | 27 | ->andWhere('c.Status =:CustomerStatus') |
|
| 93 | ->setParameters(array( |
||
| 94 | 'email' => $username, |
||
| 95 | 27 | 'delFlg' => Constant::DISABLED, |
|
| 96 | 'CustomerStatus' => $CustomerStatus, |
||
| 97 | )) |
||
| 98 | ->getQuery(); |
||
| 99 | $Customer = $query->getOneOrNullResult(); |
||
| 100 | 27 | if (!$Customer) { |
|
| 101 | throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
||
| 102 | } |
||
| 103 | |||
| 104 | 26 | return $Customer; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Refreshes the user for the account interface. |
||
| 109 | * |
||
| 110 | * It is up to the implementation to decide if the user data should be |
||
| 111 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
| 112 | * object can just be merged into some internal array of users / identity |
||
| 113 | * map. |
||
| 114 | * |
||
| 115 | * @param UserInterface $user |
||
| 116 | * |
||
| 117 | * @return UserInterface |
||
| 118 | * |
||
| 119 | * @throws UnsupportedUserException if the account is not supported |
||
| 120 | */ |
||
| 121 | 25 | View Code Duplication | public function refreshUser(UserInterface $user) |
| 122 | { |
||
| 123 | 25 | if (!$user instanceof Customer) { |
|
| 124 | throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $this->loadUserByUsername($user->getUsername()); |
||
| 128 | 25 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Whether this provider supports the given user class. |
||
| 132 | * |
||
| 133 | * @param string $class |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | 1 | public function supportsClass($class) |
|
| 141 | |||
| 142 | 33 | public function getQueryBuilderBySearchData($searchData) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * ユニークなシークレットキーを返す |
||
| 318 | * @param $app |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | 114 | View Code Duplication | public function getUniqueSecretKey($app) |
| 322 | { |
||
| 323 | $unique = Str::random(32); |
||
| 324 | $Customer = $app['eccube.repository.customer']->findBy(array( |
||
| 325 | 'secret_key' => $unique, |
||
| 326 | )); |
||
| 327 | if (count($Customer) == 0) { |
||
| 328 | 114 | return $unique; |
|
| 329 | } else { |
||
| 330 | return $this->getUniqueSecretKey($app); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * ユニークなパスワードリセットキーを返す |
||
| 336 | * @param $app |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | 1 | View Code Duplication | public function getUniqueResetKey($app) |
| 340 | { |
||
| 341 | $unique = Str::random(32); |
||
| 342 | $Customer = $app['eccube.repository.customer']->findBy(array( |
||
| 343 | 'reset_key' => $unique, |
||
| 344 | )); |
||
| 345 | if (count($Customer) == 0) { |
||
| 346 | 1 | return $unique; |
|
| 347 | } else { |
||
| 348 | return $this->getUniqueResetKey($app); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * saltを生成する |
||
| 354 | * |
||
| 355 | * @param $byte |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function createSalt($byte) |
||
| 359 | { |
||
| 360 | $generator = new SecureRandom(); |
||
| 361 | |||
| 362 | return bin2hex($generator->nextBytes($byte)); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * 入力されたパスワードをSaltと暗号化する |
||
| 367 | * |
||
| 368 | * @param $app |
||
| 369 | * @param Customer $Customer |
||
| 370 | * @return mixed |
||
| 371 | */ |
||
| 372 | 110 | public function encryptPassword($app, \Eccube\Entity\Customer $Customer) |
|
| 373 | { |
||
| 374 | $encoder = $app['security.encoder_factory']->getEncoder($Customer); |
||
| 375 | |||
| 376 | return $encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()); |
||
| 377 | 110 | } |
|
| 378 | |||
| 379 | 4 | public function getNonActiveCustomerBySecretKey($secret_key) |
|
| 380 | { |
||
| 381 | 4 | $qb = $this->createQueryBuilder('c') |
|
| 382 | 4 | ->where('c.del_flg = 0 AND c.secret_key = :secret_key') |
|
| 383 | 4 | ->leftJoin('c.Status', 's') |
|
| 384 | 4 | ->andWhere('s.id = :status') |
|
| 385 | 4 | ->setParameter('secret_key', $secret_key) |
|
| 386 | ->setParameter('status', CustomerStatus::NONACTIVE); |
||
| 387 | $query = $qb->getQuery(); |
||
| 388 | |||
| 389 | 2 | return $query->getSingleResult(); |
|
| 390 | } |
||
| 391 | |||
| 392 | 2 | public function getActiveCustomerByEmail($email) |
|
| 393 | { |
||
| 394 | 2 | $query = $this->createQueryBuilder('c') |
|
| 395 | 2 | ->where('c.email = :email AND c.Status = :status') |
|
| 396 | 2 | ->setParameter('email', $email) |
|
| 397 | 2 | ->setParameter('status', CustomerStatus::ACTIVE) |
|
| 398 | ->getQuery(); |
||
| 399 | |||
| 400 | $Customer = $query->getOneOrNullResult(); |
||
| 401 | |||
| 402 | 2 | return $Customer; |
|
| 403 | } |
||
| 404 | |||
| 405 | 4 | public function getActiveCustomerByResetKey($reset_key) |
|
| 406 | { |
||
| 407 | 4 | $query = $this->createQueryBuilder('c') |
|
| 408 | 4 | ->where('c.reset_key = :reset_key AND c.Status = :status AND c.reset_expire >= :reset_expire') |
|
| 409 | 4 | ->setParameter('reset_key', $reset_key) |
|
| 410 | 4 | ->setParameter('status', CustomerStatus::ACTIVE) |
|
| 411 | ->setParameter('reset_expire', new \DateTime()) |
||
| 412 | ->getQuery(); |
||
| 413 | |||
| 414 | 2 | $Customer = $query->getSingleResult(); |
|
| 415 | |||
| 416 | 2 | return $Customer; |
|
| 417 | } |
||
| 418 | |||
| 419 | public function getResetPassword() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * 会員の初回購入時間、購入時間、購入回数、購入金額を更新する |
||
| 426 | * |
||
| 427 | * @param $app |
||
| 428 | * @param Customer $Customer |
||
| 429 | * @param $orderStatusId |
||
| 430 | */ |
||
| 431 | 1 | public function updateBuyData($app, Customer $Customer, $orderStatusId) |
|
| 432 | { |
||
| 433 | // 会員の場合、初回購入時間・購入時間・購入回数・購入金額を更新 |
||
| 476 | } |
||
| 477 |