Passed
Push — develop ( 9d4d3e...21afce )
by Andrew
24:30 queued 11:13
created

ShortLink::afterElementDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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
use yii\helpers\StringHelper;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @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...
24
 * @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...
25
 *
26
 * @property-read string $contentColumnType
27
 */
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...
28
class ShortLink extends Field implements PreviewableFieldInterface
29
{
30
    public $redirectSrcMatch = 'pathonly';
31
    public $redirectHttpCode = 301;
32
33
    // Static Methods
34
35
    // =========================================================================
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @inheritdoc
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public static function displayName(): string
41
    {
42
        return Craft::t('retour', 'Short Link');
43
    }
44
45
    // Public Methods
46
    // =========================================================================
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @inheritdoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function getContentColumnType(): string
52
    {
53
        return Schema::TYPE_TEXT;
54
    }
55
56
    /**
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...
57
     * @inheritdoc
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59
    public function getInputHtml($value, ElementInterface $element = null): string
60
    {
61
        // Render the input template
62
        return Craft::$app->getView()->renderTemplate(
63
            'retour/_components/fields/ShortLink_input',
64
            [
65
                'name' => $this->handle,
66
                'value' => $value,
67
                'field' => $this,
68
            ]
69
        );
70
    }
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @inheritdoc
74
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
75
    public function getSettingsHtml()
76
    {
77
        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...
78
            [
79
                'field' => $this,
80
            ]);
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...
81
    }
82
83
    /**
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...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function getIsTranslatable(ElementInterface $element = null): bool
87
    {
88
        return false;
89
    }
90
91
    /**
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...
92
     * @inheritdoc
93
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
94
    public function afterElementSave(ElementInterface $element, bool $isNew)
95
    {
96
        if ($element->getIsDraft() || !$element->getSite()->hasUrls) {
97
            return;
98
        }
99
100
        $value = $element->{$this->handle};
101
        RetourPlugin::$plugin->redirects->removeElementRedirect($element);
102
103
        // Return for propagating elements
104
        if ($this->redirectSrcMatch === 'pathonly') {
105
            if ($element->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...
106
                return;
107
            }
108
        } else if (!StringHelper::startsWith($value, 'http')) {
109
            $siteUrl = $element->getSite()->getBaseUrl();
110
            $value = rtrim($siteUrl, '/') . '/' . ltrim($value, '/');
0 ignored issues
show
Bug introduced by
It seems like $siteUrl can also be of type null; however, parameter $string of rtrim() 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

110
            $value = rtrim(/** @scrutinizer ignore-type */ $siteUrl, '/') . '/' . ltrim($value, '/');
Loading history...
111
        }
112
113
        if (!empty($value)) {
114
            RetourPlugin::$plugin->redirects->enableElementRedirect($element, $value, $this->redirectSrcMatch, $this->redirectHttpCode);
115
        }
116
117
        parent::afterElementSave($element, $isNew);
118
    }
119
120
    /**
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...
121
     * @inheritdoc
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    public function afterElementDelete(ElementInterface $element)
124
    {
125
        if ($element->getIsDraft()) {
126
            return;
127
        }
128
129
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true);
130
        parent::afterElementDelete($element);
131
    }
132
133
    /**
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
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...
134
     * @inheritdoc
135
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
136
    public function getTableAttributeHtml($value, ElementInterface $element): string
137
    {
138
        $decoded = Json::decodeIfJson($value);
139
        if ($decoded) {
140
            return $decoded['legacyUrl'] ?? '';
141
        }
142
143
        // Render the input template
144
        return $value;
145
    }
146
}
147