BaseRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 5 1
A getModel() 0 4 1
A findAll() 0 4 1
A createNew() 0 8 2
A findItemById() 0 8 2
A update() 0 12 3
A delete() 0 8 2
1
<?php
2
3
namespace GuardiansLabs\Repository\Repositories;
4
5
use GuardiansLabs\Repository\Contracts\RepositoryContract;
6
use GuardiansLabs\Repository\Exceptions\RepositoryException;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
10
class BaseRepository implements RepositoryContract
11
{
12
13
    /**
14
     * The repository model.
15
     * @var Model
16
     */
17
    protected $model;
18
19
    /**
20
     * @param Model $model
21
     * @return object
22
     */
23
    public function setModel(Model $model)
24
    {
25
        $this->model = $model;
26
        return $this;
27
    }
28
29
    /**
30
     * @return Model
31
     */
32
    public function getModel()
33
    {
34
        return $this->model;
35
    }
36
37
    /**
38
     * @return Collection
39
     */
40
    public function findAll()
41
    {
42
        return $this->model->get();
43
    }
44
45
    /**
46
     * @param array $data
47
     * @return Collection
48
     * @throws RepositoryException
49
     */
50
    public function createNew(array $data)
51
    {
52
        if (empty($data)) {
53
            throw new RepositoryException("No Given Data To Insert");
54
        }
55
        $this->model->fill($data);
56
        return $this->model->save();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->model->save(); (boolean) is incompatible with the return type declared by the interface GuardiansLabs\Repository...toryContract::createNew of type Illuminate\Support\Collection.

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...
57
    }
58
59
    /**
60
     * @param $itemId
61
     * @throws RepositoryException
62
     * @return Collection
63
     */
64
    public function findItemById($itemId)
65
    {
66
        $item = $this->model->find($itemId);
67
        if (!$item) {
68
            throw new RepositoryException("Item Not Found");
69
        }
70
        return $item;
71
    }
72
73
    /**
74
     * @param $itemId
75
     * @param array $data
76
     * @throws RepositoryException
77
     * @return mixed
78
     */
79
    public function update($itemId, array $data)
80
    {
81
        $item = $this->model->find($itemId);
82
        if (empty($data)) {
83
            throw new RepositoryException("No Given Data To Update");
84
        }
85
        if (!$item) {
86
            throw new RepositoryException("No Item Found To Update");
87
        }
88
        $item->fill($data);
89
        return $item->save();
90
    }
91
92
    /**
93
     * @param $itemId
94
     * @return mixed
95
     * @throws RepositoryException
96
     */
97
    public function delete($itemId)
98
    {
99
        $item = $this->model->find($itemId);
100
        if (!$item) {
101
            throw new RepositoryException("No Item Found To delete");
102
        }
103
        return $item->delete();
104
    }
105
}
106