SpEntityDescriptorBuilder::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.439
cc 5
eloc 19
nc 4
nop 6
crap 5
1
<?php
2
3
namespace AerialShip\SamlSPBundle\Config;
4
5
use AerialShip\LightSaml\Bindings;
6
use AerialShip\LightSaml\Model\Metadata\EntityDescriptor;
7
use AerialShip\LightSaml\Model\Metadata\KeyDescriptor;
8
use AerialShip\LightSaml\Model\Metadata\Service\AssertionConsumerService;
9
use AerialShip\LightSaml\Model\Metadata\Service\SingleLogoutService;
10
use AerialShip\LightSaml\Model\Metadata\SpSsoDescriptor;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Security\Http\HttpUtils;
13
14
class SpEntityDescriptorBuilder implements EntityDescriptorProviderInterface
15
{
16
    /** @var  string */
17
    protected $authenticationServiceID;
18
19
    /** @var SPSigningProviderInterface  */
20
    protected $signingProvider;
21
22
    /** @var  array */
23
    protected $config;
24
25
    /** @var  string */
26
    protected $checkPath;
27
28
    /** @var  string */
29
    protected $logoutPath;
30
31
32
    /** @var  HttpUtils */
33
    protected $httpUtils;
34
35
36
    /** @var  Request */
37
    protected $request;
38
39
40
    /** @var  EntityDescriptor */
41
    protected $entityDescriptor;
42
43
44
45 8
    public function __construct(
46
        $authenticationServiceID,
47
        SPSigningProviderInterface $signingProvider,
48
        array $config,
49
        $checkPath,
50
        $logoutPath,
51
        HttpUtils $httpUtils = null
52
    ) {
53 8
        if (!isset($config['base_url']) && !$httpUtils) {
54 1
            throw new \RuntimeException('If config base_url is not set, then httpUtils are required');
55
        }
56 7
        if (!isset($config['entity_id'])) {
57 1
            throw new \RuntimeException('Missing required config entity_id');
58
        }
59
60 6
        if (!isset($config['want_assertions_signed'])) {
61 6
            $config['want_assertions_signed'] = false;
62 6
        }
63
64 6
        $this->authenticationServiceID = $authenticationServiceID;
65 6
        $this->signingProvider = $signingProvider;
66 6
        $this->config = $config;
67 6
        $this->checkPath = $checkPath;
68 6
        $this->logoutPath = $logoutPath;
69 6
        $this->httpUtils = $httpUtils;
70 6
    }
71
72
73
74
    /**
75
     * @return string
76
     */
77 1
    public function getAuthenticationServiceID()
78
    {
79 1
        return $this->authenticationServiceID;
80
    }
81
82
83
    /**
84
     * @param Request $request
85
     */
86 1
    public function setRequest(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
87
    {
88 1
        $this->request = $request;
89 1
    }
90
91
92
    /**
93
     * @return EntityDescriptor
94
     */
95 4
    public function getEntityDescriptor()
96
    {
97 4
        if (!$this->entityDescriptor) {
98 4
            $this->build();
99 3
        }
100 3
        return $this->entityDescriptor;
101
    }
102
103
104
105 4
    protected function build()
106
    {
107 4
        $this->entityDescriptor = new EntityDescriptor($this->config['entity_id']);
108 4
        $sp = new SpSsoDescriptor();
109 4
        $this->entityDescriptor->addItem($sp);
110 4
        $sp->setWantAssertionsSigned($this->config['want_assertions_signed']);
111
112 4
        if ($this->signingProvider->isEnabled()) {
113 1
            $sp->addKeyDescriptor(new KeyDescriptor('signing', $this->signingProvider->getCertificate()));
114 1
        }
115
116 4
        $slo = new SingleLogoutService();
117 4
        $sp->addService($slo);
118 4
        $slo->setBinding(Bindings::SAML2_HTTP_REDIRECT);
119 4
        $slo->setLocation($this->buildPath($this->logoutPath));
120
121 3
        $slo = new SingleLogoutService();
122 3
        $sp->addService($slo);
123 3
        $slo->setBinding(Bindings::SAML2_HTTP_POST);
124 3
        $slo->setLocation($this->buildPath($this->logoutPath));
125
126 3
        $sp->addService(
127 3
            new AssertionConsumerService(
128 3
                Bindings::SAML2_HTTP_POST,
129 3
                $this->buildPath($this->checkPath),
130
                0
131 3
            )
132 3
        );
133 3
        $sp->addService(
134 3
            new AssertionConsumerService(
135 3
                Bindings::SAML2_HTTP_REDIRECT,
136 3
                $this->buildPath($this->checkPath),
137
                1
138 3
            )
139 3
        );
140 3
    }
141
142
143
    /**
144
     * @param string $path
145
     * @return string
146
     * @throws \RuntimeException
147
     */
148 4
    protected function buildPath($path)
149
    {
150 4
        if (isset($this->config['base_url']) && $this->config['base_url']) {
151 2
            return $this->config['base_url'] . $path;
152
        } else {
153 2
            if (!$this->request) {
154 1
                throw new \RuntimeException('Request not set');
155
            }
156
157 1
            return $this->httpUtils->generateUri($this->request, $path);
158
        }
159
    }
160
}
161