Passed
Push — master ( b38da1...a59646 )
by Dāvis
04:33
created

Uri::setIdToken()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 5
nc 4
nop 0
dl 0
loc 8
rs 8.8571
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
        $this->setGetParams($options, $additional);
31
    }
32
33
    private function setGetParams($options, $additional)
34
    {
35
        if ($this->method === OpenIDConnectProvider::METHOD_GET) {
36
            if (isset($options['url_params']['post_logout_redirect_uri'])) {
37
                $options['url_params']['post_logout_redirect_uri'] = $additional['redirect_uri'];
38
                unset($additional['redirect_uri']);
39
            }
40
            $this->urlParams = !empty($options['url_params']) ? array_merge($options['url_params'], $additional) : $additional;
41
        }
42
    }
43
44
    public function redirect()
45
    {
46
        return new RedirectResponse($this->getUrl());
47
    }
48
49
    /**
50
     * Get the value of Url
51
     *
52
     * @param null $language
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $language is correct as it would always require null to be passed?
Loading history...
53
     *
54
     * @return mixed
55
     */
56
    public function getUrl($language = null)
57
    {
58
        $this->buildUrl($language);
59
60
        return $this->url;
61
    }
62
63
    /**
64
     * Set the value of Url
65
     *
66
     * @param mixed $url
67
     *
68
     * @return $this
69
     */
70
    public function setUrl($url)
71
    {
72
        $this->url = $url;
73
74
        return $this;
75
    }
76
77
    private function buildUrl($language = null)
78
    {
79
        $this->setIdToken();
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
    private function setIdToken()
98
    {
99
        if ($this->method === OpenIDConnectProvider::METHOD_GET) {
100
            if (isset($this->urlParams['id_token_hint']) && $this->session !== null && $this->session->has('id_token')) {
101
                if ($this->useSession === false) {
102
                    throw new InvalidArgumentException(sprintf('"%s" parameter must be set in order to use id_token_hint', 'use_session'));
103
                }
104
                $this->urlParams['id_token_hint'] = $this->session->get('id_token');
105
            }
106
        }
107
    }
108
109
    public function addParam($value)
110
    {
111
        $this->params[] = $value;
112
    }
113
114
    public function addUrlParam($name, $value)
115
    {
116
        $this->urlParams[$name] = $value;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getBase()
123
    {
124
        return $this->base;
125
    }
126
}
127