Passed
Push — v3 ( 2b006a...43f239 )
by Andrew
33:34 queued 16:32
created

Redirects::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
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
18
use yii\behaviors\AttributeTypecastBehavior;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @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...
23
 * @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...
24
 */
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...
25
class Redirects extends DbModel
26
{
27
    // Public Properties
28
    // =========================================================================
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @var int The id of the redirect.
32
     */
33
    public $id;
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var null|int The siteId of the redirect (0 or null for all sites).
37
     */
38
    public $siteId;
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var int The id of the Element associated with this redirect (unused/vestigial).
42
     */
43
    public $associatedElementId;
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var bool Whether the redirect is enabled or not.
47
     */
48
    public $enabled = true;
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @var string The unparsed URL pattern that Retour should match.
52
     */
53
    public $redirectSrcUrl;
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @var string The parsed URL pattern that Retour should match.
57
     */
58
    public $redirectSrcUrlParsed;
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @var string Should the legacy URL be matched by path or by full URL?
62
     */
63
    public $redirectSrcMatch;
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var string Whether an `exactmatch` or `regexmatch` should be used when matching the URL.
67
     */
68
    public $redirectMatchType;
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @var string The URL that should be redirected to.
72
     */
73
    public $redirectDestUrl;
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
76
     * @var int The http status code that should be used for the redirect.
77
     */
78
    public $redirectHttpCode;
79
80
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
81
     * @var int The number of times this redirect has been hit.
82
     */
83
    public $hitCount;
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
86
     * @var string A datetime string of when this redirect was last hit
87
     */
88
    public $hitLastTime;
89
90
    // Public Methods
91
    // =========================================================================
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @inheritDoc
95
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
96
    public function init()
97
    {
98
        parent::init();
99
        // If the redirectSrcUrl starts with `http`, default the match type to `fullurl`
100
        if (strpos($this->redirectSrcUrl, 'http') === 0) {
101
            $this->redirectSrcMatch = 'fullurl';
102
        }
103
    }
104
105
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
106
     * @inheritdoc
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108
    public function rules()
109
    {
110
        return [
111
            ['id', 'integer'],
112
            ['siteId', 'integer'],
113
            ['siteId', 'default', 'value' => null],
114
            ['associatedElementId', 'default', 'value' => 0],
115
            ['associatedElementId', 'integer'],
116
            ['enabled', 'boolean'],
117
            ['redirectSrcMatch', 'default', 'value' => 'pathonly'],
118
            ['redirectSrcMatch', DbStringValidator::class, 'max' => 32],
119
            ['redirectSrcMatch', 'string'],
120
            ['redirectMatchType', 'default', 'value' => 'exactmatch'],
121
            ['redirectMatchType', DbStringValidator::class, 'max' => 32],
122
            ['redirectMatchType', 'string'],
123
            [
124
                [
125
                    'redirectSrcUrl',
126
                    'redirectSrcUrlParsed',
127
                    'redirectDestUrl',
128
                ],
129
                'default',
130
                'value' => ''
131
            ],
132
            ['redirectSrcUrlParsed', ParsedUriValidator::class, 'source' => 'redirectSrcUrl'],
133
            [
134
                [
135
                    'redirectSrcUrl',
136
                    'redirectSrcUrlParsed',
137
                    'redirectDestUrl',
138
                ],
139
                UriValidator::class,
140
            ],
141
            [
142
                [
143
                    'redirectSrcUrl',
144
                    'redirectSrcUrlParsed',
145
                    'redirectMatchType',
146
                    'redirectDestUrl',
147
                ],
148
                DbStringValidator::class,
149
                'max' => 255
150
            ],
151
            [
152
                [
153
                    'redirectSrcUrl',
154
                    'redirectSrcUrlParsed',
155
                    'redirectDestUrl',
156
                ],
157
                'string'
158
            ],
159
            ['redirectHttpCode', 'integer'],
160
            ['redirectHttpCode', 'default', 'value' => 301],
161
            ['hitCount', 'default', 'value' => 0],
162
            ['hitCount', 'integer'],
163
            ['hitLastTime', 'safe'],
164
        ];
165
    }
166
167
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
168
     * @return array
169
     */
170
    public function behaviors()
171
    {
172
        return [
173
            'typecast' => [
174
                'class' => AttributeTypecastBehavior::class,
175
                // 'attributeTypes' will be composed automatically according to `rules()`
176
            ],
177
        ];
178
    }
179
}
180