Completed
Push — master ( b09517...57901d )
by
unknown
18:40
created

UrlHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 94
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildEndpointPath() 0 4 1
A providerBaseUrl() 0 19 4
A buildEndpointUrl() 0 17 3
1
<?php
2
3
4
namespace flipbox\saml\core\helpers;
5
6
use craft\helpers\UrlHelper as CraftUrlHelper;
7
use craft\records\Site;
8
use flipbox\saml\core\models\AbstractSettings;
9
use flipbox\saml\core\records\AbstractProvider;
10
11
class UrlHelper extends CraftUrlHelper
12
{
13
    /**
14
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
15
     * @see GeneralConfig::$loginPath
16
     *
17
     * @var string
18
     */
19
    const LOGIN_ENDPOINT = 'login';
20
21
    /**
22
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
23
     * @see GeneralConfig::$loginPath
24
     *
25
     * @var string
26
     */
27
    const LOGOUT_ENDPOINT = 'logout';
28
29
    /**
30
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
31
     * @see GeneralConfig::$loginPath
32
     *
33
     * @var string
34
     */
35
    const LOGIN_REQUEST_ENDPOINT = 'login/request';
36
37
    /**
38
     * This is the endpoint used to initiate logout. In this case, `logoutPath` cannot be used.
39
     * Point your logout button to this endpoint.
40
     *
41
     * @var string
42
     */
43
    const LOGOUT_REQUEST_ENDPOINT = 'logout/request';
44
45
    /**
46
     * @param AbstractSettings $settings
47
     * @param string $endpoint
48
     * @return string
49
     */
50
    public static function buildEndpointPath(AbstractSettings $settings, string $endpoint)
51
    {
52
        return implode('/', [$settings->getEndpointPrefix(), $endpoint,]);
53
    }
54
55
    /**
56
     * @param string $baseUrl
57
     * @return string
58
     */
59
    protected static function providerBaseUrl(string $baseUrl)
60
    {
61
        $url = '';
62
        // alias
63
        if (strpos($baseUrl, '@') === 0) {
64
            $url = \Craft::getAlias($baseUrl);
65
        }
66
        // env var
67
        if (strpos($baseUrl, '$') === 0) {
68
            $url = \Craft::parseEnv($baseUrl);
69
        }
70
        // url
71
        if (strpos($baseUrl, 'http') === 0) {
72
            $url = $baseUrl;
73
        }
74
75
        // Trim last /
76
        return preg_replace('#/$#', '', $url);
77
    }
78
79
    /**
80
     * @param AbstractSettings $settings
81
     * @param string $endpoint
82
     * @param AbstractProvider $provider
83
     * @param bool $fullUrl
84
     * @return string
85
     * @throws \yii\base\Exception
86
     */
87
    public static function buildEndpointUrl(AbstractSettings $settings, string $endpoint, AbstractProvider $provider, $fullUrl = true)
88
    {
89
        $uri = implode('/', [$settings->getEndpointPrefix(), $endpoint, $provider->uid]);
90
        /** @var Site $site */
91
        $site = $provider->site;
0 ignored issues
show
Documentation introduced by
The property site does not exist on object<flipbox\saml\core...cords\AbstractProvider>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
93
94
95
        $endpointUrl = $fullUrl ?  implode('/', [
96
            static::providerBaseUrl(
97
                $site ? (string)$site->baseUrl : UrlHelper::baseUrl()
98
            ),
99
            $uri
100
        ]) : "/" . $uri;
101
102
        return $endpointUrl;
103
    }
104
}
105