DoctrineStorage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Key\Storage;
6
7
use Damax\Bundle\ApiAuthBundle\Key\Key;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\FetchMode;
10
11
final class DoctrineStorage implements Storage
12
{
13
    private const FIELD_KEY = 'key';
14
    private const FIELD_TTL = 'ttl';
15
    private const FIELD_IDENTITY = 'identity';
16
17
    private $db;
18
    private $tableName;
19
    private $fields = [
20
        self::FIELD_KEY => self::FIELD_KEY,
21
        self::FIELD_TTL => self::FIELD_TTL,
22
        self::FIELD_IDENTITY => self::FIELD_IDENTITY,
23
    ];
24
25
    public function __construct(Connection $db, string $tableName, array $fields = [])
26
    {
27
        $this->db = $db;
28
        $this->tableName = $tableName;
29
        $this->fields = array_replace($this->fields, $fields);
30
    }
31
32
    public function has(string $key): bool
33
    {
34
        $sql = sprintf('SELECT 1 FROM %s WHERE %s = ?', $this->tableName, $this->fields[self::FIELD_KEY]);
35
36
        return (bool) $this->db->executeQuery($sql, [$key])->fetchColumn();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\ForwardCom...y\Result::fetchColumn() has been deprecated: Use fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

36
        return (bool) /** @scrutinizer ignore-deprecated */ $this->db->executeQuery($sql, [$key])->fetchColumn();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
37
    }
38
39
    public function remove(string $key): void
40
    {
41
        $sql = sprintf('DELETE FROM %s WHERE %s = ?', $this->tableName, $this->fields[self::FIELD_KEY]);
42
43
        $this->db->executeQuery($sql, [$key])->execute();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\ForwardCom...ility\Result::execute() has been deprecated: This feature will no longer be available on Result object in 3.0.x version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

43
        /** @scrutinizer ignore-deprecated */ $this->db->executeQuery($sql, [$key])->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
44
    }
45
46
    public function add(Key $key): void
47
    {
48
        $this->db->insert($this->tableName, [
49
            $this->fields[self::FIELD_KEY] => (string) $key,
50
            $this->fields[self::FIELD_IDENTITY] => $key->identity(),
51
            $this->fields[self::FIELD_TTL] => time() + $key->ttl(),
52
        ]);
53
    }
54
55
    public function get(string $key): Key
56
    {
57
        $fields = $this->fields[self::FIELD_IDENTITY] . ', ' . $this->fields[self::FIELD_TTL];
58
59
        $sql = sprintf('SELECT %s FROM %s WHERE %s = ?', $fields, $this->tableName, $this->fields[self::FIELD_KEY]);
60
61
        if (false === $row = $this->db->executeQuery($sql, [$key])->fetch(FetchMode::ASSOCIATIVE)) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\ForwardCompatibility\Result::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
        if (false === $row = /** @scrutinizer ignore-deprecated */ $this->db->executeQuery($sql, [$key])->fetch(FetchMode::ASSOCIATIVE)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
            throw new KeyNotFound();
63
        }
64
65
        return new Key($key, $row[$this->fields[self::FIELD_IDENTITY]], $row[$this->fields[self::FIELD_TTL]] - time());
66
    }
67
}
68