EloquentCollection   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 162
rs 10
wmc 18
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A ids() 0 4 1
A pad() 0 16 4
A skip() 0 4 1
A undecorate() 0 4 1
A __call() 0 12 3
A decorated() 0 13 2
A undecorated() 0 4 1
A findBy() 0 8 1
A filterBy() 0 11 1
A __get() 0 12 3
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Support\Decorator;
4
use Anomaly\Streams\Platform\Traits\Hookable;
5
use Illuminate\Database\Eloquent\Collection;
6
7
/**
8
 * Class EloquentCollection
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class EloquentCollection extends Collection
15
{
16
17
    use Hookable;
18
19
    /**
20
     * Return the item IDs.
21
     *
22
     * @return array
23
     */
24
    public function ids()
25
    {
26
        return $this->pluck('id')->all();
27
    }
28
29
    /**
30
     * Return a collection of decorated items.
31
     *
32
     * @return static
33
     */
34
    public function decorated()
35
    {
36
        $items = [];
37
38
        /* @var Decorator $decorator */
39
        $decorator = app(Decorator::class);
40
41
        foreach ($this->items as $item) {
42
            $items[] = $decorator->decorate($item);
43
        }
44
45
        return self::make($items);
46
    }
47
48
    /**
49
     * Return undecorated items.
50
     *
51
     * @return static|$this
52
     */
53
    public function undecorated()
54
    {
55
        return $this->undecorate();
56
    }
57
58
    /**
59
     * Pad to the specified size with a value.
60
     *
61
     * @param        $size
62
     * @param  null  $value
63
     * @return $this
64
     */
65
    public function pad($size, $value = null)
66
    {
67
        if ($this->isEmpty()) {
68
            return $this;
69
        }
70
71
        if ($value) {
72
            return new static(array_pad($this->items, $size, $value));
73
        }
74
75
        while ($this->count() < $size) {
76
            $this->items = array_merge($this->items, $this->items);
77
        }
78
79
        return new static($this->items);
80
    }
81
82
    /**
83
     * Find a model by key.
84
     *
85
     * @param $key
86
     * @param $value
87
     * @return EloquentModel
88
     */
89
    public function findBy($key, $value)
90
    {
91
        return $this->undecorated()->first(
92
            function ($entry) use ($key, $value) {
93
                return $entry->{$key} === $value;
94
            }
95
        );
96
    }
97
98
    /**
99
     * Find a model by key.
100
     *
101
     * @param $key
102
     * @param $value
103
     * @return static|$this
104
     */
105
    public function filterBy($key, $value)
106
    {
107
        /* @var Decorator $decorator */
108
        $decorator = app(Decorator::class);
109
110
        return $this->filter(
111
            function ($entry) use ($key, $value, $decorator) {
112
                return $decorator->undecorate($entry)->{$key} === $value;
113
            }
114
        );
115
    }
116
117
    /**
118
     * An alias for slice.
119
     *
120
     * @param $offset
121
     * @return $this
122
     */
123
    public function skip($offset)
124
    {
125
        return $this->slice($offset, null, true);
0 ignored issues
show
Unused Code introduced by
The call to EloquentCollection::slice() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126
    }
127
128
    /**
129
     * Return undecorated items.
130
     *
131
     * @return static|$this
132
     */
133
    public function undecorate()
134
    {
135
        return new static((new Decorator())->undecorate($this->items));
136
    }
137
138
    /**
139
     * Map to get.
140
     *
141
     * @param $name
142
     * @return mixed
143
     */
144
    public function __get($name)
145
    {
146
        if ($this->hasHook($name)) {
147
            return $this->call($name, []);
148
        }
149
150
        if ($this->has($name)) {
151
            return $this->get($name);
152
        }
153
154
        return call_user_func([$this, camel_case($name)]);
155
    }
156
157
    /**
158
     * Map to get.
159
     *
160
     * @param string $method
161
     * @param array  $parameters
162
     */
163
    public function __call($method, $parameters)
164
    {
165
        if (self::hasMacro($method)) {
166
            return parent::__call($method, $parameters);
167
        }
168
169
        if ($this->hasHook($hook = snake_case($method))) {
170
            return $this->call($hook, $parameters);
171
        }
172
173
        return $this->get($method);
174
    }
175
}
176