OwnContainer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\Container;
6
7
use DMK\MKSamlAuth\Exception\RuntimeException;
8
use DMK\MKSamlAuth\Repository\IdentityProviderRepository;
9
use DMK\MKSamlAuth\Store\CredentialStore;
10
use LightSaml\Build\Container\OwnContainerInterface;
11
use LightSaml\Builder\EntityDescriptor\SimpleEntityDescriptorBuilder;
12
use LightSaml\Credential\X509Certificate;
13
use TYPO3\CMS\Core\SingletonInterface;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
class OwnContainer implements OwnContainerInterface, SingletonInterface
17
{
18
    /**
19
     * @var SingletonContainer
20
     */
21
    private $container;
22
    /**
23
     * @var IdentityProviderRepository
24
     */
25
    private $repository;
26
27
    public function __construct(IdentityProviderRepository $repository, SingletonContainer $container)
28
    {
29
        $this->container = $container;
30
        $this->repository = $repository;
31
    }
32
33
    public function getOwnEntityDescriptorProvider()
34
    {
35
        $host = GeneralUtility::getIndpEnv('HTTP_HOST');
36
        $record = $this->repository->findByHostname($host);
37
38
        if (false === \is_array($record)) {
39
            throw new RuntimeException(sprintf('No identity provider could be found for host "%s".', $host));
40
        }
41
42
        $certificate = new X509Certificate();
43
        $certificate->loadPem($record['certificate']);
44
45
        return new SimpleEntityDescriptorBuilder(
46
            $record['name'],
47
            $record['name'],
48
            $record['url'],
49
            $certificate
50
        );
51
    }
52
53
    public function getOwnCredentials()
54
    {
55
        $host = GeneralUtility::getIndpEnv('HTTP_HOST');
56
        $record = $this->repository->findByHostname($host);
57
58
        if (false === \is_array($record)) {
59
            throw new RuntimeException(sprintf('No identity provider could be found for host "%s".', $host));
60
        }
61
62
        /** @var CredentialStore $store */
63
        $store = $this->container->getInstance(CredentialStore::class);
64
65
        return $store->getByEntityId($record['name']);
66
    }
67
}
68