Passed
Push — v3 ( 0d1d42...e1dcac )
by Andrew
32:56 queued 25:33
created

ShortLink::allowShortLinkUpdates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\ElementHelper;
17
use craft\helpers\Json;
18
use craft\helpers\UrlHelper;
19
use nystudio107\retour\Retour as RetourPlugin;
20
use yii\helpers\StringHelper;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @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...
25
 * @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...
26
 *
27
 * @property-read string $contentColumnType
28
 */
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...
29
class ShortLink extends Field implements PreviewableFieldInterface
30
{
31
    public $redirectSrcMatch = 'pathonly';
32
    public $redirectHttpCode = 301;
33
34
    static protected $allowShortLinkUpdates = true;
35
36
    // Static Methods
37
38
    // =========================================================================
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @inheritdoc
42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
43
    public static function displayName(): string
44
    {
45
        return Craft::t('retour', 'Short Link');
46
    }
47
48
    /**
49
     * Prevent element updates from updating the short link redirects.
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public static function preventShortLinkUpdates()
52
    {
53
        self::$allowShortLinkUpdates = false;
54
    }
55
56
    /**
57
     * Allow element updates to update the short link redirects.
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59
    public static function allowShortLinkUpdates()
60
    {
61
        self::$allowShortLinkUpdates = true;
62
    }
63
64
    // Public Methods
65
    // =========================================================================
66
67
    /**
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...
68
     * @inheritdoc
69
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
    public function getInputHtml($value, ElementInterface $element = null): string
71
    {
72
        // Render the input template
73
        return Craft::$app->getView()->renderTemplate(
74
            'retour/_components/fields/ShortLink_input',
75
            [
76
                'name' => $this->handle,
77
                'value' => $value,
78
                'field' => $this,
79
            ]
80
        );
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function getSettingsHtml()
87
    {
88
        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...
89
            [
90
                'field' => $this,
91
            ]);
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...
92
    }
93
94
    /**
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...
95
     * @inheritdoc
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function getIsTranslatable(ElementInterface $element = null): bool
98
    {
99
        return false;
100
    }
101
102
    /**
0 ignored issues
show
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...
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @inheritdoc
104
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
    public function afterElementSave(ElementInterface $element, bool $isNew)
106
    {
107
        if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) {
108
            return;
109
        }
110
111
        $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

111
        $value = $element->getFieldValue(/** @scrutinizer ignore-type */ $this->handle);
Loading history...
112
113
        // Return for propagating elements
114
        if ($this->redirectSrcMatch === 'pathonly') {
115
            $parentElement = ElementHelper::rootElement($element);
116
            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...
117
                return;
118
            }
119
        } else if (!empty($value) && !StringHelper::startsWith($value, 'http')) {
120
            $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...
121
        }
122
123
        if (empty($value)) {
124
            RetourPlugin::$plugin->redirects->removeElementRedirect($element, false);
125
        } else {
126
            RetourPlugin::$plugin->redirects->enableElementRedirect($element, $value, $this->redirectSrcMatch, $this->redirectHttpCode);
127
        }
128
129
        parent::afterElementSave($element, $isNew);
130
    }
131
132
    /**
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...
133
     * @inheritdoc
134
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
135
    public function afterElementDelete(ElementInterface $element)
136
    {
137
        if ($element->getIsDraft()) {
138
            return;
139
        }
140
141
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true);
142
        parent::afterElementDelete($element);
143
    }
144
145
    /**
0 ignored issues
show
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...
Coding Style introduced by
Missing short description in doc comment
Loading history...
146
     * @inheritdoc
147
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
148
    public function getTableAttributeHtml($value, ElementInterface $element): string
149
    {
150
        $decoded = Json::decodeIfJson($value);
151
        if ($decoded) {
152
            return $decoded['legacyUrl'] ?? '';
153
        }
154
155
        // Render the input template
156
        return $value;
157
    }
158
}
159