Passed
Push — v3 ( 5141f6...ef9227 )
by Andrew
31:01 queued 18:03
created

SeomaticPanel::save()   C

Complexity

Conditions 13
Paths 37

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 59
ccs 0
cts 53
cp 0
rs 6.6166
cc 13
nc 37
nop 0
crap 182

How to fix   Long Method    Complexity   

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) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\debug\panels;
13
14
use Craft;
15
use nystudio107\seomatic\events\MetaBundleDebugDataEvent;
16
use nystudio107\seomatic\helpers\MetaValue;
17
use nystudio107\seomatic\models\MetaJsonLd;
18
use nystudio107\seomatic\services\MetaContainers;
19
use yii\base\Event;
20
use yii\debug\Panel;
21
22
class SeomaticPanel extends Panel
23
{
24
    const CONTAINER_PARSED_PROPERTIES = [
25
        'metaGlobalVars',
26
        'metaSiteVars',
27
        'metaSitemapVars'
28
    ];
29
30
    /**
31
     * @var array The accumulated MetaBundle debug data
32
     */
33
    protected $metaBundles = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $viewPath = '@nystudio107/seomatic/debug/views/seomatic/';
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function init(): void
44
    {
45
        parent::init();
46
47
        Event::on(MetaContainers::class,
48
            MetaContainers::EVENT_METABUNDLE_DEBUG_DATA,
49
            function (MetaBundleDebugDataEvent $e) {
50
                if ($e->metaBundle) {
51
                    $this->metaBundles[$e->metaBundleCategory] = $e->metaBundle;
52
                }
53
            });
54
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getName(): string
61
    {
62
        return 'Seomatic';
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function getSummary(): string
69
    {
70
        return Craft::$app->getView()->render($this->viewPath . 'summary', ['panel' => $this]);
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function getDetail(): string
77
    {
78
        return Craft::$app->getView()->render($this->viewPath . 'detail', ['panel' => $this]);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function save(): array
85
    {
86
        $debugData = [];
87
        foreach ($this->metaBundles as $metaBundleCategory => $metaBundle) {
88
            $metaBundleArray = $metaBundle->toArray();
89
            $parsedMetaBundleArray = $metaBundleArray;
90
            $renderedTags = [];
91
            // Parse the variables
92
            foreach (self::CONTAINER_PARSED_PROPERTIES as $property) {
93
                MetaValue::parseArray($parsedMetaBundleArray[$property], true, true, true);
94
            }
95
            // Render the container data as an array, and also as rendered tags
96
            foreach ($metaBundle->metaContainers as $metaContainerName => $metaContainer) {
97
                $parsedMetaBundleArray['metaContainers'][$metaContainerName]['data'] = $metaContainer->renderArray();
98
                $renderedTags[$metaContainerName] = $metaContainer->render();
99
                // Add in any errors from the rendering process
100
                foreach ($metaContainer->data as $tagName => $tag) {
101
                    $isMetaJsonLdModel = false;
102
                    if (is_subclass_of($tag, MetaJsonLd::class)) {
103
                        $isMetaJsonLdModel = true;
104
                    }
105
                    $scenarios = [
106
                        'default' => 'error',
107
                        'warning' => 'warning',
108
                        'google' => 'warning',
109
                    ];
110
                    $modelScenarios = $tag->scenarios();
111
                    $scenarios = array_intersect_key($scenarios, $modelScenarios);
112
                    foreach ($scenarios as $scenario => $logLevel) {
113
                        $tag->setScenario($scenario);
114
                        $tag->validate();
115
                        foreach ($tag->errors as $param => $errors) {
116
                            /** @var array $errors */
117
                            foreach ($errors as $error) {
118
                                // Change the error level depending on the error message if this is a MetaJsonLD object
119
                                if ($isMetaJsonLdModel) {
120
                                    if (strpos($error, 'recommended') !== false) {
121
                                        $logLevel = 'warning';
122
                                    }
123
                                    if (strpos($error, 'required') !== false
124
                                        || strpos($error, 'Must be') !== false
125
                                    ) {
126
                                        $logLevel = 'error';
127
                                    }
128
                                }
129
                            }
130
                        }
131
                        $parsedMetaBundleArray['metaContainers'][$metaContainerName]['data'][$tagName]['__errors'][$logLevel] = $tag->getErrors();
132
                    }
133
                }
134
            }
135
            $debugData[$metaBundleCategory] = [
136
                'unparsed' => $metaBundleArray,
137
                'parsed' => $parsedMetaBundleArray,
138
                'renderedTags' => $renderedTags,
139
            ];
140
        }
141
142
        return $debugData;
143
    }
144
}
145