Passed
Push — master ( 927c98...cc3ba0 )
by Dāvis
09:35
created

Uri::buildUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Openidconnect\Provider;
4
5
use Sludio\HelperBundle\Openidconnect\Component\Uriable;
6
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
9
class Uri implements Uriable
10
{
11
    protected $params;
12
    protected $urlParams;
13
    protected $url;
14
    protected $base;
15
16
    public function __construct(array $options, array $additional = [], $useSession = false, $method = OpenIDConnectProvider::METHOD_POST)
17
    {
18
        $this->base = rtrim($additional['base_uri'], '/').'/';
19
        unset($additional['base_uri']);
20
21
        $this->params = !empty($options['params']) ? $options['params'] : [];
22
23
        if ($method === OpenIDConnectProvider::METHOD_GET) {
24
            if (isset($options['url_params']['post_logout_redirect_uri'])) {
25
                $options['url_params']['post_logout_redirect_uri'] = $additional['redirect_uri'];
26
                unset($additional['redirect_uri']);
27
            }
28
29
            if (isset($options['url_params']['id_token_hint'], $_SESSION['id_token'])) {
30
                if ($useSession === false) {
31
                    throw new \InvalidArgumentException(sprintf('"%s" parameter must be set in order to use id_token_hint', 'use_session'));
32
                }
33
                $additional['id_token_hint'] = $_SESSION['id_token'];
34
                unset($options['url_params']['id_token_hint']);
35
            }
36
            $this->urlParams = !empty($options['url_params']) ? array_merge($options['url_params'], $additional) : $additional;
37
        }
38
    }
39
40
    public function redirect()
41
    {
42
        return new RedirectResponse($this->getUrl());
43
    }
44
45
    /**
46
     * Get the value of Url
47
     *
48
     * @return mixed
49
     */
50
    public function getUrl()
51
    {
52
        $this->buildUrl();
53
54
        return $this->url;
55
    }
56
57
    /**
58
     * Set the value of Url
59
     *
60
     * @param mixed $url
61
     *
62
     * @return self
63
     */
64
    public function setUrl($url)
65
    {
66
        $this->url = $url;
67
68
        return $this;
69
    }
70
71
    private function buildUrl()
72
    {
73
        $url = $this->base;
74
        if (!empty($this->params)) {
75
            $url .= implode('/', $this->params);
76
        }
77
        if (!empty($this->urlParams)) {
78
            $params = http_build_query($this->urlParams);
79
            $url .= '?'.$params;
80
        }
81
        $url = urldecode($url);
82
        $this->setUrl($url);
83
    }
84
85
    public function addParam($value)
86
    {
87
        $this->params[] = $value;
88
    }
89
90
    public function addUrlParam($name, $value)
91
    {
92
        $this->urlParams[$name] = $value;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getBase()
99
    {
100
        return $this->base;
101
    }
102
}
103