Passed
Push — v3 ( 1fd07a...7a0627 )
by Andrew
47:04 queued 27:15
created

SeomaticVariable::getEnvironmentMessage()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 35
rs 8.8977
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
use nystudio107\seomatic\variables\ManifestVariable as Manifest;
30
31
use Craft;
32
33
use yii\di\ServiceLocator;
34
35
/**
36
 * Seomatic defines the `seomatic` global template variable.
37
 *
38
 * @property Helper     helper
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
39
 * @property JsonLd     jsonLd
40
 * @property Link       link
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
41
 * @property Script     script
42
 * @property Tag        tag
43
 * @property Title      title
44
 * @property Manifest   manifest
45
 *
46
 * @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...
47
 * @package   Seomatic
48
 * @since     3.0.0
49
 */
50
class SeomaticVariable extends ServiceLocator
51
{
52
    // Properties
53
    // =========================================================================
54
55
    /**
56
     * @var MetaGlobalVars
57
     */
58
    public $meta;
59
60
    /**
61
     * @var MetaSiteVars
62
     */
63
    public $site;
64
65
    /**
66
     * @var MetaSitemapVars
67
     */
68
    public $sitemap;
69
70
    /**
71
     * @var Settings
72
     */
73
    public $config;
74
75
    /**
76
     * @var MetaContainers
77
     */
78
    public $containers;
79
80
    /**
81
     * @var MetaBundles
82
     */
83
    public $bundles;
84
85
    // Public Methods
86
    // =========================================================================
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
89
     * @inheritdoc
90
     */
91
    public function __construct($config = [])
92
    {
93
        /** @noinspection PhpDeprecationInspection */
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...
94
        $components = [
95
            'helper' => Helper::class,
96
            'jsonLd' => JsonLd::class,
97
            'link' => Link::class,
98
            'script' => Script::class,
99
            'tag' => Tag::class,
100
            'title' => Title::class,
101
            'manifest' => Manifest::class
102
        ];
103
104
        $config['components'] = $components;
105
106
        parent::__construct($config);
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public function init()
113
    {
114
        parent::init();
115
116
        $replaceVars = Seomatic::$loadingContainers || Seomatic::$previewingMetaContainers;
117
        if ($this->meta === null || $replaceVars) {
118
            $this->meta = Seomatic::$plugin->metaContainers->metaGlobalVars;
119
        }
120
        if ($this->site === null || $replaceVars) {
121
            $this->site = Seomatic::$plugin->metaContainers->metaSiteVars;
122
        }
123
        if ($this->sitemap === null || $replaceVars) {
124
            $this->sitemap = Seomatic::$plugin->metaContainers->metaSitemapVars;
125
        }
126
        // Set the config settings, parsing the environment if its a frontend request
127
        $configSettings = Seomatic::$settings;
128
        if (!$replaceVars && !Craft::$app->getRequest()->getIsCpRequest()) {
129
            $configSettings->environment = Seomatic::$environment;
130
        }
131
        $this->config = $configSettings;
132
    }
133
134
    /**
135
     * Get the plugin's name
136
     *
137
     * @return null|string
138
     */
139
    public function getPluginName()
140
    {
141
        return Seomatic::$plugin->name;
142
    }
143
144
    /**
145
     * Get all of the meta bundles
146
     *
147
     * @param bool $allSites
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
148
     *
149
     * @return array
150
     */
151
    public function getContentMetaBundles(bool $allSites = true): array
152
    {
153
        return Seomatic::$plugin->metaBundles->getContentMetaBundles($allSites);
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getEnvironment()
160
    {
161
        return Seomatic::$environment;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getEnvironmentMessage()
168
    {
169
        $result = '';
170
        /** @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...
171
        $settings = Seomatic::$plugin->getSettings();
172
        $settingsEnv = $settings->environment;
173
        $env = Seomatic::$environment;
174
        $settingsUrl = UrlHelper::cpUrl('seomatic/plugin');
175
        if (Seomatic::$devMode) {
176
            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.',
177
                ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl]
178
            );
179
        }
180
        $envVar = getenv('ENVIRONMENT');
181
        if (!empty($envVar)) {
182
            $env = EnvironmentHelper::determineEnvironment();
183
            switch ($env) {
184
                case EnvironmentHelper::SEOMATIC_STAGING_ENV:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
185
                    $additionalMessage = 'The `robots` tag is set to `none` to prevent search engine indexing.';
186
                    break;
187
                case EnvironmentHelper::SEOMATIC_PRODUCTION_ENV:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
188
                    $additionalMessage = 'Tracking scripts are disabled, and the `robots` tag is set to `none` to prevent search engine indexing.';
189
                    break;
190
                default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
191
                    $additionalMessage = '';
192
                    break;
193
            }
194
            if ($settings->environment !== $env) {
195
                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}',
196
                    ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl, 'envVar' => $envVar, 'additionalMessage' => $additionalMessage]
197
                );
198
            }
199
        }
200
201
        return $result;
202
    }
203
}
204