MetaSiteVars::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 33
ccs 0
cts 2
cp 0
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 Craft;
15
use craft\helpers\DateTimeHelper;
16
use craft\validators\ArrayValidator;
17
use craft\validators\DateTimeValidator;
18
use DateTime;
19
use nystudio107\seomatic\base\VarsModel;
20
use nystudio107\seomatic\helpers\Json as JsonHelper;
21
use Throwable;
22
use function is_array;
23
use function is_string;
24
25
/**
26
 * Site variables object, containing values that determine side-wide settings
27
 *
28
 * @author    nystudio107
29
 * @package   Seomatic
30
 * @since     3.0.0
31
 */
32
class MetaSiteVars extends VarsModel
33
{
34
    // Constants
35
    // =========================================================================
36
37
    const CONTAINER_TYPE = 'MetaSiteVarsContainer';
38
39
    // Public Properties
40
    // =========================================================================
41
    /**
42
     * @var string The name of the website
43
     */
44
    public $siteName = '';
45
46
    /**
47
     * @var string The alternate name of the website
48
     */
49
    public $siteAlternateName = '';
50
51
    /**
52
     * @var Entity|array
53
     */
54
    public $identity;
55
56
    /**
57
     * @var Entity|array
58
     */
59
    public $creator;
60
61
    /**
62
     * @var string The Twitter handle
63
     */
64
    public $twitterHandle = '';
65
66
    /**
67
     * @var string The Facebook profile ID
68
     */
69
    public $facebookProfileId = '';
70
71
    /**
72
     * @var string The Facebook app ID
73
     */
74
    public $facebookAppId = '';
75
76
    /**
77
     * @var string The Google Site Verification code
78
     */
79
    public $googleSiteVerification = '';
80
81
    /**
82
     * @var string The Bing Site Verification code
83
     */
84
    public $bingSiteVerification = '';
85
86
    /**
87
     * @var string The Pinterest Site Verification code
88
     */
89
    public $pinterestSiteVerification = '';
90
91
    /**
92
     * @var string The Facebook Site Verification code
93
     */
94
    public $facebookSiteVerification = '';
95
96
    /**
97
     * @var array Array of links for Same As... sites, indexed by the handle
98
     */
99
    public $sameAsLinks = [];
100
101
    /**
102
     * @var string Google Site Links search target
103
     */
104
    public $siteLinksSearchTarget = '';
105
106
    /**
107
     * @var string Google Site Links query input
108
     */
109
    public $siteLinksQueryInput = '';
110
111
    /**
112
     * @var string Default referrer tag setting
113
     */
114
    public $referrer = 'no-referrer-when-downgrade';
115
116
    /**
117
     * @var array Array of additional custom sitemap URLs
118
     */
119
    public $additionalSitemapUrls = [];
120
121
    /**
122
     * @var DateTime
123
     */
124
    public $additionalSitemapUrlsDateUpdated;
125
126
    /**
127
     * @var array Array of additional sitemaps
128
     */
129
    public $additionalSitemaps = [];
130
131
    /**
132
     * @param array $config
133
     *
134
     * @return null|MetaSiteVars
135
     */
136
    public static function create(array $config = [])
137
    {
138
        $model = new MetaSiteVars($config);
139
        $model->normalizeData();
140
141
        return $model;
142
    }
143
144
    // Public Methods
145
    // =========================================================================
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function init()
151
    {
152
        parent::init();
153
154
        // Set some default values
155
        if (empty($this->siteName)) {
156
            try {
157
                $info = Craft::$app->getInfo();
158
            } catch (Throwable $e) {
159
                $info = null;
160
            }
161
            $siteName = Craft::$app->config->general->siteName;
162
            if (is_array($siteName)) {
163
                $siteName = reset($siteName);
164
            }
165
            $this->siteName = empty($siteName) ?: $info->getName();
0 ignored issues
show
Documentation Bug introduced by
It seems like empty($siteName) ?: $info->getName() can also be of type true. However, the property $siteName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
166
        }
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function rules(): array
173
    {
174
        return [
175
            [
176
                [
177
                    'siteName',
178
                    'siteAlternateName',
179
                    'twitterHandle',
180
                    'facebookProfileId',
181
                    'facebookAppId',
182
                    'googleSiteVerification',
183
                    'bingSiteVerification',
184
                    'pinterestSiteVerification',
185
                    'facebookSiteVerification',
186
                    'siteLinksSearchTarget',
187
                    'siteLinksQueryInput',
188
                    'referrer',
189
                ],
190
                'string',
191
            ],
192
            [
193
                [
194
                    'additionalSitemapUrlsDateUpdated',
195
                ],
196
                DateTimeValidator::class,
197
            ],
198
            [
199
                [
200
                    'sameAsLinks',
201
                    'additionalSitemapUrls',
202
                    'additionalSitemaps',
203
                ],
204
                ArrayValidator::class,
205
            ],
206
        ];
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    public function datetimeAttributes(): array
213
    {
214
        $attributes = parent::datetimeAttributes();
215
216
        if (property_exists($this, 'additionalSitemapUrlsDateUpdated')) {
217
            $attributes[] = 'additionalSitemapUrlsDateUpdated';
218
        }
219
220
        return $attributes;
221
    }
222
223
    /**
224
     * Normalizes model data
225
     */
226
    public function normalizeData()
227
    {
228
        // Decode any JSON data
229
        $properties = $this->getAttributes();
230
        foreach ($properties as $property => $value) {
231
            if (!empty($value) && is_string($value)) {
232
                $this->$property = JsonHelper::decodeIfJson($value);
233
            }
234
        }
235
        // Convert our date attributes in the additionalSitemaps array
236
        if (!empty($this->additionalSitemaps)) {
237
            foreach ($this->additionalSitemaps as $index => $additionalSitemap) {
238
                if (!empty($additionalSitemap['lastmod'])) {
239
                    $this->additionalSitemaps[$index]['lastmod']
240
                        = DateTimeHelper::toDateTime($additionalSitemap['lastmod']);
241
                }
242
            }
243
        }
244
        // Make sure these are strings
245
        if (!empty($this->facebookProfileId)) {
246
            $this->facebookProfileId = (string)$this->facebookProfileId;
247
        }
248
        if (!empty($this->facebookAppId)) {
249
            $this->facebookAppId = (string)$this->facebookAppId;
250
        }
251
        // Identity
252
        if (is_array($this->identity)) {
253
            $this->identity = new Entity($this->identity);
254
        }
255
        // Creator
256
        if (is_array($this->creator)) {
257
            $this->creator = new Entity($this->creator);
258
        }
259
    }
260
}
261