Completed
Push — master ( 50bcdc...d1e07a )
by Midori
14:44
created

ArrayRepository::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 2
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\nanodb;
6
7
use Exception;
8
use midorikocak\arraytools\ArrayValidator;
9
10
use function array_key_exists;
11
use function key;
12
use function reset;
13
14
/**
15
 * Repository is a class that receives arrays as input data and returns array of arrays
16
 */
17
class ArrayRepository implements RepositoryInterface
18
{
19
    private DatabaseInterface $db;
20
    private ?ArrayValidator $validator;
21
    private $tableName;
22
23
    public function __construct(
24
        string $tableName,
25
        DatabaseInterface $db,
26
        ?array $schema = null,
27
        ?ArrayValidator $validator = null
28
    ) {
29
        $this->db = $db;
30
        $this->tableName = $tableName;
31
        $this->validator = $validator ?? new ArrayValidator();
32
        if ($schema) {
33
            $this->validator->schema($schema);
34
        }
35
    }
36
37
    private function checkData($data): void
38
    {
39
        if (!$this->validator->validate($data)) {
0 ignored issues
show
Bug introduced by
The method validate() does not exist on null. ( Ignorable by Annotation )

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

39
        if (!$this->validator->/** @scrutinizer ignore-call */ validate($data)) {

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...
40
            throw new Exception('Invalid data');
41
        }
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function save($data): array
48
    {
49
        if (array_key_exists('id', $data)) {
50
            $this->checkData($data);
51
52
            $id = $data['id'];
53
54
            unset($data['id']);
55
56
            $this->db->update($this->tableName, $data)->where('id', $id)->execute();
57
58
            return $this->read($id);
59
        }
60
61
        $this->checkData($data);
62
        $this->db->insert($this->tableName, $data)->execute();
63
64
        $lastInsertId = $this->db->lastInsertId();
65
        return $this->db->select('users')->where('id', $lastInsertId)->fetch();
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function read(?string $id = null): array
72
    {
73
        if ($id) {
74
            $this->db->select($this->tableName)->where('id', $id)->execute();
75
        } else {
76
            $this->db->select($this->tableName)->execute();
77
        }
78
        return $this->db->fetch();
79
    }
80
81
    public function readAll(array $constraints = [], array $columns = ['*']): array
82
    {
83
        $db = $this->db->select($this->tableName, $columns);
84
85
        if (!empty($constraints)) {
86
            $value = reset($constraints);
87
            $key = key($constraints);
88
            $db->where($key, $value);
89
90
            unset($constraints[key($constraints)]);
91
92
            foreach ($constraints as $key => $value) {
93
                $db->and($key, $value);
94
            }
95
        }
96
97
        $db->execute();
98
        return $db->fetchAll();
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function remove($data): int
105
    {
106
        if (array_key_exists('id', $data)) {
107
            $this->db->delete($this->tableName)->where('id', $data['id'])->execute();
108
            return $this->db->rowCount();
109
        }
110
        return 0;
111
    }
112
}
113