StaticRedirects   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 63
dl 0
loc 160
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 61 1
A behaviors() 0 5 1
A init() 0 6 2
1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\models;
13
14
use nystudio107\retour\validators\DbStringValidator;
15
use nystudio107\retour\validators\ParsedUriValidator;
16
use nystudio107\retour\validators\UriValidator;
17
use yii\behaviors\AttributeTypecastBehavior;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
21
 * @package   Retour
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
22
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class StaticRedirects extends DbModel
25
{
26
    // Public Properties
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var int The id of the redirect.
31
     */
32
    public int $id = 0;
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var null|int The siteId of the redirect (0 or null for all sites).
36
     */
37
    public ?int $siteId = null;
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var int The id of the Element associated with this redirect (unused/vestigial).
41
     */
42
    public int $associatedElementId = 0;
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var bool Whether the redirect is enabled or not.
46
     */
47
    public bool $enabled = true;
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var string The unparsed URL pattern that Retour should match
51
     */
52
    public string $redirectSrcUrl = '';
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var string The parsed URL pattern that Retour should match
56
     */
57
    public string $redirectSrcUrlParsed = '';
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var string Should the legacy URL be matched by path or by full URL?
61
     */
62
    public string $redirectSrcMatch = '';
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @var string Whether an `exactmatch` or `regexmatch` should be used when matching the URL.
66
     */
67
    public string $redirectMatchType = '';
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var string The URL that should be redirected to.
71
     */
72
    public string $redirectDestUrl = '';
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @var int The http status code that should be used for the redirect.
76
     */
77
    public int $redirectHttpCode = 301;
78
79
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
80
     * @var int The priority that determines the selected redirect in case of multiple matches
81
     */
82
    public int $priority = 5;
83
84
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
85
     * @var int The number of times this redirect has been hit.
86
     */
87
    public int $hitCount = 0;
88
89
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
90
     * @var string A datetime string of when this redirect was last hit
91
     */
92
    public string $hitLastTime = '';
93
94
    // Public Methods
95
    // =========================================================================
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @inheritDoc
99
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
100
    public function init(): void
101
    {
102
        parent::init();
103
        // If the redirectSrcUrl starts with `http`, default the match type to `fullurl`
104
        if (strpos($this->redirectSrcUrl, 'http') === 0) {
105
            $this->redirectSrcMatch = 'fullurl';
106
        }
107
    }
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
110
     * @inheritdoc
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function rules(): array
113
    {
114
        return [
115
            ['id', 'integer'],
116
            ['siteId', 'integer'],
117
            ['siteId', 'default', 'value' => null],
118
            ['associatedElementId', 'default', 'value' => 0],
119
            ['associatedElementId', 'integer'],
120
            ['enabled', 'boolean'],
121
            ['redirectSrcMatch', 'string'],
122
            ['redirectSrcMatch', 'in', 'range' => ['pathonly', 'fullurl']],
123
            ['redirectSrcMatch', 'default', 'value' => 'pathonly'],
124
            ['redirectSrcMatch', DbStringValidator::class, 'max' => 32],
125
            ['redirectMatchType', 'string'],
126
            ['redirectMatchType', 'in', 'range' => ['exactmatch', 'regexmatch']],
127
            ['redirectMatchType', 'default', 'value' => 'exactmatch'],
128
            ['redirectMatchType', DbStringValidator::class, 'max' => 32],
129
            [
130
                [
131
                    'redirectSrcUrl',
132
                    'redirectSrcUrlParsed',
133
                    'redirectDestUrl',
134
                ],
135
                'default',
136
                'value' => '',
137
            ],
138
            ['redirectSrcUrlParsed', ParsedUriValidator::class, 'source' => 'redirectSrcUrl'],
139
            [
140
                [
141
                    'redirectSrcUrl',
142
                    'redirectSrcUrlParsed',
143
                    'redirectDestUrl',
144
                ],
145
                UriValidator::class,
146
            ],
147
            [
148
                [
149
                    'redirectSrcUrl',
150
                    'redirectSrcUrlParsed',
151
                    'redirectDestUrl',
152
                ],
153
                DbStringValidator::class,
154
                'max' => 255,
155
            ],
156
            [
157
                [
158
                    'redirectSrcUrl',
159
                    'redirectSrcUrlParsed',
160
                    'redirectDestUrl',
161
                ],
162
                'string',
163
            ],
164
            ['redirectHttpCode', 'integer'],
165
            ['redirectHttpCode', 'in', 'range' => [301, 302, 307, 308, 410]],
166
            ['redirectHttpCode', 'default', 'value' => 301],
167
            ['priority', 'default', 'value' => 5],
168
            ['priority', 'integer'],
169
            ['priority', 'in', 'range' => [1, 2, 3, 4, 5, 6, 7, 8, 9]],
170
            ['hitCount', 'default', 'value' => 0],
171
            ['hitCount', 'integer'],
172
            ['hitLastTime', 'safe'],
173
        ];
174
    }
175
176
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
177
     * @return array
178
     */
179
    public function behaviors(): array
180
    {
181
        return [
182
            'typecast' => [
183
                'class' => AttributeTypecastBehavior::class,
184
                // 'attributeTypes' will be composed automatically according to `rules()`
185
            ],
186
        ];
187
    }
188
}
189