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

RuntimeCacheRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
}