Completed
Pull Request — master (#22)
by Sergey
13:25
created

RuntimeCacheRepository::find()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
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 $data = [];
15
    private $allColumns = [];
16
17
    public function __construct()
18
    {
19
        // $this->collection = new Collection();
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
20
        // $this->allColumns = new Collection();
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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
        $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
        $hash = $this->getHash($class, $id);
40
        $data = $this->data[$hash] ?? [];
41
        if ($data) {
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 = array_intersect_key($data, array_flip($columns));
46
            }
47
            $model->fill($data);
48
            return $model;
49
        }
50
        return null;
51
52
        // if (!$columns && !$this->hasAllColumns($id, $class)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
        //     return null;
54
        // }
55
        // $cachedModel = $this->collection->get($this->getHash($class, $id));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
        // if ($cachedModel != null && $columns) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
        //     $cachedModel = Cast::storable($cachedModel);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
        //     $model = $this->instantiate($class);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        //     $model->setPrimaryKey($cachedModel->getPrimaryKey());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
        //     $model->fill(array_intersect_key($cachedModel->toArray(), array_flip($columns)));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
        //     return $model;
62
        // }
63
        // return $cachedModel;
64
    }
65
66
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
67
    {
68
        return new Collection($this->data); // @TODO return data that's related to $class
69
        // @TODO return collection of models, not array
70
    }
71
72
    public function insert(Storable $model): void
73
    {
74
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
75
        $this->data[$hash] = $model->toArray();
76
    }
77
78
    public function update(Storable $model): void
79
    {
80
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
81
        $this->data[$hash] = array_merge($this->data[$hash] ?? [], $model->toArray());
82
    }
83
84
    public function delete(Storable $model): void
85
    {
86
        $hash = $this->getHash(get_class($model), $model->getPrimaryKey());
87
        unset($this->data[$hash]);
88
    }
89
90
    public function hasAllColumns(string $id, string $class): bool
91
    {
92
        return $this->allColumns->has($this->getHash($class, $id));
0 ignored issues
show
Bug introduced by
The method has cannot be called on $this->allColumns (of type array).

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...
93
    }
94
95
    public function setAllColumns(string $id, string $class): void
96
    {
97
        $this->allColumns->put($this->getHash($class, $id), true);
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->allColumns (of type array).

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...
98
    }
99
}