EloquentBaseRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 146
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 4 1
A paginate() 0 4 1
A create() 0 9 1
A resetModel() 0 4 1
A make() 0 4 1
A update() 0 14 2
A delete() 0 11 2
A find() 0 7 1
A forModel() 0 6 1
A count() 0 4 1
1
<?php
2
3
namespace Yajra\Address\Repositories;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
8
abstract class EloquentBaseRepository extends RepositoryAbstract implements EloquentRepositoryInterface
9
{
10
    /**
11
     * BaseRepository constructor.
12
     */
13
    public function __construct()
14
    {
15
        $this->model = $this->getModel();
16
    }
17
18
    /**
19
     * Get all records.
20
     *
21
     * @return \Illuminate\Database\Eloquent\Collection
22
     */
23
    public function all()
24
    {
25
        return $this->model->newQuery()->get();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->model->newQuery()->get(); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 25 which is incompatible with the return type declared by the interface Yajra\Address\Repositori...epositoryInterface::all of type Illuminate\Database\Eloquent\Collection.
Loading history...
26
    }
27
28
    /**
29
     * Paginate records.
30
     *
31
     * @param int $limit
32
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
33
     */
34
    public function paginate($limit = 10)
35
    {
36
        return $this->model->newQuery()->paginate($limit);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->model->new...ry()->paginate($limit); (Illuminate\Pagination\LengthAwarePaginator) is incompatible with the return type declared by the interface Yajra\Address\Repositori...toryInterface::paginate of type Illuminate\Contracts\Pag...on\LengthAwarePaginator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
37
    }
38
39
    /**
40
     * Save a new entity in repository.
41
     *
42
     * @param array $attributes
43
     * @return mixed
44
     */
45
    public function create(array $attributes)
46
    {
47
        $model = $this->model->newInstance($attributes);
48
        $model->save();
49
50
        $this->resetModel();
51
52
        return $model;
53
    }
54
55
    /**
56
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
57
     */
58
    public function resetModel()
59
    {
60
        return $this->model = $this->getModel();
61
    }
62
63
    /**
64
     * Make a new entity in repository.
65
     *
66
     * @param array $attributes
67
     * @return \Illuminate\Database\Eloquent\Model
68
     */
69
    public function make(array $attributes)
70
    {
71
        return $this->model->forceFill($attributes);
72
    }
73
74
    /**
75
     * Update a entity in repository by id.
76
     *
77
     * @param array $attributes
78
     * @param int   $id
79
     * @return \Illuminate\Database\Eloquent\Model
80
     */
81
    public function update(array $attributes, $id)
82
    {
83
        if ($id instanceof Model) {
84
            $id = $id->getKey();
85
        }
86
87
        $model = $this->model->findOrFail($id);
88
        $model->fill($attributes);
89
        $model->save();
90
91
        $this->resetModel();
92
93
        return $model;
94
    }
95
96
    /**
97
     * Delete a entity in repository by id.
98
     *
99
     * @param int $id
100
     * @return bool|null
101
     * @throws \Exception
102
     */
103
    public function delete($id)
104
    {
105
        if ($id instanceof Model) {
106
            $id = $id->getKey();
107
        }
108
109
        $model = $this->find($id);
110
        $this->resetModel();
111
112
        return $model->delete();
113
    }
114
115
    /**
116
     * Find data by id.
117
     *
118
     * @param int   $id
119
     * @param array $columns
120
     * @return \Illuminate\Database\Eloquent\Model
121
     * @throws ModelNotFoundException
122
     */
123
    public function find($id, $columns = ['*'])
124
    {
125
        $model = $this->model->findOrFail($id, $columns);
126
        $this->resetModel();
127
128
        return $model;
129
    }
130
131
    /**
132
     * Set repository model instance.
133
     *
134
     * @param \Illuminate\Database\Eloquent\Model $model
135
     * @return $this
136
     */
137
    public function forModel(Model $model)
138
    {
139
        $this->model = $model;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Count all records.
146
     *
147
     * @return int
148
     */
149
    public function count()
150
    {
151
        return $this->model->count();
152
    }
153
}
154