Passed
Push — master ( 01acd6...7c9a77 )
by Konstantin
02:41
created

ReadUsersBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 208
ccs 44
cts 46
cp 0.9565
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 0 5 1
A mainFilterItem() 0 5 1
A orderByField() 0 5 1
A getValidStatuses() 0 3 1
A accountManagerId() 0 5 1
A toArray() 0 3 1
A createRoute() 0 3 1
A offset() 0 5 1
A getValidFieldsValues() 0 3 1
A dateInsertedTo() 0 9 2
A fields() 0 7 2
A statuses() 0 7 2
A orderByMethod() 0 9 3
A dateInsertedFrom() 0 9 2
1
<?php
2
3
namespace linkprofit\Tracker\builder;
4
5
use linkprofit\Tracker\request\ReadUsersRoute;
6
7
/**
8
 * Class ReadUsersBuilder
9
 *
10
 * @package linkprofit\Tracker\filter
11
 */
12
class ReadUsersBuilder implements BuilderInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    public $params = [];
18
19
    /**
20
     * @var array
21
     */
22
    private $allowedFields = [
23
        'userid', 'refid', 'username', 'apikey',
24
        'firstname', 'lastname', 'middlename',
25
        'topname', 'phone', 'city', 'regip',
26
        'dateinserted', 'datelastlogin',
27
        'status', 'commissionrate', 'managerid',
28
    ];
29
30
    /**
31
     * A — одобрен
32
     * P — в ожидании
33
     * D — отклонён
34
     *
35
     * @var array
36
     */
37
    private $allowedStatuses = [
38
        'A', 'P', 'D'
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private $allowedSorting = [
45
        SORT_DESC, SORT_ASC
46
    ];
47
48
    /**
49
     * @return array
50
     */
51 6
    public function toArray()
52
    {
53 6
        return $this->params;
54
    }
55
56
    /**
57
     * @return ReadUsersRoute
58
     */
59 5
    public function createRoute()
60
    {
61 5
        return new ReadUsersRoute($this);
62
    }
63
64
    /**
65
     * @param $limit
66
     *
67
     * @return $this
68
     */
69 6
    public function limit($limit)
70
    {
71 6
        $this->params['limit'] = $limit;
72
73 6
        return $this;
74
    }
75
76
    /**
77
     * @param $offset
78
     *
79
     * @return $this
80
     */
81 1
    public function offset($offset)
82
    {
83 1
        $this->params['offset'] = $offset;
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @param array $fields
90
     *
91
     * @return $this
92
     */
93 6
    public function fields($fields = [])
94
    {
95 6
        if (!empty($fields)) {
96 6
            $this->params['fields'] = $this->getValidFieldsValues($fields);
97
        }
98
99 6
        return $this;
100
    }
101
102
    /**
103
     * @param array $statuses
104
     *
105
     * @return $this
106
     */
107 6
    public function statuses($statuses = [])
108
    {
109 6
        if (!empty($statuses)) {
110 6
            $this->params['statuses'] = $this->getValidStatuses($statuses);
111
        }
112
113 6
        return $this;
114
    }
115
116
    /**
117
     * @param integer $unixTime
118
     *
119
     * @return $this
120
     */
121 1
    public function dateInsertedFrom($unixTime = null)
122
    {
123 1
        if ($unixTime === null) {
124
            $unixTime = time();
125
        }
126
127 1
        $this->params['dateInsertedFrom'] = date('d.m.Y', $unixTime);
128
129 1
        return $this;
130
    }
131
132
    /**
133
     * @param integer $unixTime
134
     *
135
     * @return $this
136
     */
137 1
    public function dateInsertedTo($unixTime = null)
138
    {
139 1
        if ($unixTime === null) {
140
            $unixTime = time();
141
        }
142
143 1
        $this->params['dateInsertedTo'] = date('d.m.Y', $unixTime);
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * @param $field
150
     *
151
     * @return $this
152
     */
153 1
    public function orderByField($field)
154
    {
155 1
        $this->params['orderByField'] = $field;
156
157 1
        return $this;
158
    }
159
160
    /**
161
     * @param $method
162
     *
163
     * @return $this
164
     */
165 1
    public function orderByMethod($method)
166
    {
167 1
        if (in_array($method, $this->allowedSorting, true)) {
168 1
            $method = ($method === SORT_ASC) ? 'ASC' : 'DESC';
169
        }
170
171 1
        $this->params['orderByMethod'] = $method;
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Like поиск по полям, поля не указываются
178
     *
179
     * @param string $term
180
     *
181
     * @return $this
182
     */
183 1
    public function mainFilterItem($term)
184
    {
185 1
        $this->params['mainFilterItem'] = (string) $term;
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * @param integer $id
192
     *
193
     * @return $this
194
     */
195 1
    public function accountManagerId($id)
196
    {
197 1
        $this->params['accountManagerId'] = $id;
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * @param array $fields
204
     *
205
     * @return array
206
     */
207 6
    protected function getValidFieldsValues($fields)
208
    {
209 6
        return array_values(array_intersect(array_map('strtolower', $fields), $this->allowedFields));
210
    }
211
212
    /**
213
     * @param $statuses
214
     *
215
     * @return array
216
     */
217 6
    protected function getValidStatuses($statuses)
218
    {
219 6
        return array_values(array_intersect(array_map('strtoupper', $statuses), $this->allowedStatuses));
220
    }
221
}
222