ConnectionTrait::connection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Service\DbAdmin;
4
5
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
6
use Lagdo\DbAdmin\Driver\Db\StatementInterface;
7
use Lagdo\DbAdmin\Driver\DriverInterface;
8
use Lagdo\Facades\Logger;
9
10
use function gmdate;
11
12
/**
13
 * Connection to the logging database
14
 */
15
trait ConnectionTrait
16
{
17
    /**
18
     * @var int|null
19
     */
20
    private int|null $ownerId = null;
21
22
    /**
23
     * @var ConnectionInterface
24
     */
25
    private ConnectionInterface $connection;
26
27
    /**
28
     * @param DriverInterface $driver
29
     * @param array $database
30
     */
31
    protected function connect(DriverInterface $driver, array $database)
32
    {
33
        $this->connection = $driver->createConnection($database);
34
        $this->connection->open($database['name'], $database['schema'] ?? '');
35
    }
36
37
    /**
38
     * @var string
39
     */
40
    protected function currentTime(): string
41
    {
42
        return gmdate('Y-m-d H:i:s');
43
    }
44
45
    /**
46
     * @var ConnectionInterface
47
     */
48
    protected function connection(): ConnectionInterface
49
    {
50
        return $this->connection;
51
    }
52
53
    /**
54
     * @param string $query
55
     * @param array $values
56
     *
57
     * @return bool|StatementInterface
58
     */
59
    private function executeQuery(string $query, array $values): bool|StatementInterface
60
    {
61
        $st = $this->connection->prepareStatement($query);
62
        return $this->connection->executeStatement($st, $values) ?? false;
63
    }
64
65
    /**
66
     * @var string
67
     */
68
    abstract protected function user(): string;
69
70
    /**
71
     * @param string $username
72
     *
73
     * @return int
74
     */
75
    private function readOwnerId(string $username): int
76
    {
77
        $query = "select id from dbadmin_owners where username=:username limit 1";
78
        $statement = $this->executeQuery($query, ['username' => $username]);
79
        return !$statement || !($row = $statement->fetchAssoc()) ? 0 : (int)$row['id'];
80
    }
81
82
    /**
83
     * @param string $username
84
     *
85
     * @return int
86
     */
87
    private function newOwnerId(string $username): int
88
    {
89
        // Try to save the user and return his id.
90
        $query = "insert into dbadmin_owners(username) values(:username)";
91
        $statement = $this->executeQuery($query, ['username' => $username]);
92
        if ($statement !== false) {
93
            return $this->readOwnerId($username);
94
        }
95
96
        Logger::warning('Unable to save new owner in the query logging database.', [
97
            'error' => $this->connection?->error() ??
98
                'Not connected to the logging database',
99
        ]);
100
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return integer.
Loading history...
101
    }
102
103
    /**
104
     * @param bool $canCreate
105
     *
106
     * @return int
107
     */
108
    private function getOwnerId(bool $canCreate = true): int
109
    {
110
        return $this->ownerId !== null ? $this->ownerId :
111
            ($this->readOwnerId($this->user()) ?: ($canCreate ?
112
                $this->newOwnerId($this->user()) : 0));
113
    }
114
}
115