Redirects   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 60
dl 0
loc 153
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 5 1
A rules() 0 59 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 Redirects 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 number of times this redirect has been hit.
81
     */
82
    public int $hitCount = 0;
83
84
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
85
     * @var string A datetime string of when this redirect was last hit
86
     */
87
    public string $hitLastTime = '';
88
89
    // Public Methods
90
    // =========================================================================
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @inheritDoc
94
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
95
    public function init(): void
96
    {
97
        parent::init();
98
        // If the redirectSrcUrl starts with `http`, default the match type to `fullurl`
99
        if (strpos($this->redirectSrcUrl, 'http') === 0) {
100
            $this->redirectSrcMatch = 'fullurl';
101
        }
102
    }
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @inheritdoc
106
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
107
    public function rules(): array
108
    {
109
        return [
110
            ['id', 'integer'],
111
            ['siteId', 'integer'],
112
            ['siteId', 'default', 'value' => null],
113
            ['associatedElementId', 'default', 'value' => 0],
114
            ['associatedElementId', 'integer'],
115
            ['enabled', 'boolean'],
116
            ['redirectSrcMatch', 'string'],
117
            ['redirectSrcMatch', 'in', 'range' => ['pathonly', 'fullurl']],
118
            ['redirectSrcMatch', 'default', 'value' => 'pathonly'],
119
            ['redirectSrcMatch', DbStringValidator::class, 'max' => 32],
120
            ['redirectMatchType', 'string'],
121
            ['redirectMatchType', 'in', 'range' => ['exactmatch', 'regexmatch']],
122
            ['redirectMatchType', 'default', 'value' => 'exactmatch'],
123
            ['redirectMatchType', DbStringValidator::class, 'max' => 32],
124
            [
125
                [
126
                    'redirectSrcUrl',
127
                    'redirectSrcUrlParsed',
128
                    'redirectDestUrl',
129
                ],
130
                'default',
131
                'value' => '',
132
            ],
133
            ['redirectSrcUrlParsed', ParsedUriValidator::class, 'source' => 'redirectSrcUrl'],
134
            [
135
                [
136
                    'redirectSrcUrl',
137
                    'redirectSrcUrlParsed',
138
                    'redirectDestUrl',
139
                ],
140
                UriValidator::class,
141
            ],
142
            [
143
                [
144
                    'redirectSrcUrl',
145
                    'redirectSrcUrlParsed',
146
                    'redirectMatchType',
147
                    'redirectDestUrl',
148
                ],
149
                DbStringValidator::class,
150
                'max' => 255,
151
            ],
152
            [
153
                [
154
                    'redirectSrcUrl',
155
                    'redirectSrcUrlParsed',
156
                    'redirectDestUrl',
157
                ],
158
                'string',
159
            ],
160
            ['redirectHttpCode', 'integer'],
161
            ['redirectHttpCode', 'default', 'value' => 301],
162
            ['redirectHttpCode', 'in', 'range' => [301, 302, 307, 308, 410]],
163
            ['hitCount', 'default', 'value' => 0],
164
            ['hitCount', 'integer'],
165
            ['hitLastTime', 'safe'],
166
        ];
167
    }
168
169
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
170
     * @return array
171
     */
172
    public function behaviors(): array
173
    {
174
        return [
175
            'typecast' => [
176
                'class' => AttributeTypecastBehavior::class,
177
                // 'attributeTypes' will be composed automatically according to `rules()`
178
            ],
179
        ];
180
    }
181
}
182