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

Eloquent::all()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Isswp101\Persimmon\Model;
4
5
use Isswp101\Persimmon\Cache\CacheDecorator;
6
use Isswp101\Persimmon\Collection\ICollection;
7
use Isswp101\Persimmon\DI\Container;
8
use Isswp101\Persimmon\Exceptions\IllegalCollectionException;
9
use Isswp101\Persimmon\Exceptions\ModelNotFoundException;
10
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
11
use Isswp101\Persimmon\Traits\Containerable;
12
use Isswp101\Persimmon\Traits\Eventable;
13
use Isswp101\Persimmon\Traits\Timestampable;
14
15
abstract class Eloquent implements IEloquent
16
{
17
    use Containerable, Timestampable, Eventable;
18
19
    protected $primaryKey;
20
    protected $exists = false;
21
    protected $timestamps = false;
22
    protected $cache = false;
23
24
    /** @MustBeOverridden */
25
    const COLLECTION = null;
26
27
    const PRIMARY_KEY = 'id';
28
    const CREATED_AT = 'created_at';
29
    const UPDATED_AT = 'updated_at';
30
31
    abstract protected static function di(): Container;
32
33
    public function __construct(array $attributes = [])
34
    {
35
        $this->fill($attributes);
36
    }
37
38
    public function setPrimaryKey(string $key)
39
    {
40
        $this->primaryKey = $key;
41
    }
42
43
    public function getPrimaryKey(): string
44
    {
45
        if ($this->primaryKey == null) {
46
            $this->setPrimaryKey($this->{static::PRIMARY_KEY});
47
        }
48
        return $this->primaryKey;
49
    }
50
51
    final public static function getCollection(): string
52
    {
53
        if (static::COLLECTION == null) {
54
            throw new IllegalCollectionException();
55
        }
56
        return static::COLLECTION;
57
    }
58
59
    public function exists(bool $value = null): bool
60
    {
61
        if ($value != null) {
62
            $this->exists = $value;
63
        }
64
        return $this->exists;
65
    }
66
67
    public static function all(IQueryBuilder $query, callable $callback = null): ICollection
68
    {
69
        $collection = static::di()->getRepository()->all($query, static::class,
70
            function (IEloquent $model) use ($callback) {
71
                $model->exists(true);
72
                if ($callback != null) {
73
                    $callback($model);
74
                }
75
            });
76
        return $collection;
77
    }
78
79
    public static function find(string $id, array $columns = []): ?IEloquent
80
    {
81
        $di = static::di();
82
        $model = new static();
83
        if ($model->cache) {
84
            $cacheDecorator = new CacheDecorator($di->getRepository(), $di->getCacheRepository());
85
            $model = $cacheDecorator->find($id, static::class, $columns);
86
        } else {
87
            $model = $di->getRepository()->find($id, static::class, $columns);
88
        }
89
        if ($model != null) {
90
            $model->exists = true;
91
        }
92
        return $model;
93
    }
94
95
    public static function findOrFail(string $id, array $columns = []): IEloquent
96
    {
97
        $model = static::find($id, $columns);
98
        if ($model == null) {
99
            throw new ModelNotFoundException(get_called_class(), $id);
100
        }
101
        return $model;
102
    }
103
104
    public static function create(array $attributes): IEloquent
105
    {
106
        $model = new static($attributes);
107
        $model->save();
108
        return $model;
109
    }
110
111
    public static function destroy(string $id): void
112
    {
113
        static::findOrFail($id)->delete();
114
    }
115
116
    public function save(): void
117
    {
118
        if ($this->saving() === false) {
119
            return;
120
        }
121
        $repository = $this->di()->getRepository();
122
        if ($this->timestamps) {
123
            $this->updateTimestamps();
124
        }
125
        if (!$this->exists) {
126
            $repository->insert($this);
127
        } else {
128
            $repository->update($this);
129
        }
130
        $this->exists = true;
131
        if ($this->saved() === false) {
132
            return;
133
        }
134
    }
135
136
    public function delete(): void
137
    {
138
        if ($this->deleting() === false) {
139
            return;
140
        }
141
        $this->di()->getRepository()->delete($this);
142
        $this->exists = false;
143
        if ($this->deleted() === false) {
144
            return;
145
        }
146
    }
147
}