Dependency::validateDependencies()   D
last analyzed

Complexity

Conditions 34
Paths 30

Size

Total Lines 113
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1017.01

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 113
ccs 3
cts 57
cp 0.0526
rs 4.1666
c 0
b 0
f 0
cc 34
nc 30
nop 1
crap 1017.01

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
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\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
16
/**
17
 * @author    nystudio107
18
 * @package   Seomatic
19
 * @since     3.0.0
20
 */
21
class Dependency
22
{
23
    // Constants
24
    // =========================================================================
25
26
    public const CONFIG_DEPENDENCY = 'config';
27
    public const SITE_DEPENDENCY = 'site';
28
    public const META_DEPENDENCY = 'meta';
29
    public const TAG_DEPENDENCY = 'tag';
30
    public const SCRIPT_DEPENDENCY = 'script';
31
    public const LINK_DEPENDENCY = 'link';
32
    public const JSONLD_DEPENDENCY = 'jsonld';
33
    public const FRONTEND_TEMPLATE_DEPENDENCY = 'frontend_template';
34
35
    // Static Methods
36
    // =========================================================================
37
38
    /**
39
     * @param $dependencies
40
     *
41
     * @return bool
42
     */
43 1
    public static function validateDependencies($dependencies): bool
44
    {
45
        // No dependencies means we validate
46 1
        if (empty($dependencies) || !\is_array($dependencies)) {
47 1
            return true;
48
        }
49
        $config = Seomatic::$settings;
50
        $meta = Seomatic::$seomaticVariable->meta;
51
        $site = Seomatic::$seomaticVariable->site;
52
        foreach ($dependencies as $type => $keys) {
53
            /** @var array $keys */
54
            $validates = false;
55
            // If any dependency key in the array validates, this this dependency validates
56
            switch ($type) {
57
                // Handle config setting dependencies
58
                case self::CONFIG_DEPENDENCY:
59
                    foreach ($keys as $key) {
60
                        // If any value is in the $config[$key] it validates
61
                        if (!empty($config[$key])) {
62
                            $validates = true;
63
                        }
64
                    }
65
                    break;
66
                // Handle site setting dependencies
67
                case self::SITE_DEPENDENCY:
68
                    foreach ($keys as $key) {
69
                        // If any value is in the $site[$key] it validates
70
                        if (!empty($site[$key])) {
71
                            $validates = true;
72
                        }
73
                    }
74
                    break;
75
                // Handle meta setting dependencies
76
                case self::META_DEPENDENCY:
77
                    foreach ($keys as $key) {
78
                        // If any value is in the $meta[$key] it validates
79
                        if (!empty($meta[$key])) {
80
                            $validates = true;
81
                        }
82
                    }
83
                    break;
84
                // Handle tag dependencies
85
                case self::TAG_DEPENDENCY:
86
                    foreach ($keys as $key) {
87
                        $meta = Seomatic::$plugin->tag->get($key);
88
                        if ($meta !== null) {
89
                            $options = $meta->tagAttributes();
90
                            // If the meta item exists, and would render, it validates
91
                            if ($meta->prepForRender($options)) {
92
                                $validates = true;
93
                            }
94
                        }
95
                    }
96
                    break;
97
                // Handle script dependencies
98
                case self::SCRIPT_DEPENDENCY:
99
                    foreach ($keys as $key) {
100
                        $meta = Seomatic::$plugin->script->get($key);
101
                        if ($meta !== null) {
102
                            $options = $meta->tagAttributes();
103
                            // If the meta item exists, and would render, it validates
104
                            if ($meta->prepForRender($options)) {
105
                                $validates = true;
106
                            }
107
                        }
108
                    }
109
                    break;
110
                // Handle link dependencies
111
                case self::LINK_DEPENDENCY:
112
                    foreach ($keys as $key) {
113
                        $meta = Seomatic::$plugin->link->get($key);
114
                        if ($meta !== null) {
115
                            $options = $meta->tagAttributes();
116
                            // If the meta item exists, and would render, it validates
117
                            if ($meta->prepForRender($options)) {
118
                                $validates = true;
119
                            }
120
                        }
121
                    }
122
                    break;
123
                // Handle link dependencies
124
                case self::JSONLD_DEPENDENCY:
125
                    foreach ($keys as $key) {
126
                        $meta = Seomatic::$plugin->jsonLd->get($key);
127
                        if ($meta !== null) {
128
                            $options = $meta->tagAttributes();
129
                            // If the meta item exists, and would render, it validates
130
                            if ($meta->prepForRender($options)) {
131
                                $validates = true;
132
                            }
133
                        }
134
                    }
135
                    break;
136
                // Handle frontend template dependencies
137
                case self::FRONTEND_TEMPLATE_DEPENDENCY:
138
                    foreach ($keys as $key) {
139
                        $template = Seomatic::$plugin->frontendTemplates->getFrontendTemplateByKey($key);
140
                        if ($template !== null) {
141
                            // If the frontend template exists, and is included, it validates
142
                            if ($template->include) {
143
                                $validates = true;
144
                            }
145
                        }
146
                    }
147
                    break;
148
            }
149
            // If any validation rule fails, the dependency does not validate
150
            if (!$validates) {
151
                return false;
152
            }
153
        }
154
155
        return true;
156
    }
157
}
158