| Conditions | 20 |
| Paths | 109 |
| Total Lines | 78 |
| Code Lines | 41 |
| Lines | 10 |
| Ratio | 12.82 % |
| Changes | 3 | ||
| 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 |
||
| 125 | public function buildWithSubQuery($containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em) |
||
| 126 | { |
||
| 127 | //test the container entity |
||
| 128 | if ($containerEntity === null) { |
||
| 129 | throw new \Exception('The container entity parameter must not be null.'); |
||
| 130 | } |
||
| 131 | |||
| 132 | //verify that the object has the query trait |
||
| 133 | //@todo please use an interface and cast with it in the method signature |
||
| 134 | $this->checkObjectHasQueryTrait($containerEntity); |
||
| 135 | |||
| 136 | //get the query of the container entity |
||
| 137 | $query = $containerEntity->getQuery(); |
||
| 138 | if (method_exists($containerEntity, 'additionnalQueryPart')) { |
||
| 139 | $query = $containerEntity->additionnalQueryPart(); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($query !== '' && $query !== null) { |
||
| 143 | $subQuery = $em->createQueryBuilder() |
||
| 144 | ->select('item.id') |
||
| 145 | ->from($itemsQueryBuilder->getRootEntities()[0], 'item'); |
||
| 146 | |||
| 147 | $itemsQueryBuilder |
||
| 148 | ->andWhere('main_item.id IN ('.$subQuery->getQuery()->getDql().' '.$query.')'); |
||
| 149 | } |
||
| 150 | |||
| 151 | //Add ORDER BY if set |
||
| 152 | if (method_exists($containerEntity, 'getOrderBy')) { |
||
| 153 | $orderBy = json_decode($containerEntity->getOrderBy(), true); |
||
| 154 | if ($orderBy) { |
||
| 155 | foreach ($orderBy as $addOrderBy) { |
||
| 156 | $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]); |
||
| 157 | $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']); |
||
| 158 | |||
| 159 | //If ordering field is an association, treat it as a boolean |
||
| 160 | if ($this->isAssociationField($reflectionProperty)) { |
||
| 161 | $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder'); |
||
| 162 | $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']); |
||
| 163 | } else { |
||
| 164 | $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $currentView = $this->currentView; |
||
| 171 | |||
| 172 | // If the current page is a BEP, we parse all its properties and inject them as query parameters |
||
| 173 | if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) { |
||
| 174 | |||
| 175 | // NEW |
||
| 176 | $metadatas = $em->getClassMetadata(get_class($currentEntity)); |
||
| 177 | View Code Duplication | foreach ($metadatas->fieldMappings as $fieldName => $field) { |
|
| 178 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 179 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | View Code Duplication | foreach ($metadatas->associationMappings as $fieldName => $field) { |
|
| 183 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 184 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId()); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | if (strpos($query, ':currentEntity') !== false) { |
||
| 189 | $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId()); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if (strpos($query, ':currentUser') !== false && is_object($this->getCurrentUser())) { |
||
| 194 | if (is_object($this->getCurrentUser())) { |
||
| 195 | $itemsQueryBuilder->setParameter('currentUser', $this->getCurrentUser()->getId()); |
||
| 196 | } else { |
||
| 197 | throw new AccessDeniedException(); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $itemsQueryBuilder; |
||
| 202 | } |
||
| 203 | |||
| 231 |