Completed
Push — master ( a92a0f...acd8aa )
by Ryan
08:10
created

EloquentCriteria::__call()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 11

Duplication

Lines 9
Ratio 37.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 24
rs 6.7272
cc 7
eloc 11
nc 7
nop 2
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
4
use Anomaly\Streams\Platform\Entry\EntryPresenter;
5
use Anomaly\Streams\Platform\Support\Collection;
6
use Anomaly\Streams\Platform\Support\Decorator;
7
use Anomaly\Streams\Platform\Support\Presenter;
8
use Anomaly\Streams\Platform\Traits\Hookable;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Foundation\Bus\DispatchesJobs;
11
12
/**
13
 * Class EloquentCriteria
14
 *
15
 * @link          http://anomaly.is/streams-platform
16
 * @author        AnomalyLabs, Inc. <[email protected]>
17
 * @author        Ryan Thompson <[email protected]>
18
 * @package       Anomaly\Streams\Platform\Model
19
 */
20
class EloquentCriteria
21
{
22
23
    use Hookable;
24
    use DispatchesJobs;
25
26
    /**
27
     * Safe builder methods.
28
     *
29
     * @var array
30
     */
31
    private $disabled = [
32
        'delete'
33
    ];
34
35
    /**
36
     * The query builder.
37
     *
38
     * @var Builder|\Illuminate\Database\Query\Builder
39
     */
40
    protected $query;
41
42
    /**
43
     * Set the get method.
44
     *
45
     * @var string
46
     */
47
    protected $method;
48
49
    /**
50
     * Create a new EntryCriteria instance.
51
     *
52
     * @param Builder $query
53
     * @param string  $method
54
     */
55
    public function __construct(Builder $query, $method = 'get')
56
    {
57
        $this->query  = $query;
58
        $this->method = $method;
59
    }
60
61
    /**
62
     * Get the paginated entries.
63
     *
64
     * @param array $columns
65
     * @return Collection|Presenter|EntryPresenter
66
     */
67
    public function paginate($perPage = 15, array $columns = ['*'])
68
    {
69
        return (new Decorator())->decorate($this->query->paginate($perPage, $columns));
70
    }
71
72
    /**
73
     * Get the entries.
74
     *
75
     * @param array $columns
76
     * @return Collection|Presenter|EntryPresenter
77
     */
78
    public function get(array $columns = ['*'])
79
    {
80
        return (new Decorator())->decorate($this->query->{$this->method}($columns));
81
    }
82
83
    /**
84
     * Find an entry.
85
     *
86
     * @param       $identifier
87
     * @param array $columns
88
     * @return Presenter|EntryPresenter
89
     */
90
    public function find($identifier, array $columns = ['*'])
91
    {
92
        return (new Decorator())->decorate($this->query->find($identifier, $columns));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...identifier, $columns)); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...\EloquentCriteria::find of type Anomaly\Streams\Platform\Support\Presenter|null.

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...
93
    }
94
95
    /**
96
     * Find an entry by column value.
97
     *
98
     * @param       $column
99
     * @param       $value
100
     * @param array $columns
101
     * @return Presenter|EntryPresenter
102
     */
103
    public function findBy($column, $value, array $columns = ['*'])
104
    {
105
        $this->query->where($column, $value);
106
107
        return (new Decorator())->decorate($this->query->first($columns));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...uery->first($columns)); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::findBy of type Anomaly\Streams\Platform\Support\Presenter|null.

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...
108
    }
109
110
    /**
111
     * Return the first entry.
112
     *
113
     * @param array $columns
114
     * @return EloquentModel|EntryInterface
115
     */
116
    public function first(array $columns = ['*'])
117
    {
118
        return (new Decorator())->decorate($this->query->first($columns));
119
    }
120
121
    /**
122
     * Return whether the method is safe or not.
123
     *
124
     * @param $name
125
     * @return bool
126
     */
127
    protected function methodIsSafe($name)
128
    {
129
        return (!in_array($name, $this->disabled));
130
    }
131
132
    /**
133
     * Route through __call.
134
     *
135
     * @param $name
136
     * @return Builder|null
137
     */
138
    function __get($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
    {
140
        return $this->__call($name, []);
141
    }
142
143
    /**
144
     * Call the method on the query.
145
     *
146
     * @param $name
147
     * @param $arguments
148
     * @return Builder|null
149
     */
150
    function __call($name, $arguments)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
151
    {
152
153
        if ($this->hasHook($name)) {
154
            return $this->call($name, $arguments);
155
        }
156
157
        if ($this->methodIsSafe($name)) {
158
            call_user_func_array([$this->query, $name], $arguments);
159
        }
160
161 View Code Duplication
        if (starts_with($name, 'findBy') && $column = snake_case(substr($name, 6))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
163
            call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments));
164
165
            return $this->first();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->first(); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

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...
166
        }
167
168 View Code Duplication
        if (starts_with($name, 'where') && $column = snake_case(substr($name, 5))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments));
170
        }
171
172
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Model\EloquentCriteria) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

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...
173
    }
174
}
175