Passed
Push — master ( bcf14b...b38da1 )
by Dāvis
04:25
created

Uri::buildUrl()   D

Complexity

Conditions 9
Paths 25

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 25
nop 1
dl 0
loc 25
rs 4.909
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($language = null)
50
    {
51
        $this->buildUrl($language);
52
53
        return $this->url;
54
    }
55
56
    /**
57
     * Set the value of Url
58
     *
59
     * @param mixed $url
60
     *
61
     * @return $this
62
     */
63
    public function setUrl($url)
64
    {
65
        $this->url = $url;
66
67
        return $this;
68
    }
69
70
    private function buildUrl($language = null)
71
    {
72
        if ($this->method === OpenIDConnectProvider::METHOD_GET) {
73
            if (isset($this->urlParams['id_token_hint']) && $this->session !== null && $this->session->has('id_token')) {
74
                if ($this->useSession === false) {
75
                    throw new InvalidArgumentException(sprintf('"%s" parameter must be set in order to use id_token_hint', 'use_session'));
76
                }
77
                $this->urlParams['id_token_hint'] = $this->session->get('id_token');
78
            }
79
        }
80
81
        if($language !== null){
82
            $this->urlParams['lang'] = (string)$language;
83
        }
84
85
        $url = $this->base;
86
        if (!empty($this->params)) {
87
            $url .= implode('/', $this->params);
88
        }
89
        if (!empty($this->urlParams)) {
90
            $params = http_build_query($this->urlParams);
91
            $url .= '?'.$params;
92
        }
93
        $url = urldecode($url);
94
        $this->setUrl($url);
95
    }
96
97
    public function addParam($value)
98
    {
99
        $this->params[] = $value;
100
    }
101
102
    public function addUrlParam($name, $value)
103
    {
104
        $this->urlParams[$name] = $value;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getBase()
111
    {
112
        return $this->base;
113
    }
114
}
115