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

CredentialStore   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 25
c 1
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getByEntityId() 0 34 4
1
<?php
2
3
namespace DMK\MKSamlAuth\Store;
4
5
use LightSaml\Credential\CredentialInterface;
6
use LightSaml\Credential\KeyHelper;
7
use LightSaml\Credential\X509Certificate;
8
use LightSaml\Credential\X509Credential;
9
use LightSaml\Store\Credential\CredentialStoreInterface;
10
use TYPO3\CMS\Core\Database\Connection;
11
use TYPO3\CMS\Core\Database\ConnectionPool;
12
use TYPO3\CMS\Core\SingletonInterface;
13
14
class CredentialStore implements CredentialStoreInterface, SingletonInterface
15
{
16
    /**
17
     * @var ConnectionPool
18
     */
19
    private $pool;
20
21
    public function __construct(ConnectionPool $pool)
22
    {
23
        $this->pool = $pool;
24
    }
25
26
    public function getByEntityId($entityId)
27
    {
28
        $qb = $this->pool->getQueryBuilderForTable('tx_mksamlauth_domain_model_identityprovider');
29
        $qb->select('*');
30
        $qb->from('tx_mksamlauth_domain_model_identityprovider');
31
        $qb->where($qb->expr()->eq('idp_entity_id', '?'));
32
        $qb->setMaxResults(1);
33
        $qb->setParameters([$entityId]);
34
35
        if (false === $stmt = $qb->execute()) {
0 ignored issues
show
introduced by
The condition false === $stmt = $qb->execute() is always false.
Loading history...
36
            return null;
37
        }
38
39
        if (false === $row = $stmt->fetch()) {
40
            return null;
41
        }
42
43
        $certificate = new X509Certificate();
44
        $certificate->loadPem($row['certificate']);
45
46
        $privateKey = null;
47
        if (0 < strlen($row['cert_key'])) {
48
            $privateKey = KeyHelper::createPrivateKey(
49
                $row['cert_key'],
50
                $row['passphrase'],
51
                false,
52
                $certificate->getSignatureAlgorithm()
53
            );
54
        }
55
56
        $credential = new X509Credential($certificate, $privateKey);
57
        $credential->setEntityId($entityId);
58
59
        return [$credential];
60
    }
61
62
}
63