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