| Conditions | 13 |
| Paths | 71 |
| Total Lines | 137 |
| 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 |
||
| 101 | public function handle( |
||
| 102 | CriteriaConverter $converter, |
||
| 103 | SelectQuery $query, |
||
| 104 | Criterion $criterion, |
||
| 105 | array $languageSettings |
||
| 106 | ) { |
||
| 107 | $fieldDefinitionIds = $this->getFieldDefinitionIds($criterion->target); |
||
| 108 | $subSelect = $query->subSelect(); |
||
| 109 | |||
| 110 | /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value\MapLocationValue $location */ |
||
| 111 | $location = $criterion->valueData; |
||
| 112 | |||
| 113 | /* |
||
| 114 | * Note: this formula is precise only for short distances. |
||
| 115 | * @todo if ABS function was available in Zeta Database component it should be possible to account for |
||
| 116 | * distances across the date line. Revisit when Doctrine DBAL is introduced. |
||
| 117 | */ |
||
| 118 | $longitudeCorrectionByLatitude = cos(deg2rad($location->latitude)) ** 2; |
||
| 119 | $distanceExpression = $subSelect->expr->add( |
||
| 120 | $subSelect->expr->mul( |
||
| 121 | $subSelect->expr->sub( |
||
| 122 | $this->dbHandler->quoteColumn('latitude', 'ezgmaplocation'), |
||
| 123 | $subSelect->bindValue($location->latitude) |
||
| 124 | ), |
||
| 125 | $subSelect->expr->sub( |
||
| 126 | $this->dbHandler->quoteColumn('latitude', 'ezgmaplocation'), |
||
| 127 | $subSelect->bindValue($location->latitude) |
||
| 128 | ) |
||
| 129 | ), |
||
| 130 | $subSelect->expr->mul( |
||
| 131 | $subSelect->expr->sub( |
||
| 132 | $this->dbHandler->quoteColumn('longitude', 'ezgmaplocation'), |
||
| 133 | $subSelect->bindValue($location->longitude) |
||
| 134 | ), |
||
| 135 | $subSelect->expr->sub( |
||
| 136 | $this->dbHandler->quoteColumn('longitude', 'ezgmaplocation'), |
||
| 137 | $subSelect->bindValue($location->longitude) |
||
| 138 | ), |
||
| 139 | $subSelect->bindValue($longitudeCorrectionByLatitude) |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | |||
| 143 | switch ($criterion->operator) { |
||
| 144 | case Criterion\Operator::IN: |
||
| 145 | case Criterion\Operator::EQ: |
||
| 146 | case Criterion\Operator::GT: |
||
| 147 | case Criterion\Operator::GTE: |
||
| 148 | case Criterion\Operator::LT: |
||
| 149 | case Criterion\Operator::LTE: |
||
| 150 | $operatorFunction = $this->comparatorMap[$criterion->operator]; |
||
| 151 | $distanceInDegrees = $this->kilometersToDegrees($criterion->value) ** 2; |
||
| 152 | $distanceFilter = $subSelect->expr->$operatorFunction( |
||
| 153 | $distanceExpression, |
||
| 154 | $subSelect->expr->round( |
||
| 155 | $subSelect->bindValue($distanceInDegrees), |
||
| 156 | 10 |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | break; |
||
| 160 | |||
| 161 | case Criterion\Operator::BETWEEN: |
||
| 162 | $distanceInDegrees1 = $this->kilometersToDegrees($criterion->value[0]) ** 2; |
||
| 163 | $distanceInDegrees2 = $this->kilometersToDegrees($criterion->value[1]) ** 2; |
||
| 164 | $distanceFilter = $subSelect->expr->between( |
||
| 165 | $distanceExpression, |
||
| 166 | $subSelect->expr->round( |
||
| 167 | $subSelect->bindValue($distanceInDegrees1), |
||
| 168 | 10 |
||
| 169 | ), |
||
| 170 | $subSelect->expr->round( |
||
| 171 | $subSelect->bindValue($distanceInDegrees2), |
||
| 172 | 10 |
||
| 173 | ) |
||
| 174 | ); |
||
| 175 | break; |
||
| 176 | |||
| 177 | default: |
||
| 178 | throw new RuntimeException('Unknown operator.'); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Calculate bounding box if possible |
||
| 182 | // @todo consider covering operators EQ and IN as well |
||
| 183 | $boundingConstraints = []; |
||
| 184 | switch ($criterion->operator) { |
||
| 185 | case Criterion\Operator::LT: |
||
| 186 | case Criterion\Operator::LTE: |
||
| 187 | $distanceUpper = $criterion->value; |
||
| 188 | break; |
||
| 189 | case Criterion\Operator::BETWEEN: |
||
| 190 | $distanceUpper = $criterion->value[0] > $criterion->value[1] ? |
||
| 191 | $criterion->value[0] : |
||
| 192 | $criterion->value[1]; |
||
| 193 | break; |
||
| 194 | } |
||
| 195 | if (isset($distanceUpper)) { |
||
| 196 | $boundingConstraints = $this->getBoundingConstraints($subSelect, $location, $distanceUpper); |
||
| 197 | } |
||
| 198 | |||
| 199 | $subSelect |
||
| 200 | ->select($this->dbHandler->quoteColumn('contentobject_id')) |
||
| 201 | ->from($this->dbHandler->quoteTable('ezcontentobject_attribute')) |
||
| 202 | ->innerJoin( |
||
| 203 | $this->dbHandler->quoteTable('ezgmaplocation'), |
||
| 204 | $subSelect->expr->lAnd( |
||
| 205 | [ |
||
| 206 | $subSelect->expr->eq( |
||
| 207 | $this->dbHandler->quoteColumn('contentobject_version', 'ezgmaplocation'), |
||
| 208 | $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute') |
||
| 209 | ), |
||
| 210 | $subSelect->expr->eq( |
||
| 211 | $this->dbHandler->quoteColumn('contentobject_attribute_id', 'ezgmaplocation'), |
||
| 212 | $this->dbHandler->quoteColumn('id', 'ezcontentobject_attribute') |
||
| 213 | ), |
||
| 214 | ], |
||
| 215 | $boundingConstraints |
||
| 216 | ) |
||
| 217 | ) |
||
| 218 | ->where( |
||
| 219 | $subSelect->expr->lAnd( |
||
| 220 | $subSelect->expr->eq( |
||
| 221 | $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute'), |
||
| 222 | $this->dbHandler->quoteColumn('current_version', 'ezcontentobject') |
||
| 223 | ), |
||
| 224 | $subSelect->expr->in( |
||
| 225 | $this->dbHandler->quoteColumn('contentclassattribute_id', 'ezcontentobject_attribute'), |
||
| 226 | $fieldDefinitionIds |
||
| 227 | ), |
||
| 228 | $distanceFilter, |
||
| 229 | $this->getFieldCondition($subSelect, $languageSettings) |
||
| 230 | ) |
||
| 231 | ); |
||
| 232 | |||
| 233 | return $query->expr->in( |
||
| 234 | $this->dbHandler->quoteColumn('id', 'ezcontentobject'), |
||
| 235 | $subSelect |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 319 |