Test Failed
Push — develop ( 57f7da...f91094 )
by Andrew
09:58
created

SeomaticVariable   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 74
c 6
b 0
f 0
dl 0
loc 170
ccs 0
cts 56
cp 0
rs 10
wmc 28

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A getEnvironment() 0 3 1
B getEnvironmentMessage() 0 56 11
A getPluginName() 0 3 1
C init() 0 26 14
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\variables;
13
14
use craft\helpers\UrlHelper;
15
use nystudio107\seomatic\Seomatic;
16
use nystudio107\seomatic\helpers\Environment as EnvironmentHelper;
17
use nystudio107\seomatic\models\MetaGlobalVars;
18
use nystudio107\seomatic\models\MetaSiteVars;
19
use nystudio107\seomatic\models\MetaSitemapVars;
20
use nystudio107\seomatic\models\Settings;
21
use nystudio107\seomatic\services\Helper;
22
use nystudio107\seomatic\services\JsonLd;
23
use nystudio107\seomatic\services\Link;
24
use nystudio107\seomatic\services\Script;
25
use nystudio107\seomatic\services\Tag;
26
use nystudio107\seomatic\services\Title;
27
use nystudio107\seomatic\services\MetaContainers;
28
use nystudio107\seomatic\services\MetaBundles;
29
30
use nystudio107\pluginmanifest\variables\ManifestVariable as Manifest;
31
32
use Craft;
33
34
use yii\di\ServiceLocator;
35
36
/**
37
 * Seomatic defines the `seomatic` global template variable.
38
 *
39
 * @property Helper     helper
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
40
 * @property JsonLd     jsonLd
41
 * @property Link       link
42
 * @property Script     script
43
 * @property Tag        tag
44
 * @property Title      title
45
 * @property Manifest   manifest
46
 *
47
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
48
 * @package   Seomatic
49
 * @since     3.0.0
50
 */
51
class SeomaticVariable extends ServiceLocator
52
{
53
    // Properties
54
    // =========================================================================
55
56
    /**
57
     * @var MetaGlobalVars
58
     */
59
    public $meta;
60
61
    /**
62
     * @var MetaSiteVars
63
     */
64
    public $site;
65
66
    /**
67
     * @var MetaSitemapVars
68
     */
69
    public $sitemap;
70
71
    /**
72
     * @var Settings
73
     */
74
    public $config;
75
76
    /**
77
     * @var MetaContainers
78
     */
79
    public $containers;
80
81
    /**
82
     * @var MetaBundles
83
     */
84
    public $bundles;
85
86
    // Public Methods
87
    // =========================================================================
88
89
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
90
     * @inheritdoc
91
     */
92
    public function __construct($config = [])
93
    {
94
        /** @noinspection PhpDeprecationInspection */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
95
        $components = [
96
            'helper' => Helper::class,
97
            'jsonLd' => JsonLd::class,
98
            'link' => Link::class,
99
            'script' => Script::class,
100
            'tag' => Tag::class,
101
            'title' => Title::class,
102
            'manifest' => [
103
                'class' => Manifest::class,
104
                'manifestService' => Seomatic::$plugin->manifest,
105
            ]
106
        ];
107
108
        $config['components'] = $components;
109
110
        parent::__construct($config);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function init()
117
    {
118
        parent::init();
119
120
        $replaceVars = Seomatic::$loadingMetaContainers || Seomatic::$previewingMetaContainers;
121
        if ($this->meta === null || $replaceVars) {
122
            $this->meta = Seomatic::$plugin->metaContainers->metaGlobalVars;
123
        }
124
        if ($this->site === null || $replaceVars) {
125
            $this->site = Seomatic::$plugin->metaContainers->metaSiteVars;
126
        }
127
        if ($this->sitemap === null || $replaceVars) {
128
            $this->sitemap = Seomatic::$plugin->metaContainers->metaSitemapVars;
129
        }
130
        if ($this->containers === null || $replaceVars) {
131
            $this->containers = Seomatic::$plugin->metaContainers;
132
        }
133
        if ($this->bundles === null || $replaceVars) {
134
            $this->bundles = Seomatic::$plugin->metaBundles;
135
        }
136
        // Set the config settings, parsing the environment if its a frontend request
137
        $configSettings = Seomatic::$settings;
138
        if (!$replaceVars && !Craft::$app->getRequest()->getIsCpRequest()) {
139
            $configSettings->environment = Seomatic::$environment;
140
        }
141
        $this->config = $configSettings;
142
    }
143
144
    /**
145
     * Get the plugin's name
146
     *
147
     * @return null|string
148
     */
149
    public function getPluginName()
150
    {
151
        return Seomatic::$plugin->name;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getEnvironment()
158
    {
159
        return Craft::parseEnv(Seomatic::$environment);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::parseEnv(n...\Seomatic::environment) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getEnvironmentMessage()
166
    {
167
        $result = '';
168
        /** @var Settings $settings */
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...
169
        $settings = Seomatic::$plugin->getSettings();
170
        $settingsEnv = $settings->environment;
171
        if (Seomatic::$craft31) {
172
            $settingsEnv = Craft::parseEnv($settingsEnv);
173
        }
174
        $env = Seomatic::$environment;
175
        $settingsUrl = UrlHelper::cpUrl('seomatic/plugin');
176
        $general = Craft::$app->getConfig()->getGeneral();
177
        if (!$general->allowAdminChanges) {
178
            $settingsUrl = '';
179
        }
180
        // If they've manually overridden the environment, just return it
181
        if (EnvironmentHelper::environmentOverriddenByConfig()) {
182
            return Craft::t('seomatic', 'This is overridden by the `config/seomatic.php` config setting',
183
                []
184
            );
185
        }
186
        // If devMode is on, always force the environment to be 'local'
187
        if (Seomatic::$devMode) {
188
            return Craft::t('seomatic', 'The `{settingsEnv}` [SEOmatic Environment]({settingsUrl}) setting has been overriden to `{env}`, because the `devMode` config setting is enabled. Tracking scripts are disabled, and the `robots` tag is set to `none` to prevent search engine indexing.',
189
                ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl]
190
            );
191
        }
192
        // Try to also check the `ENVIRONMENT` env var
193
        $envVar = getenv('ENVIRONMENT');
194
        if (Seomatic::$settings->manuallySetEnvironment) {
195
            $envVar = $settingsEnv;
196
        }
197
        if (!empty($envVar)) {
198
            $env = EnvironmentHelper::determineEnvironment();
199
            switch ($env) {
200
                case EnvironmentHelper::SEOMATIC_DEV_ENV:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
201
                    $additionalMessage = 'Tracking scripts are disabled, and the `robots` tag is set to `none` to prevent search engine indexing.';
202
                    break;
203
                case EnvironmentHelper::SEOMATIC_STAGING_ENV:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
204
                    $additionalMessage = 'The `robots` tag is set to `none` to prevent search engine indexing.';
205
                    break;
206
                case EnvironmentHelper::SEOMATIC_PRODUCTION_ENV:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
207
                    $additionalMessage = '';
208
                    break;
209
                default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
210
                    $additionalMessage = '';
211
                    break;
212
            }
213
            if ($settings->environment !== $env) {
214
                return Craft::t('seomatic', 'The `{settingsEnv}` [SEOmatic Environment]({settingsUrl}) setting has been overriden to `{env}`, because the `.env` setting `ENVIRONMENT` is set to `{envVar}`. {additionalMessage}',
215
                    ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl, 'envVar' => $envVar, 'additionalMessage' => $additionalMessage]
216
                );
217
            }
218
        }
219
220
        return $result;
221
    }
222
}
223