Completed
Pull Request — master (#22)
by Sergey
14:35
created

RuntimeCacheRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 67
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHash() 0 4 1
A instantiate() 0 4 1
A find() 0 8 3
A all() 0 4 1
A insert() 0 5 1
A update() 0 12 3
A delete() 0 5 1
A getCachedAttributes() 0 5 2
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\MethodNotImplementedException;
9
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
10
11
class RuntimeCacheRepository implements ICacheRepository
12
{
13
    /**
14
     * @var Storable[]
15
     */
16
    private $collection;
17
18
    public function __construct()
19
    {
20
        $this->collection = new Collection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Isswp101\Persimmon\Collection\Collection() of type object<Isswp101\Persimmon\Collection\Collection> is incompatible with the declared type array<integer,object<Iss...on\Contracts\Storable>> of property $collection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
        throw new MethodNotImplementedException();
31
    }
32
33
    public function find(string $id, string $class, array $columns = []): ?Storable
34
    {
35
        $model = $this->collection->get($this->getHash($class, $id));
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
36
        if ($model != 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...
37
            return $model->fill(array_intersect_key($model->toArray(), array_flip($columns)));
38
        }
39
        return $model;
40
    }
41
42
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
43
    {
44
        return $this->collection;
45
    }
46
47
    public function insert(Storable $model): void
48
    {
49
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
50
        $this->collection->put($hash, $model);
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51
    }
52
53
    public function update(Storable $model): void
54
    {
55
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
56
        if (!$this->collection->has($hash)) {
0 ignored issues
show
Bug introduced by
The method has cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
            $this->collection->put($hash, $model);
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
        } else {
59
            $cachedModel = $this->collection->get($hash);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
60
            foreach ($model->toArray() as $key => $value) {
61
                $cachedModel->{$key} = $value;
62
            }
63
        }
64
    }
65
66
    public function delete(Storable $model): void
67
    {
68
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
69
        $this->collection->forget($hash);
0 ignored issues
show
Bug introduced by
The method forget cannot be called on $this->collection (of type array<integer,object<Iss...on\Contracts\Storable>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
    }
71
72
    public function getCachedAttributes(string $id, string $class): array
73
    {
74
        $model = $this->find($id, $class);
75
        return $model != null ? array_keys($model->toArray()) : [];
76
    }
77
}