Completed
Push — master ( 743121...807ca0 )
by Midori
01:33
created

AbstractRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 47.62%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 41
c 1
b 0
f 1
dl 0
loc 88
ccs 20
cts 42
cp 0.4762
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A readAll() 0 21 4
A remove() 0 8 2
A read() 0 8 2
A save() 0 24 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\nanodb;
6
7
use Exception;
8
use midorikocak\querymaker\QueryInterface;
9
use ReflectionException;
10
11
use function array_filter;
12
use function array_key_exists;
13
use function array_map;
14
use function is_array;
15
use function is_null;
16
17
abstract class AbstractRepository implements RepositoryInterface
18
{
19
    protected Database $db;
20
    protected string $tableName = '';
21
    protected string $className = '';
22
23 2
    public function __construct(Database $db)
24
    {
25 2
        $this->db = $db;
26 2
    }
27
28
    /**
29
     * @throws ReflectionException
30
     */
31 1
    public function read(string $id): Item
32
    {
33 1
        if ($id) {
34 1
            $this->db->select($this->tableName)->where('id', $id)->execute();
35
        } else {
36
            $this->db->select($this->tableName)->execute();
37
        }
38 1
        return $this->className::fromArray($this->db->fetch());
39
    }
40
41
    /**
42
     * @return Item[]
43
     */
44
    public function readAll(?QueryInterface $query = null): array
45
    {
46
        if ($query !== null) {
47
            $db = $this->db->query($query);
48
        } else {
49
            $db = $this->db->select($this->tableName);
50
        }
51
52
        $db->execute();
53
        $items = $db->fetchAll();
54
        return array_map(function ($item) {
55
            $article = $this->className::fromArray($item);
56
            if (array_key_exists('id', $item)) {
57
                $article->setId($item['id']);
58
            }
59
            if (array_key_exists('user_id', $item)) {
60
                $article->setUserId($item['user_id']);
61
            }
62
63
            return $article;
64
        }, $items);
65
    }
66
67
    /**
68
     * @param Item $item
69
     * @throws ReflectionException
70
     */
71 2
    public function save($item): Item
72
    {
73 2
        $itemData = array_filter($item->toArray(), fn($item) => !is_array($item) && !is_null($item));
74
75 2
        if ($item->getId() !== null) {
76 1
            $id = $itemData['id'];
77
78 1
            unset($itemData['id']);
79
80 1
            $this->db->update($this->tableName, $itemData)->where('id', $id)->execute();
81
82 1
            return $this->read($id);
83
        }
84
85 2
        if ($this->db->insert($this->tableName, $itemData)->execute() === false) {
86
            throw new Exception('Not Found.');
87
        }
88
89 2
        $lastInsertId = $this->db->lastInsertId();
90 2
        $updatedItem = $this->db->select($this->tableName)->where('id', $lastInsertId)->fetch();
91
92 2
        $item = $this->className::fromArray($updatedItem);
93 2
        $item->setFromArray($updatedItem);
94 2
        return $item;
95
    }
96
97
    public function remove($item): int
98
    {
99
        $id = $item->getId();
100
        if ($id !== null) {
101
            $this->db->delete($this->tableName)->where('id', $id)->execute();
102
            return $this->db->rowCount();
103
        }
104
        return 0;
105
    }
106
}
107