Completed
Push — master ( 5f476f...c14aad )
by Denis
01:46
created

Collection::setOffset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 26.07.17
6
 * Time: 11:56
7
 */
8
9
namespace Lan\Ebs\Sdk\Classes;
10
11
use ArrayObject;
12
use Exception;
13
use Lan\Ebs\Sdk\Client;
14
use Lan\Ebs\Sdk\Common;
15
use ReflectionClass;
16
17
abstract class Collection extends ArrayObject implements Common
18
{
19
    private $client;
20
21
    private $loadStatus = 0;
22
23
    private $fields = [];
24
25
    private $class = null;
26
27
    private $limit = null;
28
29
    private $offset = null;
30
31
    /**
32
     * Collection constructor.
33
     *
34
     * @param  Client $client
35
     * @param  array $fields
36
     * @param  string $class
37
     * @param  int $limit
38
     * @param  int $offset
39
     * @throws Exception
40
     */
41 3
    public function __construct(Client $client, array $fields, $class, $limit, $offset)
42
    {
43 3
        if (!$client) {
44
            throw new Exception('Client not defined');
45
        }
46
47 3
        if (!is_array($fields)) {
48
            throw new Exception('Fields for model of collection mast be array');
49
        }
50
51 3
        $reflectionClass = new ReflectionClass($class);
52
53 3
        if (!$reflectionClass->isSubclassOf(Model::class)) {
54
            throw new Exception('Class of model collection not subclass for Model');
55
        }
56
57 3
        $this->client = $client;
58 3
        $this->fields = $fields;
59 3
        $this->class = $class;
60
61 3
        $this->setLimit($limit);
62 3
        $this->setOffset($offset);
63 3
    }
64
65
    /**
66
     * @param int $limit
67
     * @throws Exception
68
     */
69 3
    public function setLimit($limit)
70
    {
71 3
        $this->limit = $limit;
72
73 3
        if ($this->loadStatus == 200) {
74
            $this->load(true);
75
        }
76 3
    }
77
78
    /**
79
     * @param bool $force
80
     * @return $this
81
     * @throws Exception
82
     */
83 3
    public function load($force = false)
84
    {
85 3
        if ($this->loadStatus == 200 && !$force) {
86
            return $this;
87
        }
88
89
        $params = [
90 3
            'limit' => $this->limit,
91 3
            'offset' => $this->offset
92
        ];
93
94 3
        if (!empty($this->fields)) {
95
            $params['fields'] = implode(',', (array)$this->fields);
96
        }
97
98 3
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__), $params);
99
100
        $this->exchangeArray($response['data']);
101
102
        $this->loadStatus = $response['status'];
103
104
        unset($response);
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param int $offset
111
     * @throws Exception
112
     */
113 3
    public function setOffset($offset)
114
    {
115 3
        $this->offset = $offset;
116
117 3
        if ($this->loadStatus == 200) {
118
            $this->load(true);
119
        }
120 3
    }
121
122
    public function getIterator()
123
    {
124
        return new CollectionIterator($this);
125
    }
126
127
    /**
128
     * @return int
129
     * @throws Exception
130
     */
131 2
    public function count()
132
    {
133 2
        $this->load();
134
135
        return parent::count();
136
    }
137
138
    /**
139
     * @return array
140
     * @throws Exception
141
     */
142
    public function getData()
143
    {
144
        $this->load();
145
146
        return $this->getArrayCopy();
147
    }
148
149
    /**
150
     * @return Model
151
     * @throws Exception
152
     */
153 1
    public function reset()
154
    {
155 1
        $this->load();
156
157
        return $this->createModel(reset($this));
158
    }
159
160
    /**
161
     * @param array $data
162
     * @return Model
163
     * @throws Exception
164
     */
165
    public function createModel(array $data = null)
166
    {
167
        $class = $this->class;
168
169
        /**
170
         * @var Model $model
171
         */
172
        $model = new $class($this->client, $this->fields);
173
174
        $model->set($data === null ? current($this) : $data);
175
176
        return $model;
177
    }
178
179
    /**
180
     * @return Model
181
     * @throws Exception
182
     */
183
    public function end()
184
    {
185
        $this->load();
186
187
        return $this->createModel(end($this));
188
    }
189
}