Passed
Push — master ( cab269...25ed85 )
by M. Mikkel
05:48
created

CpFieldInspectHelper::getRedirectUrl()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 20
rs 8.8333
1
<?php
2
3
namespace mmikkel\cpfieldinspect\helpers;
4
5
use Craft;
6
use craft\base\ElementInterface;
0 ignored issues
show
Bug introduced by
The type craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use craft\elements\Asset;
8
use craft\elements\Category;
9
use craft\elements\Entry;
10
use craft\elements\GlobalSet;
11
use craft\elements\User;
12
use craft\commerce\elements\Product;
13
use craft\helpers\Html;
14
use craft\helpers\UrlHelper;
15
use craft\models\EntryType;
0 ignored issues
show
Bug introduced by
The type craft\models\EntryType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
use yii\base\InvalidConfigException;
18
19
class CpFieldInspectHelper
20
{
21
22
    /**
23
     * @param string|null $url
24
     * @return string
25
     * @throws \craft\errors\SiteNotFoundException
26
     * @throws \yii\base\Exception
27
     * @throws \yii\base\InvalidConfigException
28
     */
29
    public static function getRedirectUrl(?string $url = null): string
30
    {
31
        if (!$url) {
32
            $url = implode('?', array_filter([implode('/', Craft::$app->getRequest()->getSegments()), Craft::$app->getRequest()->getQueryStringWithoutPath()]));
33
        }
34
        // Special case for globals – account for their handles being edited before redirecting back
35
        $segments = explode('/', $url);
36
        if (($segments[0] ?? null) === 'globals') {
37
            if (Craft::$app->getIsMultiSite()) {
38
                $siteHandle = $segments[1] ?? null;
39
                $globalSetHandle = $segments[2] ?? null;
40
            } else {
41
                $siteHandle = Craft::$app->getSites()->getPrimarySite()->handle;
42
                $globalSetHandle = $segments[1] ?? null;
43
            }
44
            if ($siteHandle && $globalSetHandle && $globalSet = GlobalSet::find()->site($siteHandle)->handle($globalSetHandle)->one()) {
45
                $url = "edit/$globalSet->id?site=$siteHandle";
46
            }
47
        }
48
        return Craft::$app->getSecurity()->hashData($url);
0 ignored issues
show
Bug introduced by
The call to yii\base\Security::hashData() has too few arguments starting with key. ( Ignorable by Annotation )

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

48
        return Craft::$app->getSecurity()->/** @scrutinizer ignore-call */ hashData($url);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
    }
50
51
    /**
52
     * @throws InvalidConfigException
53
     */
54
    public static function renderEditSourceLink(array $context): string
55
    {
56
        $element = $context['element'] ?? $context['entry'] ?? $context['asset'] ?? $context['globalSet'] ?? $context['user'] ?? $context['category'] ?? $context['product'] ?? null;
57
        if (empty($element)) {
58
            return '';
59
        }
60
        return static::getEditElementSourceButton($element);
61
    }
62
63
    /**
64
     * @param ElementInterface|null $element
65
     * @param array $attributes
66
     * @param string|null $size
67
     * @return string
68
     * @throws \yii\base\InvalidConfigException
69
     */
70
    public static function getEditElementSourceButton(?ElementInterface $element, array $attributes = [], ?string $size = null): string
71
    {
72
        if (empty($element)) {
73
            return '';
74
        }
75
        $html = '';
76
        if ($element instanceof Entry) {
77
            if (!empty($element->typeId)) {
78
                $html .= static::_getEditSourceButtonHtml(
79
                    label: 'Edit entry type',
80
                    path: "settings/entry-types/$element->typeId",
81
                    attributes: [
82
                        'data-type-id' => $element->typeId,
83
                    ],
84
                    size: $size
85
                );
86
            }
87
            if (!empty($element->sectionId)) {
88
                $html .= static::_getEditSourceButtonHtml(label: 'Edit section', path: "settings/sections/$element->sectionId", size: $size);
89
            }
90
        } else if ($element instanceof Asset) {
91
            $html = static::_getEditSourceButtonHtml(label: 'Edit volume', path: "settings/assets/volumes/{$element->volumeId}", size: $size);
92
        } else if ($element instanceof GlobalSet) {
93
            $html = static::_getEditSourceButtonHtml(label: 'Edit global set', path: "settings/globals/{$element->id}", size: $size);
94
        } else if ($element instanceof User) {
95
            $html = static::_getEditSourceButtonHtml('Edit settings', 'settings/users/fields', [
96
                'style' => 'margin-top:20px;',
97
            ], $size);
98
        } else if ($element instanceof Category) {
99
            $html = static::_getEditSourceButtonHtml(label: 'Edit category group', path: "settings/categories/{$element->groupId}", size: $size);
100
        } else if (class_exists(Product::class) && $element instanceof Product) {
101
            $html = static::_getEditSourceButtonHtml(label: 'Edit product type', path: "commerce/settings/producttypes/{$element->typeId}", size: $size);
102
        }
103
        if (empty($html)) {
104
            return '';
105
        }
106
        return Html::tag('div', $html, [
107
            ...$attributes,
108
            'class' => [
109
                'cp-field-inspect-sourcebtn-wrapper flex',
110
                ...$attributes['class'] ?? [],
111
            ],
112
        ]);
113
    }
114
115
    /**
116
     * @param string $label
117
     * @param string $path
118
     * @param array $attributes
119
     * @param string|null $size
120
     * @return string
121
     */
122
    private static function _getEditSourceButtonHtml(string $label, string $path, array $attributes = [], ?string $size = null): string
123
    {
124
        return Html::tag('a', Craft::t('cp-field-inspect', $label), [
125
            'href' => UrlHelper::cpUrl($path),
126
            'class' => [
127
                'btn settings icon',
128
                $size === 'small' ? 'small' : null,
129
            ],
130
            'data-cp-field-inspect-sourcebtn' => true,
131
            ...$attributes,
132
        ]);
133
    }
134
135
}
136