Passed
Push — develop ( 43720c...c72799 )
by Andrew
09:22
created

MetaScriptContainer::dataLayerHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\models;
13
14
use nystudio107\seomatic\Seomatic;
15
use nystudio107\seomatic\base\MetaContainer;
16
17
use Craft;
18
19
use yii\caching\TagDependency;
20
use yii\web\View;
21
22
/**
23
 * @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...
24
 * @package   Seomatic
25
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class MetaScriptContainer extends MetaContainer
28
{
29
    // Constants
30
    // =========================================================================
31
32
    const CONTAINER_TYPE = 'MetaScriptContainer';
33
34
    // Public Properties
35
    // =========================================================================
36
37
    /**
38
     * The data in this container
39
     *
40
     * @var MetaScript[] $data
41
     */
42
    public $data = [];
43
44
    /**
45
     * @var int
46
     */
47
    public $position = View::POS_HEAD;
48
49
    // Public Methods
50
    // =========================================================================
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $dependency should have a doc-comment as per coding-style.
Loading history...
53
     * @inheritdoc
54
     */
55
    public function includeMetaData($dependency)
56
    {
57
        Craft::beginProfile('MetaScriptContainer::includeMetaData', __METHOD__);
58
        $uniqueKey = $this->handle.$dependency->tags[3] . $this->dataLayerHash();
59
        $cache = Craft::$app->getCache();
60
        if ($this->clearCache) {
61
            TagDependency::invalidate($cache, $uniqueKey);
62
        }
63
        $tagData = $cache->getOrSet(
64
            $this::CONTAINER_TYPE.$uniqueKey,
65
            function () use ($uniqueKey) {
66
                Craft::info(
67
                    $this::CONTAINER_TYPE.' cache miss: '.$uniqueKey,
68
                    __METHOD__
69
                );
70
                $tagData = [];
71
                if ($this->prepForInclusion()) {
72
                    /** @var $metaScriptModel MetaScript */
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...
73
                    foreach ($this->data as $metaScriptModel) {
74
                        if ($metaScriptModel->include) {
75
                            $js = $metaScriptModel->render();
76
                            $tagData[] = [
77
                                'js' => $js,
78
                                'position' => $metaScriptModel->position ?? $this->position
79
                            ];
80
                            // If `devMode` is enabled, validate the Meta Script and output any model errors
81
                            if (Seomatic::$devMode) {
82
                                $metaScriptModel->debugMetaItem(
83
                                    'Script attribute: '
84
                                );
85
                            }
86
                        }
87
                    }
88
                }
89
90
                return $tagData;
91
            },
92
            Seomatic::$cacheDuration,
93
            $dependency
94
        );
95
        // Register the tags
96
        foreach ($tagData as $config) {
97
            Seomatic::$view->registerJs(
98
                $config['js'],
99
                $config['position']
100
            );
101
        }
102
        Craft::endProfile('MetaScriptContainer::includeMetaData', __METHOD__);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function normalizeContainerData()
109
    {
110
        parent::normalizeContainerData();
111
112
        foreach ($this->data as $key => $config) {
113
            $config['key'] = $key;
114
            $this->data[$key] = MetaScript::create($config);
0 ignored issues
show
Bug introduced by
$config of type nystudio107\seomatic\models\MetaScript is incompatible with the type array expected by parameter $config of nystudio107\seomatic\models\MetaScript::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $this->data[$key] = MetaScript::create(/** @scrutinizer ignore-type */ $config);
Loading history...
115
        }
116
    }
117
118
    // Protected Methods
119
    // =========================================================================
120
121
    protected function dataLayerHash(): string
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
122
    {
123
        $data = '';
124
        foreach ($this->data as $metaScriptModel) {
125
            $data .= serialize($metaScriptModel->dataLayer);
126
        }
127
128
        return md5($data);
129
    }
130
}
131