Passed
Push — develop ( a54c5a...15ba0e )
by Andrew
32:09 queued 17:50
created

ShortLink   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 25
eloc 50
c 5
b 1
f 0
dl 0
loc 149
ccs 0
cts 86
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A preventShortLinkUpdates() 0 3 1
A afterElementDelete() 0 8 3
C afterElementSave() 0 35 13
A getIsTranslatable() 0 3 1
A displayName() 0 3 1
A getTableAttributeHtml() 0 9 2
A getInputHtml() 0 20 2
A getSettingsHtml() 0 5 1
A allowShortLinkUpdates() 0 3 1
1
<?php
2
/**
3
 * Retour plugin for Craft CMS
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) 2022 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\Element;
14
use craft\base\ElementInterface;
15
use craft\base\Field;
16
use craft\base\PreviewableFieldInterface;
17
use craft\helpers\ElementHelper;
18
use craft\helpers\Html;
19
use craft\helpers\Json;
20
use craft\helpers\UrlHelper;
21
use nystudio107\retour\Retour as RetourPlugin;
22
use yii\helpers\StringHelper;
23
24
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
 * @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...
26
 * @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...
27
 * @since     3.2.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...
28
 *
29
 * @property-read string $contentColumnType
30
 */
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...
31
class ShortLink extends Field implements PreviewableFieldInterface
32
{
33
    public $redirectSrcMatch = 'pathonly';
34
    public $redirectHttpCode = 301;
35
36
    static protected $allowShortLinkUpdates = true;
37
38
    // Static Methods
39
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @inheritdoc
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public static function displayName(): string
46
    {
47
        return Craft::t('retour', 'Short Link');
48
    }
49
50
    /**
51
     * Prevent element updates from updating the short link redirects.
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public static function preventShortLinkUpdates()
54
    {
55
        self::$allowShortLinkUpdates = false;
56
    }
57
58
    /**
59
     * Allow element updates to update the short link redirects.
60
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
61
    public static function allowShortLinkUpdates()
62
    {
63
        self::$allowShortLinkUpdates = true;
64
    }
65
66
    // Public Methods
67
    // =========================================================================
68
69
    /**
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...
70
     * @inheritdoc
71
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
72
    public function getInputHtml($value, ElementInterface $element = null): string
73
    {
74
        // Get our id & namespaceId
75
        $craft35 = version_compare(Craft::$app->getVersion(), '3.5', '>=');
76
        if ($craft35) {
77
            $id = Html::id($this->handle);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type null; however, parameter $id of craft\helpers\Html::id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $id = Html::id(/** @scrutinizer ignore-type */ $this->handle);
Loading history...
78
        } else {
79
            $id = Craft::$app->getView()->formatInputId($this->handle);
80
        }
81
        $namespacedId = Craft::$app->getView()->namespaceInputId($id);
82
83
        // Render the input template
84
        return Craft::$app->getView()->renderTemplate(
85
            'retour/_components/fields/ShortLink_input',
86
            [
87
                'name' => $this->handle,
88
                'value' => $value,
89
                'field' => $this,
90
                'id' => $id,
91
                'namespacedId' => $namespacedId,
92
            ]
93
        );
94
    }
95
96
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
97
     * @inheritdoc
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function getSettingsHtml()
100
    {
101
        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...
102
            [
103
                'field' => $this,
104
            ]);
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...
105
    }
106
107
    /**
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...
108
     * @inheritdoc
109
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
110
    public function getIsTranslatable(ElementInterface $element = null): bool
111
    {
112
        return false;
113
    }
114
115
    /**
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...
116
     * @inheritdoc
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118
    public function afterElementSave(ElementInterface $element, bool $isNew)
119
    {
120
        if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) {
121
            return;
122
        }
123
124
        $value = $element->getFieldValue($this->handle);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type null; however, parameter $fieldHandle of craft\base\ElementInterface::getFieldValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        $value = $element->getFieldValue(/** @scrutinizer ignore-type */ $this->handle);
Loading history...
125
126
        // Return for propagating elements
127
        if ($this->redirectSrcMatch === 'pathonly') {
128
            $parentElement = ElementHelper::rootElement($element);
129
            if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) {
0 ignored issues
show
Bug introduced by
Accessing propagating on the interface craft\base\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
130
                return;
131
            }
132
        } else if (!empty($value) && !StringHelper::startsWith($value, 'http')) {
133
            $value = UrlHelper::siteUrl($value, null, null, $element->siteId);
0 ignored issues
show
Bug introduced by
Accessing siteId on the interface craft\base\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
134
        }
135
136
        $parentElement = ElementHelper::rootElement($element);
137
        RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false);
138
139
        if (!empty($value)) {
140
            $redirectSrcMatch = $this->redirectSrcMatch;
141
142
            if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) {
143
                if (!UrlHelper::isAbsoluteUrl($value)) {
144
                    $value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId);
145
                    $redirectSrcMatch = 'fullurl';
146
                }
147
            }
148
149
            RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode);
150
        }
151
152
        parent::afterElementSave($element, $isNew);
153
    }
154
155
    /**
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...
156
     * @inheritdoc
157
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
158
    public function afterElementDelete(ElementInterface $element)
159
    {
160
        if ($element->getIsDraft() || $element->getIsRevision()) {
161
            return;
162
        }
163
164
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true);
165
        parent::afterElementDelete($element);
166
    }
167
168
    /**
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...
169
     * @inheritdoc
170
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
171
    public function getTableAttributeHtml($value, ElementInterface $element): string
172
    {
173
        $decoded = Json::decodeIfJson($value);
174
        if ($decoded) {
175
            return $decoded['legacyUrl'] ?? '';
176
        }
177
178
        // Render the input template
179
        return $value;
180
    }
181
}
182