CredentialStore::getByEntityId()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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