Passed
Push — develop ( 23539e...9d4d3e )
by Andrew
12:19
created

ShortLink   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 27
c 2
b 0
f 0
dl 0
loc 97
ccs 0
cts 53
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A afterElementDelete() 0 8 2
A afterElementSave() 0 12 3
A getContentColumnType() 0 3 1
A displayName() 0 3 1
A getTableAttributeHtml() 0 9 2
A getInputHtml() 0 9 1
A getSettingsHtml() 0 5 1
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
6
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @license   https://nystudio107.com/license
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
8
 */
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...
9
10
namespace nystudio107\retour\fields;
11
12
use Craft;
13
use craft\base\ElementInterface;
14
use craft\base\Field;
15
use craft\base\PreviewableFieldInterface;
16
use craft\helpers\Json;
17
use nystudio107\retour\Retour as RetourPlugin;
18
use yii\db\Schema;
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.1.74
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
 *
25
 * @property-read string $contentColumnType
26
 */
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...
27
class ShortLink extends Field implements PreviewableFieldInterface
28
{
29
    public $redirectSrcMatch = 'pathonly';
30
    public $redirectHttpCode = 301;
31
32
    // Static Methods
33
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @inheritdoc
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public static function displayName(): string
40
    {
41
        return Craft::t('retour', 'Short Link');
42
    }
43
44
    // Public Methods
45
    // =========================================================================
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @inheritdoc
49
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
50
    public function getContentColumnType(): string
51
    {
52
        return Schema::TYPE_TEXT;
53
    }
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
56
     * @inheritdoc
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public function getInputHtml($value, ElementInterface $element = null): string
59
    {
60
        // Render the input template
61
        return Craft::$app->getView()->renderTemplate(
62
            'retour/_components/fields/ShortLink_input',
63
            [
64
                'name' => $this->handle,
65
                'value' => $value,
66
                'field' => $this,
67
            ]
68
        );
69
    }
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
72
     * @inheritdoc
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    public function getSettingsHtml()
75
    {
76
        return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
77
            [
78
                'field' => $this,
79
            ]);
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $isNew should have a doc-comment as per coding-style.
Loading history...
83
     * @inheritdoc
84
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85
    public function afterElementSave(ElementInterface $element, bool $isNew)
86
    {
87
        if ($element->getIsDraft()) {
88
            return;
89
        }
90
91
        $value = $element->{$this->handle};
92
        RetourPlugin::$plugin->redirects->removeElementRedirect($element);
93
        if (!empty($value)) {
94
            RetourPlugin::$plugin->redirects->enableElementRedirect($element, $value, $this->redirectSrcMatch, $this->redirectHttpCode);
95
        }
96
        parent::afterElementSave($element, $isNew);
97
    }
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
100
     * @inheritdoc
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function afterElementDelete(ElementInterface $element)
103
    {
104
        if ($element->getIsDraft()) {
105
            return;
106
        }
107
108
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true);
109
        parent::afterElementDelete($element);
110
    }
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
113
     * @inheritdoc
114
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
115
    public function getTableAttributeHtml($value, ElementInterface $element): string
116
    {
117
        $decoded = Json::decodeIfJson($value);
118
        if ($decoded) {
119
            return $decoded['legacyUrl'] ?? '';
120
        }
121
122
        // Render the input template
123
        return $value;
124
    }
125
}
126