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

RuntimeCacheRepository::find()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 3
nop 3
1
<?php
2
3
namespace Isswp101\Persimmon\Repository;
4
5
use Isswp101\Persimmon\Collection\Collection;
6
use Isswp101\Persimmon\Collection\ICollection;
7
use Isswp101\Persimmon\Contracts\Storable;
8
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException;
9
use Isswp101\Persimmon\Helpers\Cast;
10
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
11
12
class RuntimeCacheRepository implements ICacheRepository
13
{
14
    private $collection;
15
    private $allColumns;
16
17
    public function __construct()
18
    {
19
        $this->collection = new Collection();
20
        $this->allColumns = new Collection();
21
    }
22
23
    private function getHash(string $class, string $id): string
24
    {
25
        return $class . '@' . $id;
26
    }
27
28
    public function instantiate(string $class): Storable
29
    {
30
        $instance = new $class;
31
        if (!$instance instanceof Storable) {
32
            throw new ClassTypeErrorException(Storable::class);
33
        }
34
        return $instance;
35
    }
36
37
    public function find(string $id, string $class, array $columns = []): ?Storable
38
    {
39
        if (!$columns && !$this->hasAllColumns($id, $class)) {
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...
40
            return null;
41
        }
42
        $cachedModel = $this->collection->get($this->getHash($class, $id));
43
        if ($cachedModel != null && $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...
44
            $cachedModel = Cast::storable($cachedModel);
45
            $model = $this->instantiate($class);
46
            $model->setPrimaryKey($cachedModel->getPrimaryKey());
47
            $model->fill(array_intersect_key($cachedModel->toArray(), array_flip($columns)));
48
            return $model;
49
        }
50
        return $cachedModel;
51
    }
52
53
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
54
    {
55
        return $this->collection;
56
    }
57
58
    public function insert(Storable $model): void
59
    {
60
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
61
        $this->collection->put($hash, $model);
62
    }
63
64
    public function update(Storable $model): void
65
    {
66
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
67
        if (!$this->collection->has($hash)) {
68
            $this->collection->put($hash, $model);
69
        } else {
70
            $cachedModel = $this->collection->get($hash);
71
            foreach ($model->toArray() as $key => $value) {
72
                $cachedModel->{$key} = $value;
73
            }
74
        }
75
    }
76
77
    public function delete(Storable $model): void
78
    {
79
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
80
        $this->collection->forget($hash);
81
    }
82
83
    public function hasAllColumns(string $id, string $class): bool
84
    {
85
        return $this->allColumns->has($this->getHash($class, $id));
86
    }
87
88
    public function setAllColumns(string $id, string $class): void
89
    {
90
        $this->allColumns->put($this->getHash($class, $id), true);
91
    }
92
}