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

SpEntityDescriptorStore::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\EntityDescriptor;
6
7
use LightSaml\Model\Metadata\EntityDescriptor;
8
use LightSaml\Model\Metadata\IdpSsoDescriptor;
9
use LightSaml\Model\Metadata\SingleSignOnService;
10
use LightSaml\Model\Metadata\SpSsoDescriptor;
11
use LightSaml\Model\XmlDSig\SignatureStringReader;
12
use LightSaml\Store\EntityDescriptor\EntityDescriptorStoreInterface;
13
use TYPO3\CMS\Core\Database\ConnectionPool;
14
15
class SpEntityDescriptorStore implements EntityDescriptorStoreInterface
16
{
17
    /**
18
     * @var ConnectionPool
19
     */
20
    private $pool;
21
22
    public function __construct(ConnectionPool $pool)
23
    {
24
        $this->pool = $pool;
25
    }
26
27
    public function get($entityId)
28
    {
29
        $conn = $this->pool->getConnectionForTable('tx_mksamlauth_domain_model_identityprovider');
30
31
        $qb = $conn->createQueryBuilder();
32
        $qb->select('i.*');
33
        $qb->from('tx_mksamlauth_domain_model_identityprovider', 'i');
34
        $qb->where($qb->expr()->eq('i.name', ':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 null !== $this->get($entityId);
45
    }
46
47
    public function all()
48
    {
49
        $conn = $this->pool->getConnectionForTable('tx_mksamlauth_domain_model_identityprovider');
50
51
        $qb = $conn->createQueryBuilder();
52
        $qb->select('i.*');
53
        $qb->from('tx_mksamlauth_domain_model_identityprovider', 'i');
54
        $qb->groupBy('i.name');
55
56
        $result = [];
57
58
        foreach ($qb->execute()->fetchAll() as $row) {
59
            $result[] = $this->map($row);
60
        }
61
62
        return $result;
63
    }
64
65
    private function map(array $row): EntityDescriptor
66
    {
67
        $descriptor = new SpSsoDescriptor();
68
        $descriptor->addAssertionConsumerService($row['name']);
69
70
        $entityDescriptor = new EntityDescriptor(
71
            $row['idp_entity_id'],
72
            [$descriptor]
73
        );
74
75
        $entityDescriptor->setSignature(new SignatureStringReader($row['idp_certificate']));
76
77
        return $entityDescriptor;
78
    }
79
}
80