DataGateway::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DataSource\TableDataGateway;
6
7
use ArrayAccess;
8
use DataSource\Connection;
9
use BasePatterns\RecordSet\Row;
10
use BasePatterns\RecordSet\Table;
11
use BasePatterns\RecordSet\RecordSet;
12
13
abstract class DataGateway implements ArrayAccess
14
{
15
    /**
16
     * @var DataSetHolder
17
     */
18
    public $holder;
19
20
    abstract public function getTableName(): string;
21 3
22
    public function __construct(Connection $connection)
23 3
    {
24 3
        $this->holder = new DataSetHolder($connection);
25 3
    }
26
27 3
    public function getData(): RecordSet
28
    {
29 3
        return $this->holder->data;
30
    }
31
32 3
    public function getTable(): Table
33
    {
34 3
        return $this->getData()->getTable($this->getTableName());
35
    }
36
37 1
    public function loadAll(): void
38
    {
39 1
        $commandString = sprintf("SELECT * FROM `%s`", $this->getTableName());
40 1
        $this->holder->fillData($commandString, $this->getTableName());
41 1
    }
42
43 2
    public function loadWhere(string $whereClause, array $params): void
44
    {
45 2
        $commandString = sprintf("SELECT * FROM `%s` WHERE %s", $this->getTableName(), $whereClause);
46 2
        $this->holder->fillData($commandString, $this->getTableName(), $params);
47 2
    }
48
49 1
    public function update(): void
50
    {
51 1
        $this->holder->update();
52 1
    }
53
54
    public function offsetExists($offset): bool
55
    {
56
        return array_key_exists($offset, $this->holder->data[$this->getTableName()]);
57
    }
58
59 3
    public function offsetGet($offset)
60
    {
61 3
        $rows = $this->getTable()->getRows();
62
        $hit = array_filter($rows, function (Row $row) use ($offset) {
63 3
            return $row->id == $offset;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on BasePatterns\RecordSet\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
64 3
        });
65
66 3
        return reset($hit) ?: null;
67
    }
68
69
    public function offsetSet($offset, $value): void
70
    {
71
        false;
72
    }
73
74
    public function offsetUnset($offset): void
75
    {
76
        false;
77
    }
78
}
79