Completed
Pull Request — master (#22)
by Sergey
12:32
created

RuntimeCacheRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\Repository;
4
5
use Isswp101\Persimmon\Collection\ICollection;
6
use Isswp101\Persimmon\Contracts\Storable;
7
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException;
8
use Isswp101\Persimmon\Exceptions\MethodNotImplementedException;
9
use Isswp101\Persimmon\Helpers\Arr;
10
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
11
12
class RuntimeCacheRepository implements IRepository
13
{
14
    private $data = [];
15
16
    private function getHash(string $id, string $class): string
17
    {
18
        return $class . '@' . $id;
19
    }
20
21
    private function getHashByModel(Storable $model): string
22
    {
23
        return $this->getHash($model->getPrimaryKey(), get_class($model));
24
    }
25
26
    public function instantiate(string $class): Storable
27
    {
28
        $instance = new $class;
29
        if (!$instance instanceof Storable) {
30
            throw new ClassTypeErrorException(Storable::class);
31
        }
32
        return $instance;
33
    }
34
35
    public function find(string $id, string $class, array $columns = []): ?Storable
36
    {
37
        $hash = $this->getHash($id, $class);
38
        $data = $this->data[$hash] ?? [];
39
        if (!$data) {
40
            return null;
41
        }
42
        $model = $this->instantiate($class);
43
        $model->setPrimaryKey($id);
44
        if ($columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
            $data = Arr::only($data, $columns);
46
        }
47
        $model->fill($data);
48
        return $model;
49
    }
50
51
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
52
    {
53
        throw new MethodNotImplementedException(__METHOD__);
54
    }
55
56
    public function insert(Storable $model): void
57
    {
58
        $hash = $this->getHashByModel($model);
59
        $this->data[$hash] = $model->toArray();
60
    }
61
62
    public function update(Storable $model): void
63
    {
64
        $hash = $this->getHashByModel($model);
65
        $this->data[$hash] = array_merge($this->data[$hash] ?? [], $model->toArray());
66
    }
67
68
    public function delete(Storable $model): void
69
    {
70
        $hash = $this->getHashByModel($model);
71
        unset($this->data[$hash]);
72
    }
73
}