| Conditions | 7 |
| Paths | 16 |
| Total Lines | 69 |
| Code Lines | 40 |
| 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 |
||
| 35 | public static function getContainerArrays( |
||
| 36 | array $containerKeys, |
||
| 37 | string $uri, |
||
| 38 | int $siteId = null, |
||
| 39 | bool $asArray = false |
||
| 40 | ): array { |
||
| 41 | // Normalize the incoming URI to account for `__home__` |
||
| 42 | $uri = ($uri === '__home__') ? '' : $uri; |
||
| 43 | // Determine the siteId |
||
| 44 | if ($siteId === null) { |
||
| 45 | $siteId = Craft::$app->getSites()->currentSite->id |
||
| 46 | ?? Craft::$app->getSites()->primarySite->id |
||
| 47 | ?? 1; |
||
| 48 | } |
||
| 49 | $uri = trim($uri, '/'); |
||
| 50 | // Add a caching layer on top of controller requests |
||
| 51 | $sourceId = ''; |
||
| 52 | /** @var Element $element */ |
||
| 53 | $element = Craft::$app->getElements()->getElementByUri($uri, $siteId, false); |
||
| 54 | if ($element !== null) { |
||
| 55 | list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId) |
||
| 56 | = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); |
||
| 57 | } |
||
| 58 | $metaContainers = Seomatic::$plugin->metaContainers; |
||
| 59 | // Get our cache key |
||
| 60 | $asArrayKey = $asArray ? 'true' : 'false'; |
||
| 61 | $cacheKey = $uri.$siteId.implode($containerKeys).$asArrayKey; |
||
| 62 | // Load the meta containers |
||
| 63 | $dependency = new TagDependency([ |
||
| 64 | 'tags' => [ |
||
| 65 | $metaContainers::GLOBAL_METACONTAINER_CACHE_TAG, |
||
| 66 | $metaContainers::METACONTAINER_CACHE_TAG.$sourceId, |
||
| 67 | $metaContainers::METACONTAINER_CACHE_TAG.$uri.$siteId, |
||
| 68 | $metaContainers::METACONTAINER_CACHE_TAG.$cacheKey, |
||
| 69 | ], |
||
| 70 | ]); |
||
| 71 | |||
| 72 | $cache = Craft::$app->getCache(); |
||
| 73 | $result = $cache->getOrSet( |
||
| 74 | self::CACHE_KEY.$cacheKey, |
||
| 75 | function () use ($uri, $siteId, $containerKeys, $asArray) { |
||
| 76 | $result = []; |
||
| 77 | Craft::info( |
||
| 78 | 'Meta controller container cache miss: '.$uri.'/'.$siteId, |
||
| 79 | __METHOD__ |
||
| 80 | ); |
||
| 81 | // Load the meta containers and parse our globals |
||
| 82 | Seomatic::$plugin->metaContainers->previewMetaContainers($uri, $siteId, true); |
||
| 83 | |||
| 84 | // Iterate through the desired $containerKeys |
||
| 85 | foreach ($containerKeys as $containerKey) { |
||
| 86 | if ($asArray) { |
||
| 87 | $result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersArrayByType( |
||
| 88 | $containerKey |
||
| 89 | ); |
||
| 90 | } else { |
||
| 91 | $result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersByType( |
||
| 92 | $containerKey |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $result; |
||
| 98 | }, |
||
| 99 | Seomatic::$cacheDuration, |
||
| 100 | $dependency |
||
| 101 | ); |
||
| 102 | |||
| 103 | return $result; |
||
| 104 | } |
||
| 106 |