Completed
Push — master ( 7e75f8...53d3fb )
by Ryan
10:44 queued 05:05
created

EloquentCollection::undecorate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
        $decorator = app('Robbo\Presenter\Decorator');
39
40
        foreach ($this->items as $item) {
41
            $items[] = $decorator->decorate($item);
42
        }
43
44
        return self::make($items);
45
    }
46
47
    /**
48
     * Pad to the specified size with a value.
49
     *
50
     * @param        $size
51
     * @param  null  $value
52
     * @return $this
53
     */
54
    public function pad($size, $value = null)
55
    {
56
        if ($this->isEmpty()) {
57
            return $this;
58
        }
59
60
        if ($value) {
61
            return new static(array_pad($this->items, $size, $value));
62
        }
63
64
        while ($this->count() < $size) {
65
            $this->items = array_merge($this->items, $this->items);
66
        }
67
68
        return new static($this->items);
69
    }
70
71
    /**
72
     * Find a model by key.
73
     *
74
     * @param $key
75
     * @param $value
76
     * @return EloquentModel
77
     */
78
    public function findBy($key, $value)
79
    {
80
        return $this->first(
81
            function ($entry) use ($key, $value) {
82
                return $entry->{$key} === $value;
83
            }
84
        );
85
    }
86
87
    /**
88
     * An alias for slice.
89
     *
90
     * @param $offset
91
     * @return $this
92
     */
93
    public function skip($offset)
94
    {
95
        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...
96
    }
97
98
    /**
99
     * Return undecorated items.
100
     *
101
     * @return static|$this
102
     */
103
    public function undecorate()
104
    {
105
        return new static((new Decorator())->undecorate($this->items));
106
    }
107
108
    /**
109
     * Map to get.
110
     *
111
     * @param $name
112
     * @return mixed
113
     */
114
    public function __get($name)
115
    {
116
        if ($this->hasHook($hook = 'get_' . $name)) {
117
            return $this->call($hook, []);
118
        }
119
120
        if ($this->has($name)) {
121
            return $this->get($name);
122
        }
123
124
        return call_user_func([$this, camel_case($name)]);
125
    }
126
127
    /**
128
     * Map to get.
129
     *
130
     * @param string $method
131
     * @param array  $parameters
132
     */
133 View Code Duplication
    public function __call($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
134
    {
135
        if ($this->hasHook($hook = snake_case($method))) {
136
            return $this->call($hook, $parameters);
137
        }
138
139
        return $this->get($method);
140
    }
141
}
142