| Conditions | 18 |
| Paths | 262 |
| Total Lines | 118 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| 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 |
||
| 61 | public function find($data) |
||
| 62 | { |
||
| 63 | if (!isset($data['element'])) { |
||
| 64 | throw new Exception('Required parameter `element` was not supplied to `craft.similar.find`.'); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!isset($data['context'])) { |
||
| 68 | throw new Exception('Required parameter `context` was not supplied to `craft.similar.find`.'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** @var Element $element */ |
||
| 72 | $element = $data['element']; |
||
| 73 | $context = $data['context']; |
||
| 74 | $criteria = $data['criteria'] ?? []; |
||
| 75 | |||
| 76 | if (\is_object($criteria)) { |
||
| 77 | /** @var ElementQueryInterface $criteria */ |
||
| 78 | $criteria = $criteria->toArray(); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Get an ElementQuery for this Element |
||
| 82 | $elementClass = \is_object($element) ? \get_class($element) : $element; |
||
| 83 | /** @var EntryQuery $query */ |
||
| 84 | $query = $this->getElementQuery($elementClass, $criteria); |
||
| 85 | |||
| 86 | // If the $query is null, just return an empty Entry |
||
| 87 | if (!$query) { // no results |
||
| 88 | return new Entry(); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Stash any orderBy directives from the $query for our anonymous function |
||
| 92 | $this->preOrder = $query->orderBy; |
||
| 93 | $this->limit = $query->limit; |
||
| 94 | // Extract the $tagIds from the $context |
||
| 95 | if (\is_array($context)) { |
||
| 96 | $tagIds = $context; |
||
| 97 | } else { |
||
| 98 | /** @var ElementQueryInterface $context */ |
||
| 99 | $tagIds = $context->ids(); |
||
| 100 | } |
||
| 101 | $this->targetElements = $tagIds; |
||
| 102 | |||
| 103 | // We need to modify the actual craft\db\Query after the ElementQuery has been prepared |
||
| 104 | $query->on(ElementQuery::EVENT_AFTER_PREPARE, [$this, 'eventAfterPrepareHandler']); |
||
| 105 | // Return the data as an array, and only fetch the `id` and `siteId` |
||
| 106 | $query->asArray(true); |
||
| 107 | $query->select(['elements.id', 'elements_sites.siteId']); |
||
| 108 | $query->andWhere(['not', ['elements.id' => $element->id]]); |
||
| 109 | |||
| 110 | // Unless site criteria is provided, force the element's site. |
||
| 111 | if (empty($criteria['siteId']) && empty($criteria['site'])) { |
||
| 112 | $query->andWhere(['elements_sites.siteId' => $element->siteId]); |
||
| 113 | } |
||
| 114 | |||
| 115 | $query->andWhere(['in', 'relations.targetId', $tagIds]); |
||
| 116 | $query->leftJoin(['relations' => Table::RELATIONS], '[[elements.id]] = [[relations.sourceId]]'); |
||
| 117 | $results = $query->all(); |
||
| 118 | |||
| 119 | // Fetch the elements based on the returned `id` and `siteId` |
||
| 120 | $elements = Craft::$app->getElements(); |
||
| 121 | $models = []; |
||
| 122 | |||
| 123 | $queryConditions = []; |
||
| 124 | $similarCounts = []; |
||
| 125 | |||
| 126 | // Build the query conditions for a new element query. |
||
| 127 | // The reason we have to do it in two steps is because the `count` property is added by a behavior after element creation |
||
| 128 | // So if we just try to tack that on in the original query, it will throw an error on element creation |
||
| 129 | foreach ($results as $config) { |
||
| 130 | $siteId = $config['siteId']; |
||
| 131 | $elementId = $config['id']; |
||
| 132 | |||
| 133 | if ($elementId && $siteId) { |
||
| 134 | if (empty($queryConditions[$siteId])) { |
||
| 135 | $queryConditions[$siteId] = []; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Write down elements per site and similar counts |
||
| 139 | $queryConditions[$siteId][] = $elementId; |
||
| 140 | $key = $siteId . '-' . $elementId; |
||
| 141 | $similarCounts[$key] = $config['count']; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | if (empty($results)) { |
||
| 146 | return []; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Fetch all the elements in one fell swoop, including any preset eager-loaded conditions |
||
| 150 | $query = $this->getElementQuery($elementClass, $criteria); |
||
| 151 | |||
| 152 | // Make sure we fetch the elements that are similar only |
||
| 153 | $query->on(ElementQuery::EVENT_AFTER_PREPARE, function (CancelableEvent $event) use ($queryConditions) { |
||
| 154 | /** @var ElementQuery $query */ |
||
| 155 | $query = $event->sender; |
||
| 156 | $first = true; |
||
| 157 | |||
| 158 | foreach ($queryConditions as $siteId => $elementIds) { |
||
| 159 | $method = $first ? 'where' : 'orWhere'; |
||
| 160 | $query->subQuery->$method(['and', [ |
||
| 161 | 'elements_sites.siteId' => $siteId, |
||
| 162 | 'elements.id' => $elementIds] |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | }); |
||
| 166 | |||
| 167 | $elements = $query->all(); |
||
| 168 | |||
| 169 | foreach ($elements as $element) { |
||
| 170 | // The `count` property is added dynamically by our CountBehavior behavior |
||
| 171 | $key = $element->siteId . '-' . $element->id; |
||
| 172 | if (!empty($similarCounts[$key])) { |
||
| 173 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 174 | $element->count = $similarCounts[$key]; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return $elements; |
||
| 179 | } |
||
| 229 |