UserQuery::prepareMultiFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Illuminate\Support\Collection;
6
use Arrilot\BitrixModels\Models\UserModel;
7
8
/**
9
 * @method UserQuery active()
10
 * @method UserQuery fromGroup($groupId)
11
 */
12
class UserQuery extends OldCoreQuery
13
{
14
    /**
15
     * Query sort.
16
     *
17
     * @var array
18
     */
19
    public $sort = ['last_name' => 'asc'];
20
21
    /**
22
     * List of standard entity fields.
23
     *
24
     * @var array
25
     */
26
    protected $standardFields = [
27
        'ID',
28
        'PERSONAL_WWW',
29
        'PERSONAL_ZIP',
30
        'IS_ONLINE',
31
        'ACTIVE',
32
        'PERSONAL_ICQ',
33
        'PERSONAL_COUNTRY',
34
        'WORK_CITY',
35
        'LAST_LOGIN',
36
        'PERSONAL_GENDER',
37
        'PERSONAL_NOTES',
38
        'WORK_STATE',
39
        'LOGIN',
40
        'PERSONAL_PHOTO',
41
        'WORK_COMPANY',
42
        'WORK_ZIP',
43
        'EMAIL',
44
        'PERSONAL_PHONE',
45
        'WORK_DEPARTMENT',
46
        'WORK_COUNTRY',
47
        'NAME',
48
        'PERSONAL_FAX',
49
        'WORK_POSITION',
50
        'WORK_PROFILE',
51
        'LAST_NAME',
52
        'PERSONAL_MOBILE',
53
        'WORK_WWW',
54
        'WORK_NOTES',
55
        'SECOND_NAME',
56
        'PERSONAL_PAGER',
57
        'WORK_PHONE',
58
        'ADMIN_NOTES',
59
        'TIMESTAMP_X',
60
        'PERSONAL',
61
        'STREET',
62
        'WORK_FAX',
63
        'XML_ID',
64
        'PERSONAL_BIRTHDAY',
65
        'PERSONAL_MAILBOX',
66
        'WORK_PAGER',
67
        'LAST_NAME',
68
        'DATE_REGISTER',
69
        'PERSONAL_CITY',
70
        'WORK_STREET',
71
        'SECOND_NAME',
72
        'PERSONAL_PROFESSION',
73
        'PERSONAL_STATE',
74
        'WORK_MAILBOX',
75
        'STORED_HASH',
76
        'CHECKWORD_TIME',
77
        'EXTERNAL_AUTH_ID',
78
        'CONFIRM_CODE',
79
        'LOGIN_ATTEMPTS',
80
        'LAST_ACTIVITY_DATE',
81
        'AUTO_TIME_ZONE',
82
        'TIME_ZONE',
83
        'PASSWORD',
84
        'CHECKWORD',
85
        'LID',
86
    ];
87
88
    /**
89
     * Get the collection of users according to the current query.
90
     *
91
     * @return Collection
92
     */
93
    protected function loadModels()
94
    {
95
        $queryType = 'UserQuery::getList';
96
        $sort = $this->sort;
97
        $filter = $this->normalizeFilter();
98
        $params = [
99
            'SELECT'     => $this->propsMustBeSelected() ? ['UF_*'] : ($this->normalizeUfSelect() ?: false),
100
            'NAV_PARAMS' => $this->navigation,
101
            'FIELDS'     => $this->normalizeSelect(),
102
        ];
103
        $selectGroups = $this->groupsMustBeSelected();
104
        $keyBy = $this->keyBy;
105
106
        $callback = function() use ($sort, $filter, $params, $selectGroups){
107
            $users = [];
108
            $rsUsers = $this->bxObject->getList($sort, $sortOrder = false, $filter, $params);
109
            while ($arUser = $this->performFetchUsingSelectedMethod($rsUsers)) {
110
                if ($selectGroups) {
111
                    $arUser['GROUP_ID'] = $this->bxObject->getUserGroup($arUser['ID']);
112
                }
113
        
114
                $this->addItemToResultsUsingKeyBy($users, new $this->modelName($arUser['ID'], $arUser));
115
            }
116
    
117
            return new Collection($users);
118
        };
119
120
        return $this->handleCacheIfNeeded(compact('queryType', 'sort', 'filter', 'params', 'selectGroups', 'keyBy'), $callback);
121
    }
122
123
    /**
124
     * Get the first user with a given login.
125
     *
126
     * @param string $login
127
     *
128
     * @return UserModel
129
     */
130
    public function getByLogin($login)
131
    {
132
        $this->filter['LOGIN_EQUAL_EXACT'] = $login;
133
134
        return $this->first();
135
    }
136
137
    /**
138
     * Get the first user with a given email.
139
     *
140
     * @param string $email
141
     *
142
     * @return UserModel
143
     */
144
    public function getByEmail($email)
145
    {
146
        $this->filter['EMAIL'] = $email;
147
148
        return $this->first();
149
    }
150
151
    /**
152
     * Get count of users according the current query.
153
     *
154
     * @return int
155
     */
156
    public function count()
157
    {
158
        if ($this->queryShouldBeStopped) {
159
            return 0;
160
        }
161
162
        $queryType = 'UserQuery::count';
163
        $filter = $this->normalizeFilter();
164
        $callback = function() use ($filter) {
165
            return (int) $this->bxObject->getList($order = 'ID', $by = 'ASC', $filter, [
166
                'NAV_PARAMS' => [
167
                    'nTopCount' => 0,
168
                ],
169
            ])->NavRecordCount;
170
        };
171
172
        return $this->handleCacheIfNeeded(compact('queryType', 'filter'), $callback);
173
    }
174
175
    /**
176
     * Determine if groups must be selected.
177
     *
178
     * @return bool
179
     */
180
    protected function groupsMustBeSelected()
181
    {
182
        return in_array('GROUPS', $this->select) || in_array('GROUP_ID', $this->select) || in_array('GROUPS_ID', $this->select);
183
    }
184
185
    /**
186
     * Normalize filter before sending it to getList.
187
     * This prevents some inconsistency.
188
     *
189
     * @return array
190
     */
191
    protected function normalizeFilter()
192
    {
193
        $this->substituteField($this->filter, 'GROUPS', 'GROUPS_ID');
194
        $this->substituteField($this->filter, 'GROUP_ID', 'GROUPS_ID');
195
196
        return $this->filter;
197
    }
198
199
    /**
200
     * Normalize select before sending it to getList.
201
     * This prevents some inconsistency.
202
     *
203
     * @return array
204
     */
205 View Code Duplication
    protected function normalizeSelect()
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...
206
    {
207
        if ($this->fieldsMustBeSelected()) {
208
            $this->select = array_merge($this->standardFields, $this->select);
209
        }
210
211
        $this->select[] = 'ID';
212
213
        return $this->clearSelectArray();
214
    }
215
216
    /**
217
     * Normalize select UF before sending it to getList.
218
     *
219
     * @return array
220
     */
221
    protected function normalizeUfSelect()
222
    {
223
        return preg_grep('/^(UF_+)/', $this->select);
224
    }
225
    
226
    protected function prepareMultiFilter(&$key, &$value)
227
    {
228
        $value = join(' | ', $value);
229
    }
230
}
231