Completed
Pull Request — master (#22)
by Sergey
04:55
created

BaseModel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 0
loc 122
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
di() 0 1 ?
A __construct() 0 4 1
A getPrimaryKey() 0 4 1
A setPrimaryKey() 0 4 1
A exists() 0 7 2
A getCollectionName() 0 7 2
A all() 0 12 1
A find() 0 8 2
A findOrFail() 0 8 2
A create() 0 6 1
A destroy() 0 4 1
B save() 0 19 5
A delete() 0 11 3
1
<?php
2
3
namespace Isswp101\Persimmon\Model;
4
5
use Isswp101\Persimmon\Collection\ICollection;
6
use Isswp101\Persimmon\DI\Container;
7
use Isswp101\Persimmon\Exceptions\IllegalCollectionException;
8
use Isswp101\Persimmon\Exceptions\ModelNotFoundException;
9
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
10
use Isswp101\Persimmon\Traits\Containerable;
11
use Isswp101\Persimmon\Traits\Eventable;
12
use Isswp101\Persimmon\Traits\Timestampable;
13
14
/**
15
 *
16
 * @TODO
17
 * 1. Events +
18
 * 2. Timestamps +
19
 * 3. Cache
20
 * 4. Update __toString() +
21
 */
22
abstract class BaseModel implements IEloquent
23
{
24
    use Containerable, Timestampable, Eventable;
25
26
    protected $exists = false;
27
    protected $timestamps = false;
28
29
    /** @MustBeOverridden */
30
    const COLLECTION = null;
31
32
    const PRIMARY_KEY = 'id';
33
    const CREATED_AT = 'created_at';
34
    const UPDATED_AT = 'updated_at';
35
36
    abstract protected static function di(): Container;
37
38
    public function __construct(array $attributes = [])
39
    {
40
        $this->fill($attributes);
41
    }
42
43
    public function getPrimaryKey(): string
44
    {
45
        return $this->{static::PRIMARY_KEY};
46
    }
47
48
    public function setPrimaryKey(string $key)
49
    {
50
        $this->{static::PRIMARY_KEY} = $key;
51
    }
52
53
    final public static function getCollectionName(): string
54
    {
55
        if (static::COLLECTION == null) {
56
            throw new IllegalCollectionException();
57
        }
58
        return static::COLLECTION;
59
    }
60
61
    public function exists(bool $value = null): bool
62
    {
63
        if ($value != null) {
64
            $this->exists = $value;
65
        }
66
        return $this->exists;
67
    }
68
69
    public static function all(IQueryBuilder $query, array $columns = [], callable $callback = null): ICollection
70
    {
71
        $collection = static::di()->getRepository()->all(
72
            $query,
73
            static::class,
74
            $columns,
75
            function (IEloquent $model) use ($callback) {
76
                $model->exists(true);
77
                $callback($model);
78
            });
79
        return $collection;
80
    }
81
82
    public static function find($id, array $columns = []): IEloquent
83
    {
84
        $model = static::di()->getRepository()->find($id, static::class, $columns);
85
        if ($model != null) {
86
            $model->exists = true;
0 ignored issues
show
Bug introduced by
Accessing exists on the interface Isswp101\Persimmon\Contracts\Storable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
        }
88
        return $model;
89
    }
90
91
    public static function findOrFail($id, array $columns = []): IEloquent
92
    {
93
        $model = static::find($id, $columns);
94
        if ($model == null) {
95
            throw new ModelNotFoundException(get_called_class(), $id);
96
        }
97
        return $model;
98
    }
99
100
    public static function create(array $attributes): IEloquent
101
    {
102
        $model = new static($attributes);
103
        $model->save();
104
        return $model;
105
    }
106
107
    public static function destroy($id)
108
    {
109
        static::findOrFail($id)->delete();
110
    }
111
112
    public function save(array $columns = [])
113
    {
114
        if ($this->saving() === false) {
115
            return;
116
        }
117
        $repository = $this->di()->getRepository();
118
        if ($this->timestamps) {
119
            $this->updateTimestamps();
120
        }
121
        if (!$this->exists) {
122
            $repository->insert($this);
123
        } else {
124
            $repository->update($this);
125
        }
126
        $this->exists = true;
127
        if ($this->saved() === false) {
128
            return;
129
        }
130
    }
131
132
    public function delete()
133
    {
134
        if ($this->deleting() === false) {
135
            return;
136
        }
137
        $this->di()->getRepository()->delete($this->getPrimaryKey());
0 ignored issues
show
Documentation introduced by
$this->getPrimaryKey() is of type string, but the function expects a object<Isswp101\Persimmon\Contracts\Storable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
        $this->exists = false;
139
        if ($this->deleted() === false) {
140
            return;
141
        }
142
    }
143
}