Conditions | 11 |
Paths | 192 |
Total Lines | 95 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
47 | public static function getContainerArrays( |
||
48 | array $containerKeys, |
||
49 | string $uri, |
||
50 | int $siteId = null, |
||
51 | bool $asArray = false, |
||
52 | ): array { |
||
53 | // Normalize the incoming URI to account for `__home__` |
||
54 | $uri = ($uri === '__home__') ? '' : $uri; |
||
55 | // Determine the siteId |
||
56 | if ($siteId === null) { |
||
57 | $siteId = Craft::$app->getSites()->currentSite->id |
||
58 | ?? Craft::$app->getSites()->primarySite->id |
||
59 | ?? 1; |
||
60 | } |
||
61 | // Set Craft's current site to the siteId we decided upon, stashing the current site first |
||
62 | $sites = Craft::$app->getSites(); |
||
63 | $oldCurrentSite = $siteId; |
||
|
|||
64 | try { |
||
65 | $oldCurrentSite = $sites->getCurrentSite(); |
||
66 | } catch (SiteNotFoundException $e) { |
||
67 | Craft::error($e->getMessage(), __METHOD__); |
||
68 | } |
||
69 | $sites->setCurrentSite($siteId); |
||
70 | // Trim the URI |
||
71 | $uri = trim($uri, '/'); |
||
72 | // Add a caching layer on top of controller requests |
||
73 | $sourceId = ''; |
||
74 | /** @var Element $element */ |
||
75 | $element = Craft::$app->getElements()->getElementByUri($uri, $siteId, false); |
||
76 | $sourceBundleType = ''; |
||
77 | if ($element !== null) { |
||
78 | list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId) |
||
79 | = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); |
||
80 | } |
||
81 | $metaContainers = Seomatic::$plugin->metaContainers; |
||
82 | // Cache requests that have a token associated with them separately |
||
83 | $token = ''; |
||
84 | $request = Craft::$app->getRequest(); |
||
85 | if (!$request->isConsoleRequest) { |
||
86 | try { |
||
87 | $token = $request->getToken() ?? ''; |
||
88 | } catch (BadRequestHttpException $e) { |
||
89 | } |
||
90 | } |
||
91 | // Get our cache key |
||
92 | $asArrayKey = $asArray ? 'true' : 'false'; |
||
93 | $cacheKey = $uri . $siteId . implode($containerKeys) . $asArrayKey . Seomatic::$environment . $token; |
||
94 | // Load the meta containers |
||
95 | $dependency = new TagDependency([ |
||
96 | 'tags' => [ |
||
97 | $metaContainers::GLOBAL_METACONTAINER_CACHE_TAG, |
||
98 | $metaContainers::METACONTAINER_CACHE_TAG . $sourceId . $sourceBundleType . $siteId, |
||
99 | $metaContainers::METACONTAINER_CACHE_TAG . $uri . $siteId, |
||
100 | $metaContainers::METACONTAINER_CACHE_TAG . $cacheKey, |
||
101 | ], |
||
102 | ]); |
||
103 | $cache = Craft::$app->getCache(); |
||
104 | $result = $cache->getOrSet( |
||
105 | self::CACHE_KEY . $cacheKey, |
||
106 | function() use ($uri, $siteId, $containerKeys, $asArray) { |
||
107 | $result = []; |
||
108 | Craft::info( |
||
109 | 'Meta controller container cache miss: ' . $uri . '/' . $siteId, |
||
110 | __METHOD__ |
||
111 | ); |
||
112 | // Load the meta containers and parse our globals |
||
113 | Seomatic::$headlessRequest = true; |
||
114 | Seomatic::$plugin->metaContainers->previewMetaContainers($uri, $siteId, true); |
||
115 | // Iterate through the desired $containerKeys |
||
116 | foreach ($containerKeys as $containerKey) { |
||
117 | if ($asArray) { |
||
118 | $result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersArrayByType( |
||
119 | $containerKey |
||
120 | ); |
||
121 | } else { |
||
122 | $result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersByType( |
||
123 | $containerKey |
||
124 | ); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | |||
129 | return $result; |
||
130 | }, |
||
131 | Seomatic::$cacheDuration, |
||
132 | $dependency |
||
133 | ); |
||
134 | // Restore old current site |
||
135 | $sites->setCurrentSite($oldCurrentSite); |
||
136 | // Invalidate the cache we just created if there were pending image transforms in it |
||
137 | if (ImageTransformHelper::$pendingImageTransforms) { |
||
138 | TagDependency::invalidate($cache, $dependency->tags[3]); |
||
139 | } |
||
140 | |||
141 | return $result; |
||
142 | } |
||
144 |