DbalClientStorage::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Bgy\OAuth2Server\DbalStorage;
4
5
use Bgy\OAuth2\Storage\ClientNotFound;
6
use Bgy\OAuth2\Storage\ClientStorage;
7
use Bgy\OAuth2\Client as ClientModel;
8
use Doctrine\DBAL\Connection;
9
10
/**
11
 * @author Boris Guéry <[email protected]>
12
 */
13
class DbalClientStorage implements ClientStorage
14
{
15
    private $dbalConnection;
16
    private $tableConfiguration;
17
18
    public function __construct(Connection $dbalConnection, TableConfiguration $tableConfiguration)
19
    {
20
        $this->dbalConnection     = $dbalConnection;
21
        $this->tableConfiguration = $tableConfiguration;
22
    }
23
24
    public function save(ClientModel $client)
25
    {
26
        $this->dbalConnection->insert(
27
            $this->tableConfiguration->getClientTableName(),
28
            [
29
                'client_id'     => $client->getId(),
30
                'client_secret' => $client->getSecret(),
31
                'redirect_uris' => serialize($client->getRedirectUris()),
32
                'grant_types'   => serialize($client->getAllowedGrantTypes()),
33
34
            ]
35
        );
36
    }
37
38
    public function delete(ClientModel $client)
39
    {
40
        $this->dbalConnection->delete(
41
            $this->tableConfiguration->getClientTableName(),
42
            ['client_id' => $client->getId()]
43
        );
44
    }
45
46
    public function findById($clientId)
47
    {
48
49
        $qb = $this->dbalConnection->createQueryBuilder();
50
        $stmt = $qb->select('*')
51
            ->from($this->tableConfiguration->getClientTableName())
52
            ->where(
53
                $qb->expr()->like('client_id', ':clientId')
54
            )
55
            ->setMaxResults(1)
56
            ->setParameter('clientId', $clientId)
57
            ->execute()
58
        ;
59
60
        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
61
62
63
        if (1 !== count($rows)) {
64
65
            throw new ClientNotFound($clientId);
66
        }
67
68
        return new ClientModel(
69
            $rows[0]['client_id'],
70
            $rows[0]['client_secret'],
71
            unserialize($rows[0]['redirect_uris']),
72
            unserialize($rows[0]['allowed_grant_types'])
73
        );
74
    }
75
}
76