Passed
Branch v3 (3b6795)
by Andrew
07:36
created

ShortLink::afterElementSave()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 26
ccs 0
cts 20
cp 0
rs 8.0555
cc 9
nc 8
nop 2
crap 90
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 nystudio107\retour\Retour as RetourPlugin;
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...
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...
49
     * @inheritdoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function getInputHtml($value, ElementInterface $element = null): string
52
    {
53
        // Render the input template
54
        return Craft::$app->getView()->renderTemplate(
55
            'retour/_components/fields/ShortLink_input',
56
            [
57
                'name' => $this->handle,
58
                'value' => $value,
59
                'field' => $this,
60
            ]
61
        );
62
    }
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @inheritdoc
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function getSettingsHtml()
68
    {
69
        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...
70
            [
71
                'field' => $this,
72
            ]);
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...
73
    }
74
75
    /**
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
Missing short description in doc comment
Loading history...
76
     * @inheritdoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getIsTranslatable(ElementInterface $element = null): bool
79
    {
80
        return false;
81
    }
82
83
    /**
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...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function afterElementSave(ElementInterface $element, bool $isNew)
87
    {
88
        if ($element->getIsDraft() || !$element->getSite()->hasUrls) {
89
            return;
90
        }
91
92
        $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

92
        $value = $element->getFieldValue(/** @scrutinizer ignore-type */ $this->handle);
Loading history...
93
94
        // Return for propagating elements
95
        if ($this->redirectSrcMatch === 'pathonly') {
96
            $parentElement = ElementHelper::rootElement($element);
97
            if ($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...
98
                return;
99
            }
100
        } else if (!empty($value) && !StringHelper::startsWith($value, 'http')) {
101
            $siteUrl = $element->getSite()->getBaseUrl();
102
            $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

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