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

RuntimeCacheRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 4 1
A update() 0 4 1
A delete() 0 4 1
A getCachedAttributes() 0 4 1
A __construct() 0 4 1
A getHash() 0 4 1
A instantiate() 0 8 2
A find() 0 8 3
A all() 0 4 1
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\Model\IEloquent;
10
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
11
12
class RuntimeCacheRepository implements ICacheRepository
13
{
14
    private $collection;
15
16
    public function __construct()
17
    {
18
        $this->collection = new Collection();
19
    }
20
21
    private function getHash(string $class, string $id): string
22
    {
23
        return $class . '@' . $id;
24
    }
25
26
    public function instantiate(string $class): Storable
27
    {
28
        $instance = new $class;
29
        if (!$instance instanceof IEloquent) {
30
            throw new ClassTypeErrorException(IEloquent::class);
31
        }
32
        return $instance;
33
    }
34
35
    public function find(string $id, string $class, array $columns = []): ?Storable
36
    {
37
        $model = $this->collection->get($this->getHash($class, $id));
38
        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...
39
            return $model->fill(array_intersect_key($model->toArray(), array_flip($columns)));
40
        }
41
        return $model;
42
    }
43
44
    public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection
45
    {
46
        return $this->collection;
47
    }
48
49
    public function insert(Storable $model)
50
    {
51
        // TODO: Implement insert() method.
52
    }
53
54
    public function update(Storable $model)
55
    {
56
        // TODO: Implement update() method.
57
    }
58
59
    public function delete(Storable $model)
60
    {
61
        // TODO: Implement delete() method.
62
    }
63
64
    public function getCachedAttributes(string $id, string $class): array
65
    {
66
        // TODO: Implement getCachedAttributes() method.
67
    }
68
}