Dba   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 45
c 1
b 0
f 0
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B exec() 0 23 7
A query() 0 20 4
A connect() 0 4 2
A disconnect() 0 5 2
A connectToServer() 0 12 2
A languageDialect() 0 3 1
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\Raw;
4
5
6
use kalanis\kw_mapper\Interfaces\IDriverSources;
7
use kalanis\kw_mapper\Interfaces\IPassConnection;
8
use kalanis\kw_mapper\MapperException;
9
use kalanis\kw_mapper\Storage\Database\ADatabase;
10
use kalanis\kw_mapper\Storage\Database\Dialects;
11
use kalanis\kw_mapper\Storage\Database\TConnection;
12
13
14
/**
15
 * Class Dba
16
 * @package kalanis\kw_mapper\Storage\Database\Raw
17
 * @link https://www.php.net/manual/en/book.dba.php
18
 * @codeCoverageIgnore remote connection
19
 */
20
class Dba extends ADatabase implements IPassConnection
21
{
22
    use TConnection;
23
24
    protected string $extension = 'dba';
25
    /** @var resource|null */
26
    protected $connection = null;
27
28
    public function languageDialect(): string
29
    {
30
        return Dialects\EmptyDialect::class;
31
    }
32
33
    public function disconnect(): void
34
    {
35
        if ($this->isConnected()) {
36
            dba_close($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type object; however, parameter $dba of dba_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

36
            dba_close(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
37
            $this->connection = null;
38
        }
39
    }
40
41
    /**
42
     * @param string $key
43
     * @throws MapperException
44
     * @return array<string|int, string|int|float|null|array<string|int|float|null>>
45
     */
46
    public function query(string $key): array
47
    {
48
        if (empty($key)) {
49
            return [];
50
        }
51
52
        $this->connect();
53
54
        $results = [];
55
        $key = dba_firstkey($this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_firstkey() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $key = dba_firstkey(/** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
56
57
        while (false !== $key) {
58
            $line = dba_fetch($key, $this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $handle of dba_fetch() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

58
            $line = dba_fetch($key, /** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
59
            if (false !== $line) {
60
                $results[] = $line;
61
            }
62
            $key = dba_nextkey($this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_nextkey() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

62
            $key = dba_nextkey(/** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
63
        }
64
65
        return $results;
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param string $action
71
     * @param string $content
72
     * @throws MapperException
73
     * @return bool
74
     */
75
    public function exec(string $action, string $key, string $content = ''): bool
76
    {
77
        if (empty($key)) {
78
            return false;
79
        }
80
81
        $this->connect();
82
83
        if (dba_exists($key, $this->getConnection())) {
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_exists() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

83
        if (dba_exists($key, /** @scrutinizer ignore-type */ $this->getConnection())) {
Loading history...
84
            if (IDriverSources::ACTION_UPDATE == $action) {
85
                return dba_replace($key, $content, $this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_replace() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

85
                return dba_replace($key, $content, /** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
86
            } elseif (IDriverSources::ACTION_DELETE == $action) {
87
                return dba_delete($key, $this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_delete() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

87
                return dba_delete($key, /** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
88
            } else {
89
                return false;
90
            }
91
        } else {
92
            if (IDriverSources::ACTION_UPDATE == $action) {
93
                return dba_insert($key, $content, $this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type object; however, parameter $dba of dba_insert() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

93
                return dba_insert($key, $content, /** @scrutinizer ignore-type */ $this->getConnection());
Loading history...
94
            } elseif (IDriverSources::ACTION_INSERT == $action) {
95
                return dba_insert($key, $content, $this->getConnection());
96
            } else {
97
                return false;
98
            }
99
        }
100
    }
101
102
    /**
103
     * @throws MapperException
104
     */
105
    public function connect(): void
106
    {
107
        if (!$this->isConnected()) {
108
            $this->connection = $this->connectToServer();
109
        }
110
    }
111
112
    /**
113
     * @throws MapperException
114
     * @return resource
115
     */
116
    protected function connectToServer()
117
    {
118
        $connection = dba_open(
119
            $this->config->getLocation(),
120
            $this->config->getDatabase(),
121
            $this->config->getType()
122
        );
123
        if (!$connection) {
0 ignored issues
show
introduced by
$connection is of type resource, thus it always evaluated to false.
Loading history...
124
            throw new MapperException('DBA connection failed.');
125
        }
126
127
        return $connection;
128
    }
129
}
130