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 | 11 | public function newCustomer() |
|
| 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 | 70 | public function loadUserByUsername($username) |
|
| 82 | { |
||
| 83 | // 本会員ステータスの会員のみ有効. |
||
| 84 | $CustomerStatus = $this |
||
| 85 | 70 | ->getEntityManager() |
|
| 86 | 70 | ->getRepository('Eccube\Entity\Master\CustomerStatus') |
|
| 87 | 70 | ->find(CustomerStatus::ACTIVE); |
|
| 88 | |||
| 89 | 70 | $query = $this->createQueryBuilder('c') |
|
| 90 | 70 | ->where('c.email = :email') |
|
| 91 | 70 | ->andWhere('c.del_flg = :delFlg') |
|
| 92 | 70 | ->andWhere('c.Status =:CustomerStatus') |
|
| 93 | 70 | ->setParameters(array( |
|
| 94 | 70 | 'email' => $username, |
|
| 95 | 70 | 'delFlg' => Constant::DISABLED, |
|
| 96 | 70 | 'CustomerStatus' => $CustomerStatus, |
|
| 97 | )) |
||
| 98 | 70 | ->setMaxResults(1) |
|
| 99 | 70 | ->getQuery(); |
|
| 100 | 70 | $Customer = $query->getOneOrNullResult(); |
|
| 101 | 70 | if (!$Customer) { |
|
| 102 | 1 | throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
|
| 103 | } |
||
| 104 | |||
| 105 | 69 | return $Customer; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Refreshes the user for the account interface. |
||
| 110 | * |
||
| 111 | * It is up to the implementation to decide if the user data should be |
||
| 112 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
| 113 | * object can just be merged into some internal array of users / identity |
||
| 114 | * map. |
||
| 115 | * |
||
| 116 | * @param UserInterface $user |
||
| 117 | * |
||
| 118 | * @return UserInterface |
||
| 119 | * |
||
| 120 | * @throws UnsupportedUserException if the account is not supported |
||
| 121 | */ |
||
| 122 | 67 | View Code Duplication | public function refreshUser(UserInterface $user) |
| 130 | |||
| 131 | /** |
||
| 132 | * Whether this provider supports the given user class. |
||
| 133 | * |
||
| 134 | * @param string $class |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 1 | public function supportsClass($class) |
|
| 142 | |||
| 143 | 40 | public function getQueryBuilderBySearchData($searchData) |
|
| 144 | { |
||
| 145 | 40 | $qb = $this->createQueryBuilder('c') |
|
| 146 | 40 | ->select('c') |
|
| 147 | 40 | ->andWhere('c.del_flg = 0'); |
|
| 148 | |||
| 149 | 40 | if (isset($searchData['multi']) && Str::isNotBlank($searchData['multi'])) { |
|
| 150 | //スペース除去 |
||
| 151 | 16 | $clean_key_multi = preg_replace('/\s+|[ ]+/u', '',$searchData['multi']); |
|
| 152 | 16 | if (preg_match('/^\d+$/', $clean_key_multi)) { |
|
| 153 | $qb |
||
| 154 | 8 | ->andWhere('c.id = :customer_id') |
|
| 155 | 8 | ->setParameter('customer_id', $clean_key_multi); |
|
| 156 | } else { |
||
| 157 | $qb |
||
| 158 | 8 | ->andWhere('CONCAT(c.name01, c.name02) LIKE :name OR CONCAT(c.kana01, c.kana02) LIKE :kana OR c.email LIKE :email') |
|
| 159 | 8 | ->setParameter('name', '%' . $clean_key_multi . '%') |
|
| 160 | 8 | ->setParameter('kana', '%' . $clean_key_multi . '%') |
|
| 161 | 8 | ->setParameter('email', '%' . $clean_key_multi . '%'); |
|
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // Pref |
||
| 166 | 40 | View Code Duplication | if (!empty($searchData['pref']) && $searchData['pref']) { |
| 167 | $qb |
||
| 168 | 1 | ->andWhere('c.Pref = :pref') |
|
| 169 | 1 | ->setParameter('pref', $searchData['pref']->getId()); |
|
| 170 | } |
||
| 171 | |||
| 172 | // sex |
||
| 173 | 40 | if (!empty($searchData['sex']) && count($searchData['sex']) > 0) { |
|
| 174 | 2 | $sexs = array(); |
|
| 175 | 2 | foreach ($searchData['sex'] as $sex) { |
|
| 176 | 2 | $sexs[] = $sex->getId(); |
|
| 177 | } |
||
| 178 | |||
| 179 | $qb |
||
| 180 | 2 | ->andWhere($qb->expr()->in('c.Sex', ':sexs')) |
|
| 181 | 2 | ->setParameter('sexs', $sexs); |
|
| 182 | } |
||
| 183 | |||
| 184 | 40 | View Code Duplication | if (!empty($searchData['birth_month']) && $searchData['birth_month']) { |
| 185 | $qb |
||
| 186 | 1 | ->andWhere('EXTRACT(MONTH FROM c.birth) = :birth_month') |
|
| 187 | 1 | ->setParameter('birth_month', $searchData['birth_month']); |
|
| 188 | } |
||
| 189 | |||
| 190 | // birth |
||
| 191 | 40 | View Code Duplication | if (!empty($searchData['birth_start']) && $searchData['birth_start']) { |
| 192 | 2 | $date = $searchData['birth_start'] |
|
| 193 | 2 | ->format('Y-m-d H:i:s'); |
|
| 194 | $qb |
||
| 195 | 2 | ->andWhere('c.birth >= :birth_start') |
|
| 196 | 2 | ->setParameter('birth_start', $date); |
|
| 197 | } |
||
| 198 | 40 | View Code Duplication | if (!empty($searchData['birth_end']) && $searchData['birth_end']) { |
| 199 | 2 | $date = clone $searchData['birth_end']; |
|
| 200 | $date = $date |
||
| 201 | 2 | ->modify('+1 days') |
|
| 202 | 2 | ->format('Y-m-d H:i:s'); |
|
| 203 | $qb |
||
| 204 | 2 | ->andWhere('c.birth < :birth_end') |
|
| 205 | 2 | ->setParameter('birth_end', $date); |
|
| 206 | } |
||
| 207 | |||
| 208 | // tel |
||
| 209 | 40 | View Code Duplication | if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) { |
| 210 | $qb |
||
| 211 | 1 | ->andWhere('CONCAT(c.tel01, c.tel02, c.tel03) LIKE :tel') |
|
| 212 | 1 | ->setParameter('tel', '%' . $searchData['tel'] . '%'); |
|
| 213 | } |
||
| 214 | |||
| 215 | // buy_total |
||
| 216 | 40 | View Code Duplication | if (isset($searchData['buy_total_start']) && Str::isNotBlank($searchData['buy_total_start'])) { |
| 217 | $qb |
||
| 218 | 1 | ->andWhere('c.buy_total >= :buy_total_start') |
|
| 219 | 1 | ->setParameter('buy_total_start', $searchData['buy_total_start']); |
|
| 220 | } |
||
| 221 | 40 | View Code Duplication | if (isset($searchData['buy_total_end']) && Str::isNotBlank($searchData['buy_total_end'])) { |
| 222 | $qb |
||
| 223 | 1 | ->andWhere('c.buy_total <= :buy_total_end') |
|
| 224 | 1 | ->setParameter('buy_total_end', $searchData['buy_total_end']); |
|
| 225 | } |
||
| 226 | |||
| 227 | // buy_times |
||
| 228 | 40 | if (!empty($searchData['buy_times_start']) && $searchData['buy_times_start']) { |
|
| 229 | $qb |
||
| 230 | 1 | ->andWhere('c.buy_times >= :buy_times_start') |
|
| 231 | 1 | ->setParameter('buy_times_start', $searchData['buy_times_start']); |
|
| 232 | } |
||
| 233 | 40 | if (!empty($searchData['buy_times_end']) && $searchData['buy_times_end']) { |
|
| 234 | $qb |
||
| 235 | 1 | ->andWhere('c.buy_times <= :buy_times_end') |
|
| 236 | 1 | ->setParameter('buy_times_end', $searchData['buy_times_end']); |
|
| 237 | } |
||
| 238 | |||
| 239 | // create_date |
||
| 240 | 40 | View Code Duplication | if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) { |
| 241 | 1 | $date = $searchData['create_date_start'] |
|
| 242 | 1 | ->format('Y-m-d H:i:s'); |
|
| 243 | $qb |
||
| 244 | 1 | ->andWhere('c.create_date >= :create_date_start') |
|
| 245 | 1 | ->setParameter('create_date_start', $date); |
|
| 246 | } |
||
| 247 | 40 | View Code Duplication | if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) { |
| 248 | 1 | $date = clone $searchData['create_date_end']; |
|
| 249 | $date = $date |
||
| 250 | 1 | ->modify('+1 days') |
|
| 251 | 1 | ->format('Y-m-d H:i:s'); |
|
| 252 | $qb |
||
| 253 | 1 | ->andWhere('c.create_date < :create_date_end') |
|
| 254 | 1 | ->setParameter('create_date_end', $date); |
|
| 255 | } |
||
| 256 | |||
| 257 | // update_date |
||
| 258 | 40 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
| 259 | 1 | $date = $searchData['update_date_start'] |
|
| 260 | 1 | ->format('Y-m-d H:i:s'); |
|
| 261 | $qb |
||
| 262 | 1 | ->andWhere('c.update_date >= :update_date_start') |
|
| 263 | 1 | ->setParameter('update_date_start', $date); |
|
| 264 | } |
||
| 265 | 40 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
| 266 | 1 | $date = clone $searchData['update_date_end']; |
|
| 267 | $date = $date |
||
| 268 | 1 | ->modify('+1 days') |
|
| 269 | 1 | ->format('Y-m-d H:i:s'); |
|
| 270 | $qb |
||
| 271 | 1 | ->andWhere('c.update_date < :update_date_end') |
|
| 272 | 1 | ->setParameter('update_date_end', $date); |
|
| 273 | } |
||
| 274 | |||
| 275 | // last_buy |
||
| 276 | 40 | View Code Duplication | if (!empty($searchData['last_buy_start']) && $searchData['last_buy_start']) { |
| 277 | 1 | $date = $searchData['last_buy_start'] |
|
| 278 | 1 | ->format('Y-m-d H:i:s'); |
|
| 279 | $qb |
||
| 280 | 1 | ->andWhere('c.last_buy_date >= :last_buy_start') |
|
| 281 | 1 | ->setParameter('last_buy_start', $date); |
|
| 282 | } |
||
| 283 | 40 | View Code Duplication | if (!empty($searchData['last_buy_end']) && $searchData['last_buy_end']) { |
| 284 | 1 | $date = clone $searchData['last_buy_end']; |
|
| 285 | $date = $date |
||
| 286 | 1 | ->modify('+1 days') |
|
| 287 | 1 | ->format('Y-m-d H:i:s'); |
|
| 288 | $qb |
||
| 289 | 1 | ->andWhere('c.last_buy_date < :last_buy_end') |
|
| 290 | 1 | ->setParameter('last_buy_end', $date); |
|
| 291 | } |
||
| 292 | |||
| 293 | // status |
||
| 294 | 40 | if (!empty($searchData['customer_status']) && count($searchData['customer_status']) > 0) { |
|
| 295 | $qb |
||
| 296 | 2 | ->andWhere($qb->expr()->in('c.Status', ':statuses')) |
|
| 297 | 2 | ->setParameter('statuses', $searchData['customer_status']); |
|
| 298 | } |
||
| 299 | |||
| 300 | // buy_product_name、buy_product_code |
||
| 301 | 40 | View Code Duplication | if (isset($searchData['buy_product_code']) && Str::isNotBlank($searchData['buy_product_code'])) { |
| 302 | $qb |
||
| 303 | 1 | ->leftJoin('c.Orders', 'o') |
|
| 304 | 1 | ->leftJoin('o.OrderDetails', 'od') |
|
| 305 | 1 | ->andWhere('od.product_name LIKE :buy_product_name OR od.product_code LIKE :buy_product_name') |
|
| 306 | 1 | ->setParameter('buy_product_name', '%' . $searchData['buy_product_code'] . '%'); |
|
| 307 | } |
||
| 308 | |||
| 309 | // Order By |
||
| 310 | 40 | $qb->addOrderBy('c.update_date', 'DESC'); |
|
| 311 | |||
| 312 | 40 | return $qb; |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * ユニークなシークレットキーを返す |
||
| 317 | * @param $app |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 298 | View Code Duplication | public function getUniqueSecretKey($app) |
| 321 | { |
||
| 322 | 298 | $unique = Str::random(32); |
|
| 323 | 298 | $Customer = $app['eccube.repository.customer']->findBy(array( |
|
| 324 | 298 | 'secret_key' => $unique, |
|
| 325 | )); |
||
| 326 | 298 | if (count($Customer) == 0) { |
|
| 327 | 298 | return $unique; |
|
| 328 | } else { |
||
| 329 | return $this->getUniqueSecretKey($app); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * ユニークなパスワードリセットキーを返す |
||
| 335 | * @param $app |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | 2 | View Code Duplication | public function getUniqueResetKey($app) |
| 339 | { |
||
| 340 | 2 | $unique = Str::random(32); |
|
| 341 | 2 | $Customer = $app['eccube.repository.customer']->findBy(array( |
|
| 342 | 2 | 'reset_key' => $unique, |
|
| 343 | )); |
||
| 344 | 2 | if (count($Customer) == 0) { |
|
| 345 | 2 | return $unique; |
|
| 346 | } else { |
||
| 347 | return $this->getUniqueResetKey($app); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * saltを生成する |
||
| 353 | * |
||
| 354 | * @param $byte |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 298 | public function createSalt($byte) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * 入力されたパスワードをSaltと暗号化する |
||
| 366 | * |
||
| 367 | * @param $app |
||
| 368 | * @param Customer $Customer |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | 298 | public function encryptPassword($app, \Eccube\Entity\Customer $Customer) |
|
| 377 | |||
| 378 | 5 | public function getNonActiveCustomerBySecretKey($secret_key) |
|
| 390 | |||
| 391 | 3 | public function getActiveCustomerByEmail($email) |
|
| 404 | |||
| 405 | 5 | public function getActiveCustomerByResetKey($reset_key) |
|
| 418 | |||
| 419 | 4 | public function getResetPassword() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * 会員の初回購入時間、購入時間、購入回数、購入金額を更新する |
||
| 426 | * |
||
| 427 | * @param $app |
||
| 428 | * @param Customer $Customer |
||
| 429 | * @param $orderStatusId |
||
| 430 | */ |
||
| 431 | 14 | public function updateBuyData($app, Customer $Customer, $orderStatusId) |
|
| 476 | } |
||
| 477 |