Completed
Pull Request — master (#22)
by Sergey
03:51
created

BaseModel::save()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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