Completed
Push — 8.7 ( b6d8c1...3ac046 )
by Markus
06:49
created

IdpEntityDescriptorStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 31
c 1
b 0
f 0
dl 0
loc 62
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A __construct() 0 3 1
A all() 0 17 2
A get() 0 12 2
A map() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\EntityDescriptor;
6
7
use LightSaml\Builder\EntityDescriptor\SimpleEntityDescriptorBuilder;
8
use LightSaml\Credential\X509Certificate;
9
use LightSaml\Model\Metadata\EntityDescriptor;
10
use LightSaml\Model\XmlDSig\SignatureStringReader;
11
use LightSaml\Store\EntityDescriptor\EntityDescriptorStoreInterface;
12
use TYPO3\CMS\Core\Database\ConnectionPool;
13
use TYPO3\CMS\Core\SingletonInterface;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
class IdpEntityDescriptorStore implements EntityDescriptorStoreInterface, SingletonInterface
17
{
18
    /**
19
     * @var ConnectionPool
20
     */
21
    private $pool;
22
23
    public function __construct(ConnectionPool $connectionPool)
24
    {
25
        $this->pool = $connectionPool;
26
    }
27
28
    public function get($entityId)
29
    {
30
        $conn = $this->pool->getConnectionForTable('tx_mksamlauth_domain_model_identityprovider');
31
        $qb = $conn->createQueryBuilder();
32
        $qb->select('i.*');
33
        $qb->from('tx_mksamlauth_domain_model_identityprovider', 'i');
34
        $qb->where($qb->expr()->eq('i.idp_entity_id', ':id'));
35
        $qb->setParameter(':id', $entityId);
36
37
        $row = $qb->execute()->fetch();
38
39
        return $row ? $this->map($row) : null;
40
    }
41
42
    public function has($entityId)
43
    {
44
        return $this->get($entityId) !== null;
45
    }
46
47
    public function all()
48
    {
49
        $conn = $this->pool->getConnectionForTable('tx_mksamlauth_domain_model_identityprovider');
50
        $qb = $conn->createQueryBuilder();
51
        $qb->select('i.*');
52
        $qb->from('tx_mksamlauth_domain_model_identityprovider', 'i');
53
        $qb->groupBy('i.idp_entity_id');
54
55
        $stmt = $qb->execute();
56
57
        $result = [];
58
59
        foreach ($stmt->fetchAll() as $row) {
60
            $result[] = $this->map($row);
61
        }
62
63
        return $result;
64
    }
65
66
    private function map(array $row): EntityDescriptor
67
    {
68
        $certificate = new X509Certificate();
69
        $certificate->loadPem($row['idp_certificate']);
70
71
        return GeneralUtility::makeInstance(
72
            SimpleEntityDescriptorBuilder::class,
73
            $row['idp_entity_id'],
74
            null,
75
            $row['url'],
76
            $certificate
77
        )->get();
78
    }
79
}
80