Passed
Push — main ( 28b00c...2eb7a0 )
by Thierry
20:47 queued 09:28
created

ConnectionTrait::getOwnerId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 5
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);
0 ignored issues
show
Bug introduced by
The method prepareStatement() does not exist on Lagdo\DbAdmin\Driver\Db\ConnectionInterface. ( Ignorable by Annotation )

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

61
        /** @scrutinizer ignore-call */ 
62
        $st = $this->connection->prepareStatement($query);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        return $this->connection->executeStatement($st, $values) ?? false;
0 ignored issues
show
Bug introduced by
The method executeStatement() does not exist on Lagdo\DbAdmin\Driver\Db\ConnectionInterface. ( Ignorable by Annotation )

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

62
        return $this->connection->/** @scrutinizer ignore-call */ executeStatement($st, $values) ?? false;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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