Completed
Push — master ( 3ee305...36a69b )
by Ryan
07:04
created

EloquentCollection::findBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Illuminate\Database\Eloquent\Collection;
4
5
/**
6
 * Class EloquentCollection
7
 * The base eloquent collection used by all our models.
8
 *
9
 * @link    http://anomaly.is/streams-platform
10
 * @author  AnomalyLabs, Inc. <[email protected]>
11
 * @author  Ryan Thompson <[email protected]>
12
 * @package Anomaly\Streams\Platform\Collection
13
 */
14
class EloquentCollection extends Collection
15
{
16
17
    /**
18
     * Return the item IDs.
19
     *
20
     * @return array
21
     */
22
    public function ids()
23
    {
24
        return $this->lists('id')->all();
25
    }
26
27
    /**
28
     * Return a collection of decorated items.
29
     *
30
     * @return static
31
     */
32
    public function decorated()
33
    {
34
        $items = [];
35
36
        $decorator = app('Robbo\Presenter\Decorator');
37
38
        foreach ($this->items as $item) {
39
            $items[] = $decorator->decorate($item);
40
        }
41
42
        return self::make($items);
43
    }
44
45
    /**
46
     * Return shuffled items.
47
     *
48
     * @param int $amount
0 ignored issues
show
Bug introduced by
There is no parameter named $amount. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
     * @return static
50
     */
51 View Code Duplication
    public function shuffle()
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...
52
    {
53
        $shuffled = [];
54
55
        $keys = array_keys($this->items);
56
57
        shuffle($keys);
58
59
        foreach ($keys as $key) {
60
            $shuffled[$key] = $this->items[$key];
61
        }
62
63
        return new static($shuffled);
64
    }
65
66
    /**
67
     * Pad to the specified size with a value.
68
     *
69
     * @param       $size
70
     * @param null  $value
71
     * @return $this
72
     */
73
    public function pad($size, $value = null)
74
    {
75
        if ($this->isEmpty()) {
76
            return $this;
77
        }
78
79
        if ($value) {
80
            return new static(array_pad($this->items, $size, $value));
81
        }
82
83
        while ($this->count() < $size) {
84
            $this->items = array_merge($this->items, $this->items);
85
        }
86
87
        return new static($this->items);
88
    }
89
90
    /**
91
     * Find a model by key.
92
     *
93
     * @param $key
94
     * @param $value
95
     * @return EloquentModel
96
     */
97
    public function findBy($key, $value)
98
    {
99
        return $this->first(
100
            function ($index, $entry) use ($key, $value) {
101
                return $entry->{$key} === $value;
102
            }
103
        );
104
    }
105
106
    /**
107
     * An alias for slice.
108
     *
109
     * @param $offset
110
     * @return $this
111
     */
112
    public function skip($offset)
113
    {
114
        return $this->slice($offset, null, true);
115
    }
116
}
117