Completed
Push — master ( e8241e...60d0c3 )
by
unknown
04:24 queued 02:47
created

Model::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.1406
1
<?php
2
3
namespace Lan\Ebs\Sdk\Classes;
4
5
use Exception;
6
use Lan\Ebs\Sdk\Client;
7
use Lan\Ebs\Sdk\Common;
8
9
abstract class Model implements Common
10
{
11
    const MESSAGE_ID_REQUIRED = 'Id is required';
12
    const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed';
13
14
    private $client;
15
16
    private $fields = [];
17
18
    private $data = null;
19
20
    private $id = null;
21
22
    private $lastStatus = 0;
23
24
    /**
25
     * Model constructor.
26
     *
27
     * @param  Client $client
28
     * @param  $fields
29
     * @throws Exception
30
     */
31 6
    public function __construct(Client $client, array $fields)
32
    {
33 6
        if (!$client) {
34
            throw new Exception('Client not defined');
35
        }
36
37 6
        if (!is_array($fields)) {
38
            throw new Exception('Fields for model of collection mast be array');
39
        }
40
41 6
        $this->client = $client;
42 6
        $this->fields = $fields;
43 6
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getFields()
49
    {
50
        return $this->fields;
51
    }
52
53
    /**
54
     * @param array $data
55
     * @return $this
56
     * @throws Exception
57
     */
58 1
    public function post(array $data)
59
    {
60 1
        $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__), $data);
61
62
        $this->set($response['data'], $response['status']);
63
64
        return $this;
65
    }
66
67
    /**
68
     * Set data to model
69
     *
70
     * @param  array $data
71
     * @param  null $status
72
     * @return $this
73
     * @throws Exception
74
     */
75 3
    public function set(array $data, $status = null)
76
    {
77 3
        if (empty($data['id']) && empty($this->getId())) {
78 2
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
79
        }
80
81 1
        if (!empty($data['id']) && !empty($this->getId()) && $data['id'] != $this->getId()) {
82
            throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED);
83
        }
84
85 1
        if (!empty($data['id'])) {
86 1
            $this->setId($data['id']);
87
        }
88
89 1
        $this->data = array_merge((array)$this->data, $data);
90
91 1
        if ($status) {
92
            $this->lastStatus = $status;
93
        }
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * @param array $data
100
     * @return $this
101
     * @throws Exception
102
     */
103
    public function put(array $data)
104
    {
105
        $this->set($data);
106
107
        $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $data);
108
109
        $this->set($response['data'], $response['status']);
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return null
116
     */
117 5
    public function getId()
118
    {
119 5
        return $this->id;
120
    }
121
122
    /**
123
     * @param $id
124
     * @return Model
125
     * @throws Exception
126
     */
127 1
    public function setId($id)
128
    {
129 1
        return $this->id = $id;
130
    }
131
132
    /**
133
     * @param null $id
134
     * @return $this
135
     * @throws Exception
136
     */
137
    public function delete($id = null)
138
    {
139
        if (empty($this->getId())) {
140
            $this->set(['id' => $id]);
141
        }
142
143
        $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]));
144
145
        $this->set($response['data'], $response['status']);
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param $name
152
     * @return mixed
153
     * @throws Exception
154
     */
155
    public function __get($name)
156
    {
157
        $data = $this->get();
158
159
        if (!array_key_exists($name, $data)) {
160
            throw new Exception('Поле ' . $name . ' не указано при создвнии объекта модели ' . get_class($this) . ' (см. 2-й аргумент)');
161
        }
162
163
        return $data[$name];
164
    }
165
166
    /**
167
     * @param null $id
168
     * @return null
169
     * @throws Exception
170
     */
171 2
    public function get($id = null)
172
    {
173 2
        if ($id === null && $this->getId() !== null) {
174
            return $this->data;
175
        }
176
177 2
        if (!$id) {
178 2
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
179
        }
180
181
        $this->setId($id);
182
183
        $params = $this->fields ? ['fields' => implode(',', $this->fields)] : [];
184
185
        $response = $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $params);
186
187
        $this->set($response['data'], $response['status']);
188
189
        return $this->data;
190
    }
191
192
    /**
193
     * @return Client
194
     */
195 1
    protected function getClient()
196
    {
197 1
        return $this->client;
198
    }
199
}