Passed
Push — master ( 80df18...80df18 )
by Dāvis
04:16
created

Uri::__construct()   C

Complexity

Conditions 8
Paths 30

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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