Completed
Push — master ( 1392b0...257c81 )
by Denis
01:57
created

Model::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 26.07.17
6
 * Time: 12:30
7
 */
8
9
namespace Lan\Ebs\Sdk\Classes;
10
11
use Exception;
12
use Lan\Ebs\Sdk\Client;
13
use Lan\Ebs\Sdk\Common;
14
15
abstract class Model implements Common
16
{
17
    const MESSAGE_ID_REQUIRED = 'Id is required';
18
    const MESSAGE_ID_CAN_NOT_CHANGED = 'Id can not be changed';
19
20
    private $client;
21
22
    private $fields = [];
23
24
    private $data = null;
25
26
    private $id = null;
27
28
    private $lastStatus = 0;
29
30
    /**
31
     * Model constructor.
32
     *
33
     * @param  Client $client
34
     * @param  $fields
35
     * @throws Exception
36
     */
37 10
    public function __construct(Client $client, array $fields)
38
    {
39 10
        if (!$client) {
40
            throw new Exception('Client not defined');
41
        }
42
43 10
        if (!is_array($fields)) {
44
            throw new Exception('Fields for model of collection mast be array');
45
        }
46
47 10
        $this->client = $client;
48 10
        $this->fields = $fields;
49 10
    }
50
51 4
    public function get($id = null)
52
    {
53 4
        if ($id === null && $this->id !== null) {
54 3
            return $this->data;
55
        }
56
57 3
        $this->set(['id' => $id]);
58
59 2
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $this->getFields());
60
61 1
        $this->set($response['data'], $response['status']);
62
63 1
        return $this->data;
64
    }
65
66
    /**
67
     * Set data to model
68
     *
69
     * @param  array $data
70
     * @param  null $status
71
     * @return $this
72
     * @throws Exception
73
     */
74 10
    public function set(array $data, $status = null)
75
    {
76 10
        if (empty($data['id']) && empty($this->id)) {
77 4
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
78
        }
79
80 8
        if (!empty($data['id']) && !empty($this->id) && $data['id'] != $this->id) {
81
            throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED);
82
        }
83
84 8
        $this->data = array_merge(
85 8
            (array)$this->data,
86 8
            array_intersect_key($data, array_flip(array_merge($this->getFields(), ['id'])))
87
        );
88
89 8
        $this->id = $this->data['id'];
90
91 8
        if ($status) {
92 3
            $this->lastStatus = $status;
93
        }
94
95 8
        return $this;
96
    }
97
98 8
    public function getFields()
99
    {
100 8
        $class = get_class($this);
101
102 8
        return array_merge(['id'], $this->fields ? $this->fields : $class::$defaultFields);
103
    }
104
105 8
    public function getId()
106
    {
107 8
        return $this->id;
108
    }
109
110 1
    public function post(array $data)
111
    {
112 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__), $data);
113
114 1
        $this->set($response['data'], $response['status']);
115
116 1
        return $this;
117
    }
118
119 1
    public function put(array $data)
120
    {
121 1
        $this->set($data);
122
123 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $data);
124
125
        $this->set($response['data'], $response['status']);
126
127
        return $this;
128
    }
129
130 1
    public function delete($id = null)
131
    {
132 1
        if (empty($this->id)) {
133
            $this->set(['id' => $id]);
134
        }
135
136 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]));
137
138 1
        $this->set($response['data'], $response['status']);
139
140 1
        return $this;
141
    }
142
143 5
    public function __get($name)
144
    {
145 5
        if ($this->data === null || !array_key_exists($name, $this->data)) {
146
            throw new Exception('Param ' . $name . ' not defined for ' . get_class($this));
147
        }
148
149 5
        return $this->data[$name];
150
    }
151
}