Completed
Push — master ( 1f59ba...d3bd3a )
by Nekrasov
02:20
created

Collector::performQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
nop 0
1
<?php
2
3
namespace Arrilot\Collectors;
4
5
abstract class Collector
6
{
7
    /**
8
     * Ids that are collected from sources.
9
     *
10
     * @var array
11
     */
12
    protected $ids = [];
13
14
    /**
15
     * Fields that should be selected.
16
     *
17
     * @var mixed
18
     */
19
    protected $select = null;
20
21
    /**
22
     * Additional filter.
23
     *
24
     * @var mixed
25
     */
26
    protected $where = null;
27
28
    /**
29
     * Data keyed by id.
30
     *
31
     * @var array
32
     */
33
    protected $data = [];
34
35
    /**
36
     * Get data for given ids.
37
     *
38
     * @param array $ids
39
     * @return array
40
     */
41
    abstract protected function getList(array $ids);
42
43
    /**
44
     * Add a collection source.
45
     *
46
     * @param $collection
47
     * @param mixed $fields
48
     * @return $this
49
     */
50
    public function fromCollection($collection, $fields)
51
    {
52
        $fields = (array) $fields;
53
        foreach ($collection as $item) {
54
            $this->collectIdsFromItem($item, $fields);
55
        }
56
57
        return $this;
58
    }
59
60
    /**
61
     * Add an item source.
62
     *
63
     * @param $item
64
     * @param mixed $fields
65
     * @return $this
66
     */
67
    public function fromItem($item, $fields)
68
    {
69
        $fields = (array) $fields;
70
        $this->collectIdsFromItem($item, $fields);
71
72
        return $this;
73
    }
74
75
    /**
76
     * Setter for select.
77
     *
78
     * @param mixed $select
79
     * @return $this
80
     */
81
    public function select($select)
82
    {
83
        $this->select = $select;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Setter for where.
90
     *
91
     * @param mixed $where
92
     * @return $this
93
     */
94
    public function where($where)
95
    {
96
        $this->where = $where;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Perform main query.
103
     *
104
     * @return array
105
     */
106
    public function performQuery()
107
    {
108
        if (!$this->ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
109
            return [];
110
        }
111
112
        return $this->getList($this->getIdsWithoutDuplications());
113
    }
114
    
115
    /**
116
     * Collect ids from source and add them to $this->ids
117
     *
118
     * @param $item
119
     * @param array $fields
120
     */
121
    protected function collectIdsFromItem($item, array $fields)
122
    {
123
        foreach ($fields as $field) {
124
            foreach ((array) $item[$field] as $id) {
125
                if ((int) $id) {
126
                    $this->ids[] = (int) $id;
127
                }
128
            }
129
        }
130
    }
131
132
    /**
133
     * A faster alternative to array_unique.
134
     *
135
     * @return array
136
     */
137
    protected function getIdsWithoutDuplications()
138
    {
139
        return array_flip(array_flip($this->ids));
140
    }
141
}
142