Completed
Branch v1 (d74520)
by Karl
01:43
created

MultiCollection   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 106
ccs 40
cts 44
cp 0.9091
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B append() 0 17 5
B filter() 0 21 5
C orderBy() 0 23 7
A truncate() 0 8 2
1
<?php namespace JobApis\Jobs\Client;
2
3
class MultiCollection extends Collection
4
{
5
    /**
6
     * Append a collection to this collection.
7
     *
8
     * @param Collection $collection
9
     *
10
     * @return $this
11
     */
12 4
    public function append(Collection $collection)
13
    {
14
        // If there are jobs, add them to the collection
15 4
        if ($collection->count()) {
16 2
            foreach ($collection->all() as $job) {
17 2
                $this->add($job);
18 2
            }
19 2
        }
20
        // If there are errors, add them to the collection
21 4
        if ($collection->getErrors()) {
22 4
            foreach ($collection->getErrors() as $error) {
23 4
                $this->addError($error);
24 4
            }
25 4
        }
26
27 4
        return $this;
28
    }
29
30
    /**
31
     * Filter items by a field having a specific value.
32
     *
33
     * @param string $field
34
     * @param string $value
35
     * @param string $operator
36
     *
37
     * @return $this
38
     */
39 4
    public function filter($field, $value, $operator = '=')
40
    {
41 4
        $this->items = array_filter(
42 4
            $this->items,
43
            function ($item) use ($field, $value, $operator) {
44 4
                if (!isset($item->{$field})) {
45 2
                    throw new \Exception("Property not defined.");
46
                }
47 2
                if ($operator == '>') {
48
                    return $item->{$field} > $value;
49 2
                } elseif ($operator == '<') {
50
                    return $item->{$field} < $value;
51 2
                } elseif ($operator == '=') {
52 2
                    return $item->{$field} == $value;
53
                }
54
                return false;
55
            }
56 4
        );
57
58 2
        return $this;
59
    }
60
61
    /**
62
     * Order items by a field value.
63
     *
64
     * @param string $orderBy
65
     * @param string $order
66
     *
67
     * @return $this
68
     */
69 6
    public function orderBy($orderBy, $order = 'desc')
70
    {
71 6
        usort(
72 6
            $this->items,
73 6
            function ($item1, $item2) use ($orderBy, $order) {
74 6
                if (!isset($item1->{$orderBy}) || !isset($item2->{$orderBy})) {
75 2
                    throw new \Exception("Property not defined.");
76
                }
77
                // If the two items are equal, return 0
78 4
                if ($item1->{$orderBy} == $item2->{$orderBy}) {
79
                    return 0;
80
                }
81
                // If ascending, test whether Item 1 is less than Item 2
82 4
                if ($order === 'asc') {
83 2
                    return $item1->{$orderBy} < $item2->{$orderBy} ? -1 : 1;
84
                }
85
                // Else assuming descending.
86 2
                return $item1->{$orderBy} > $item2->{$orderBy} ? -1 : 1;
87
            }
88 6
        );
89
90 4
        return $this;
91
    }
92
93
    /**
94
     * Truncate the items to a maximum number of results.
95
     *
96
     * @param null $max
97
     *
98
     * @return $this
99
     */
100 2
    public function truncate($max = null)
101
    {
102 2
        if ($max) {
103 2
            $this->items = array_slice($this->items, 0, $max);
104 2
        }
105
106 2
        return $this;
107
    }
108
}
109