Passed
Push — develop ( 763cf7...df804f )
by Andrew
06:06
created

Container::getContainerArrays()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 69
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 40
nc 16
nop 4
dl 0
loc 69
rs 8.3466
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2019 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
16
use Craft;
17
use craft\base\Element;
18
19
use yii\caching\TagDependency;
20
21
/**
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 * @package   Seomatic
24
 * @since     3.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
26
class Container
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const CACHE_KEY = 'seomatic_metacontroller_';
32
33
    // Static Methods
34
    // =========================================================================
35
    public static function getContainerArrays(
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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
    }
105
}
106