Completed
Push — master ( 136cb7...59b361 )
by Nekrasov
02:22
created

D7Query::runtime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixModels\Adapters\D7Adapter;
6
use Illuminate\Support\Collection;
7
8
class D7Query extends BaseQuery
9
{
10
    /**
11
     * Query select.
12
     *
13
     * @var array
14
     */
15
    public $select = ['*'];
16
17
    /**
18
     * Query group by.
19
     *
20
     * @var array
21
     */
22
    public $group = [];
23
24
    /**
25
     * Query runtime.
26
     *
27
     * @var array
28
     */
29
    public $runtime = [];
30
31
    /**
32
     * Query limit.
33
     *
34
     * @var int|null
35
     */
36
    public $limit = null;
37
38
    /**
39
     * Query offset.
40
     *
41
     * @var int|null
42
     */
43
    public $offset = null;
44
45
    /**
46
     * Cache joins?
47
     *
48
     * @var bool
49
     */
50
    public $cacheJoins = false;
51
52
    /**
53
     * Data doubling?
54
     *
55
     * @var bool
56
     */
57
    public $dataDoubling = true;
58
59
    /**
60
     * Adapter to interact with Bitrix D7 API.
61
     *
62
     * @var D7Adapter
63
     */
64
    protected $bxObject;
65
66
    /**
67
     * Get count of users that match $filter.
68
     *
69
     * @return int
70
     */
71
    public function count()
72
    {
73
        $className = $this->bxObject->getClassName();
74
        $queryType = 'D7Query::count';
75
        $filter = $this->filter;
76
77
        $callback = function () use ($filter) {
78
            return (int) $this->bxObject->getCount($filter);
79
        };
80
81
        return $this->handleCacheIfNeeded(compact('className', 'filter', 'queryType'), $callback);
82
    }
83
    
84
    /**
85
     * Get list of items.
86
     *
87
     * @return Collection
88
     */
89
    public function getList()
90
    {
91
        if ($this->queryShouldBeStopped) {
92
            return new Collection();
93
        }
94
95
        $params = [
96
            'select' => $this->select,
97
            'filter' => $this->filter,
98
            'group' => $this->group,
99
            'order' => $this->sort,
100
            'limit' => $this->limit,
101
            'offset' => $this->offset,
102
            'runtime' => $this->runtime,
103
        ];
104
105
        if ($this->cacheTtl && $this->cacheJoins) {
106
            $params['cache'] = ['ttl' => $this->cacheTtl, 'cache_joins' => true];
107
        }
108
109
        $className = $this->bxObject->getClassName();
110
        $queryType = 'D7Query::getList';
111
        $keyBy = $this->keyBy;
112
113
        $callback = function () use ($className, $params) {
114
            $rows = [];
115
            $result = $this->bxObject->getList($params);
116
            while ($row = $result->fetch()) {
117
                $this->addItemToResultsUsingKeyBy($rows, new $this->modelName($row['ID'], $row));
118
            }
119
120
            return new Collection($rows);
121
        };
122
123
        return $this->handleCacheIfNeeded(compact('className', 'params', 'queryType', 'keyBy'), $callback);
124
    }
125
126
    /**
127
     * Setter for limit.
128
     *
129
     * @param  int|null  $value
130
     * @return $this
131
     */
132
    public function limit($value)
133
    {
134
        $this->limit = $value;
135
        
136
        return $this;
137
    }
138
139
    /**
140
     * Setter for offset.
141
     *
142
     * @param  int|null  $value
143
     * @return $this
144
     */
145
    public function offset($value)
146
    {
147
        $this->offset = $value;
148
149
        return $this;
150
    }
151
    
152
    /**
153
     * Setter for offset.
154
     *
155
     * @param  array|\Bitrix\Main\Entity\ExpressionField $fields
156
     * @return $this
157
     */
158
    public function runtime($fields)
159
    {
160
        $this->runtime = is_array($fields) ? $fields : [$fields];
161
162
        return $this;
163
    }
164
165
    /**
166
     * Setter for cacheJoins.
167
     *
168
     * @param  bool $value
169
     * @return $this
170
     */
171
    public function cacheJoins($value = true)
172
    {
173
        $this->cacheJoins = $value;
174
175
        return $this;
176
    }
177
178
    public function enableDataDoubling()
179
    {
180
        $this->dataDoubling = true;
181
182
        return $this;
183
    }
184
185
    public function disableDataDoubling()
186
    {
187
        $this->dataDoubling = false;
188
189
        return $this;
190
    }
191
    
192
    /**
193
     * For testing.
194
     *
195
     * @param $bxObject
196
     * @return $this
197
     */
198
    public function setAdapter($bxObject)
199
    {
200
        $this->bxObject = $bxObject;
201
202
        return $this;
203
    }
204
}
205