Completed
Push — master ( 85f480...889fac )
by Nekrasov
02:22 queued 33s
created

UserQuery   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 218
Duplicated Lines 4.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 1
cbo 2
dl 10
loc 218
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getByLogin() 0 6 1
A getByEmail() 0 6 1
A count() 0 18 2
A groupsMustBeSelected() 0 4 3
A normalizeFilter() 0 7 1
A normalizeSelect() 10 10 2
B getList() 0 33 6
A normalizeUfSelect() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function getList()
94
    {
95
        if ($this->queryShouldBeStopped) {
96
            return new Collection();
97
        }
98
    
99
        $queryType = 'UserQuery::getList';
100
        $sort = $this->sort;
101
        $filter = $this->normalizeFilter();
102
        $params = [
103
            'SELECT'     => $this->propsMustBeSelected() ? ['UF_*'] : ($this->normalizeUfSelect() ?: false),
104
            'NAV_PARAMS' => $this->navigation,
105
            'FIELDS'     => $this->normalizeSelect(),
106
        ];
107
        $selectGroups = $this->groupsMustBeSelected();
108
        $keyBy = $this->keyBy;
109
110
        $callback = function() use ($sort, $filter, $params, $selectGroups){
111
            $users = [];
112
            $rsUsers = $this->bxObject->getList($sort, $sortOrder = false, $filter, $params);
113
            while ($arUser = $this->performFetchUsingSelectedMethod($rsUsers)) {
114
                if ($selectGroups) {
115
                    $arUser['GROUP_ID'] = $this->bxObject->getUserGroup($arUser['ID']);
116
                }
117
        
118
                $this->addItemToResultsUsingKeyBy($users, new $this->modelName($arUser['ID'], $arUser));
119
            }
120
    
121
            return new Collection($users);
122
        };
123
124
        return $this->handleCacheIfNeeded(compact('queryType', 'sort', 'filter', 'params', 'selectGroups', 'keyBy'), $callback);
125
    }
126
127
    /**
128
     * Get the first user with a given login.
129
     *
130
     * @param string $login
131
     *
132
     * @return UserModel
133
     */
134
    public function getByLogin($login)
135
    {
136
        $this->filter['LOGIN_EQUAL_EXACT'] = $login;
137
138
        return $this->first();
139
    }
140
141
    /**
142
     * Get the first user with a given email.
143
     *
144
     * @param string $email
145
     *
146
     * @return UserModel
147
     */
148
    public function getByEmail($email)
149
    {
150
        $this->filter['EMAIL'] = $email;
151
152
        return $this->first();
153
    }
154
155
    /**
156
     * Get count of users according the current query.
157
     *
158
     * @return int
159
     */
160
    public function count()
161
    {
162
        if ($this->queryShouldBeStopped) {
163
            return 0;
164
        }
165
166
        $queryType = 'UserQuery::count';
167
        $filter = $this->normalizeFilter();
168
        $callback = function() use ($filter) {
169
            return (int) $this->bxObject->getList($order = 'ID', $by = 'ASC', $filter, [
170
                'NAV_PARAMS' => [
171
                    'nTopCount' => 0,
172
                ],
173
            ])->NavRecordCount;
174
        };
175
176
        return $this->handleCacheIfNeeded(compact('queryType', 'filter'), $callback);
177
    }
178
179
    /**
180
     * Determine if groups must be selected.
181
     *
182
     * @return bool
183
     */
184
    protected function groupsMustBeSelected()
185
    {
186
        return in_array('GROUPS', $this->select) || in_array('GROUP_ID', $this->select) || in_array('GROUPS_ID', $this->select);
187
    }
188
189
    /**
190
     * Normalize filter before sending it to getList.
191
     * This prevents some inconsistency.
192
     *
193
     * @return array
194
     */
195
    protected function normalizeFilter()
196
    {
197
        $this->substituteField($this->filter, 'GROUPS', 'GROUPS_ID');
198
        $this->substituteField($this->filter, 'GROUP_ID', 'GROUPS_ID');
199
200
        return $this->filter;
201
    }
202
203
    /**
204
     * Normalize select before sending it to getList.
205
     * This prevents some inconsistency.
206
     *
207
     * @return array
208
     */
209 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...
210
    {
211
        if ($this->fieldsMustBeSelected()) {
212
            $this->select = array_merge($this->standardFields, $this->select);
213
        }
214
215
        $this->select[] = 'ID';
216
217
        return $this->clearSelectArray();
218
    }
219
220
    /**
221
     * Normalize select UF before sending it to getList.
222
     *
223
     * @return array
224
     */
225
    protected function normalizeUfSelect()
226
    {
227
        return preg_grep('/^(UF_+)/', $this->select);
228
    }
229
}
230