| Conditions | 7 |
| Paths | 3 |
| Total Lines | 51 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | public function purgeExpiredRecords( |
||
| 56 | int $daysOld, |
||
| 57 | int $changeType = ZAuthConstant::VERIFYCHGTYPE_REGEMAIL, |
||
| 58 | bool $deleteUserEntities = true |
||
| 59 | ): array { |
||
| 60 | if ($daysOld < 1) { |
||
| 61 | return []; |
||
| 62 | } |
||
| 63 | // Expiration date/times, as with all date/times in the Users module, are stored as UTC. |
||
| 64 | $staleRecordUTC = new DateTime(null, new DateTimeZone('UTC')); |
||
| 65 | $staleRecordUTC->modify("-{$daysOld} days"); |
||
| 66 | |||
| 67 | $qb = $this->createQueryBuilder('v'); |
||
| 68 | $and = $qb->expr()->andX() |
||
| 69 | ->add($qb->expr()->eq('v.changetype', ':changeType')) |
||
| 70 | ->add($qb->expr()->isNotNull('v.createdDate')) |
||
| 71 | ->add($qb->expr()->neq('v.createdDate', ':createdDtNot')) |
||
| 72 | ->add($qb->expr()->lt('v.createdDate', ':createdDtMax')); |
||
| 73 | $qb->select('v') |
||
| 74 | ->where($and) |
||
| 75 | ->setParameter('changeType', $changeType) |
||
| 76 | ->setParameter('createdDtNot', '0000-00-00 00:00:00') |
||
| 77 | ->setParameter('createdDtMax', $staleRecordUTC); |
||
| 78 | $staleVerificationRecords = $qb->getQuery()->getResult(); |
||
| 79 | |||
| 80 | $deletedUsers = []; |
||
| 81 | $userRepo = $this->_em->getRepository(UserEntity::class); |
||
| 82 | $authRepo = $this->_em->getRepository(AuthenticationMappingEntity::class); |
||
| 83 | if (!empty($staleVerificationRecords)) { |
||
| 84 | foreach ($staleVerificationRecords as $staleVerificationRecord) { |
||
| 85 | if ($deleteUserEntities) { |
||
| 86 | $user = $userRepo->find($staleVerificationRecord['uid']); |
||
| 87 | if (null !== $user) { |
||
| 88 | $deletedUsers[] = $user; |
||
| 89 | // delete user |
||
| 90 | $this->_em->remove($user); |
||
| 91 | } |
||
| 92 | $mapping = $authRepo->findOneBy(['uid' => $staleVerificationRecord['uid']]); |
||
| 93 | if (null !== $mapping) { |
||
| 94 | // delete mapping |
||
| 95 | $this->_em->remove($mapping); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // delete verification record |
||
| 100 | $this->_em->remove($staleVerificationRecord); |
||
| 101 | } |
||
| 102 | $this->_em->flush(); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $deletedUsers; |
||
| 106 | } |
||
| 162 |