| Conditions | 10 |
| Paths | 136 |
| Total Lines | 72 |
| Code Lines | 46 |
| 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 |
||
| 63 | public function createSearchQuery(RecoveryTokenQuery $query): Query |
||
| 64 | { |
||
| 65 | $queryBuilder = $this->createQueryBuilder('rt'); |
||
| 66 | |||
| 67 | if ($query->authorizationContext instanceof InstitutionAuthorizationContextInterface) { |
||
| 68 | // Modify query to filter on authorization context |
||
| 69 | // We want to list all recovery tokens of the institution we are RA for. |
||
| 70 | $this->authorizationRepositoryFilter->filter( |
||
| 71 | $queryBuilder, |
||
| 72 | $query->authorizationContext, |
||
| 73 | 'rt.institution', |
||
| 74 | 'iac', |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | if ($query->identityId instanceof IdentityId) { |
||
| 78 | $queryBuilder |
||
| 79 | ->andWhere('rt.identityId = :identityId') |
||
| 80 | ->setParameter('identityId', $query->identityId); |
||
| 81 | } |
||
| 82 | if ($query->type) { |
||
| 83 | $queryBuilder |
||
| 84 | ->andWhere('rt.type = :type') |
||
| 85 | ->setParameter('type', $query->type); |
||
| 86 | } |
||
| 87 | if ($query->status) { |
||
| 88 | $stringStatus = $query->status; |
||
| 89 | if (!RecoveryTokenStatus::isValidStatus($stringStatus)) { |
||
| 90 | throw new RuntimeException( |
||
| 91 | sprintf( |
||
| 92 | 'Received invalid status "%s" in RecoveryTokenRepository::createSearchQuery', |
||
| 93 | $stringStatus, |
||
| 94 | ), |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | // we need to resolve the string value to database value using the correct doctrine type. Normally this is |
||
| 99 | // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
||
| 100 | // so we do it by hand |
||
| 101 | $doctrineType = Type::getType(RecoveryTokenStatusType::NAME); |
||
| 102 | $secondFactorStatus = RecoveryTokenStatus::$stringStatus(); |
||
| 103 | |||
| 104 | $databaseValue = $doctrineType->convertToDatabaseValue( |
||
| 105 | $secondFactorStatus, |
||
| 106 | $this->getEntityManager()->getConnection()->getDatabasePlatform(), |
||
| 107 | ); |
||
| 108 | |||
| 109 | $queryBuilder->andWhere('rt.status = :status')->setParameter('status', $databaseValue); |
||
| 110 | } |
||
| 111 | if ($query->name) { |
||
| 112 | $queryBuilder |
||
| 113 | ->andWhere('rt.name LIKE :name') |
||
| 114 | ->setParameter('name', sprintf('%%%s%%', $query->name)); |
||
| 115 | } |
||
| 116 | if ($query->email) { |
||
| 117 | $queryBuilder |
||
| 118 | ->andWhere('rt.email LIKE :email') |
||
| 119 | ->setParameter('email', sprintf('%%%s%%', $query->email)); |
||
| 120 | } |
||
| 121 | if ($query->institution) { |
||
| 122 | $queryBuilder |
||
| 123 | ->andWhere('rt.institution = :institution') |
||
| 124 | ->setParameter('institution', $query->institution); |
||
| 125 | } |
||
| 126 | match ($query->orderBy) { |
||
| 127 | 'name', 'type', 'email', 'institution', 'status' => $queryBuilder->orderBy( |
||
| 128 | sprintf('rt.%s', $query->orderBy), |
||
| 129 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC', |
||
| 130 | ), |
||
| 131 | default => $queryBuilder->getQuery(), |
||
| 132 | }; |
||
| 133 | |||
| 134 | return $queryBuilder->getQuery(); |
||
| 135 | } |
||
| 166 |