Passed
Branch master (eb9804)
by Dāvis
04:29
created

Uri::buildUrl()   C

Complexity

Conditions 9
Paths 21

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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