Completed
Pull Request — master (#22)
by Sergey
18:05 queued 03:05
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\Contracts\Presentable;
6
use Isswp101\Persimmon\Contracts\Storable;
7
use Isswp101\Persimmon\DI\Container;
8
use Isswp101\Persimmon\Exceptions\IllegalCollectionException;
9
use Isswp101\Persimmon\Exceptions\ModelNotFoundException;
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, Storable, Presentable
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
    public static function getCollection(): 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 find($id, array $columns = null): IEloquent
70
    {
71
        $model = static::di()->getRepository()->find($id, static::class, $columns);
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 69 can also be of type null; however, Isswp101\Persimmon\Repository\IRepository::find() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
        if ($model != null) {
73
            $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...
74
        }
75
        return $model;
76
    }
77
78
    public static function findOrFail($id, array $columns = null): IEloquent
79
    {
80
        $model = static::find($id, $columns);
81
        if ($model == null) {
82
            throw new ModelNotFoundException(get_called_class(), $id);
83
        }
84
        return $model;
85
    }
86
87
    public static function create(array $attributes): IEloquent
88
    {
89
        return null;
90
    }
91
92
    public static function destroy($id)
93
    {
94
        return null;
95
    }
96
97
    public function save(array $columns = null)
98
    {
99
        if ($this->saving() === false) {
100
            return;
101
        }
102
        $repository = $this->di()->getRepository();
103
        if ($this->timestamps) {
104
            $this->updateTimestamps();
105
        }
106
        if (!$this->exists) {
107
            $repository->insert($this);
108
        } else {
109
            $repository->update($this);
110
        }
111
        $this->exists = true;
112
        if ($this->saved() === false) {
113
            return;
114
        }
115
    }
116
117
    public function delete()
118
    {
119
        if ($this->deleting() === false) {
120
            return;
121
        }
122
        $this->di()->getRepository()->delete($this->getId());
123
        $this->exists = false;
124
        if ($this->deleted() === false) {
125
            return;
126
        }
127
    }
128
}