Passed
Push — master ( 1ead13...659e03 )
by M. Mikkel
03:50
created

Redirect   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B getRedirectUrl() 0 20 7
1
<?php
2
3
namespace mmikkel\cpfieldinspect\services;
4
5
use Craft;
6
use craft\base\Component;
7
use craft\elements\GlobalSet;
8
9
/**
10
 * 
11
 */
12
class Redirect extends Component
13
{
14
15
    /**
16
     * @param string|null $url
17
     * @return string
18
     * @throws \craft\errors\SiteNotFoundException
19
     * @throws \yii\base\Exception
20
     * @throws \yii\base\InvalidConfigException
21
     */
22
    public function getRedirectUrl(?string $url = null): string
23
    {
24
        if (!$url) {
25
            $url = \implode('?', \array_filter([\implode('/', Craft::$app->getRequest()->getSegments()), Craft::$app->getRequest()->getQueryStringWithoutPath()]));
26
        }
27
        // Special case for globals – account for their handles being edited before redirecting back
28
        $segments = \explode('/', $url);
29
        if ($segments[0] ?? null === 'globals') {
30
            if (Craft::$app->getIsMultiSite()) {
31
                $siteHandle = $segments[1] ?? null;
32
                $globalSetHandle = $segments[2] ?? null;
33
            } else {
34
                $siteHandle = Craft::$app->getSites()->getPrimarySite()->handle;
35
                $globalSetHandle = $segments[1] ?? null;
36
            }
37
            if ($siteHandle && $globalSetHandle && $globalSet = GlobalSet::find()->site($siteHandle)->handle($globalSetHandle)->one()) {
38
                $url = "edit/$globalSet->id?site=$siteHandle";
39
            }
40
        }
41
        return Craft::$app->getSecurity()->hashData($url, Craft::$app->getConfig()->getGeneral()->securityKey);
42
    }
43
44
}
45