Conditions | 9 |
Paths | 96 |
Total Lines | 70 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
79 | public function createSearchQuery(RaSecondFactorQuery $query): Query |
||
80 | { |
||
81 | $queryBuilder = $this |
||
82 | ->createQueryBuilder('sf'); |
||
83 | |||
84 | // Modify query to filter on authorization context |
||
85 | // We want to list all second factors of the institution we are RA for. |
||
86 | $this->authorizationRepositoryFilter->filter( |
||
87 | $queryBuilder, |
||
88 | $query->authorizationContext, |
||
89 | 'sf.institution', |
||
90 | 'iac', |
||
91 | ); |
||
92 | |||
93 | if ($query->name) { |
||
94 | $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name)); |
||
95 | } |
||
96 | |||
97 | if ($query->type) { |
||
98 | $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type); |
||
99 | } |
||
100 | |||
101 | if ($query->secondFactorId) { |
||
102 | $queryBuilder |
||
103 | ->andWhere('sf.secondFactorId = :secondFactorId') |
||
104 | ->setParameter('secondFactorId', $query->secondFactorId); |
||
105 | } |
||
106 | |||
107 | if ($query->email) { |
||
108 | $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email)); |
||
109 | } |
||
110 | |||
111 | if ($query->institution) { |
||
112 | $queryBuilder->andWhere('sf.institution = :institution')->setParameter('institution', $query->institution); |
||
113 | } |
||
114 | |||
115 | if ($query->status) { |
||
116 | $stringStatus = $query->status; |
||
117 | if (!SecondFactorStatus::isValidStatus($stringStatus)) { |
||
118 | throw new RuntimeException( |
||
119 | sprintf( |
||
120 | 'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery', |
||
121 | $stringStatus, |
||
122 | ), |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | // we need to resolve the string value to database value using the correct doctrine type. Normally this is |
||
127 | // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
||
128 | // so we do it by hand |
||
129 | $doctrineType = Type::getType(SecondFactorStatusType::NAME); |
||
130 | $secondFactorStatus = SecondFactorStatus::$stringStatus(); |
||
131 | |||
132 | $databaseValue = $doctrineType->convertToDatabaseValue( |
||
133 | $secondFactorStatus, |
||
134 | $this->getEntityManager()->getConnection()->getDatabasePlatform(), |
||
135 | ); |
||
136 | |||
137 | $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue); |
||
138 | } |
||
139 | |||
140 | match ($query->orderBy) { |
||
141 | 'name', 'type', 'secondFactorId', 'email', 'institution', 'status' => $queryBuilder->orderBy( |
||
142 | sprintf('sf.%s', $query->orderBy), |
||
143 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC', |
||
144 | ), |
||
145 | default => $queryBuilder->getQuery(), |
||
146 | }; |
||
147 | |||
148 | return $queryBuilder->getQuery(); |
||
149 | } |
||
223 |