Completed
Push — master ( f22d83...d711c3 )
by Denis
02:01
created

Model::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 setId($id) {
52 4
        return $this->set(['id' => $id]);
53
    }
54
55 5
    public function get($id = null)
56
    {
57 5
        if ($id === null && $this->id !== null) {
58 4
            return $this->data;
59
        }
60
61 3
        $this->setId($id);
62
63 3
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), ['fields' => implode(',', $this->getFields())]);
64
65 2
        $this->set($response['data'], $response['status']);
66
67 2
        return $this->data;
68
    }
69
70
    /**
71
     * Set data to model
72
     *
73
     * @param  array $data
74
     * @param  null $status
75
     * @return $this
76
     * @throws Exception
77
     */
78 10
    public function set(array $data, $status = null)
79
    {
80 10
        if (empty($data['id']) && empty($this->id)) {
81 4
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
82
        }
83
84 8
        if (!empty($data['id']) && !empty($this->id) && $data['id'] != $this->id) {
85
            throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED);
86
        }
87
88 8
        $this->data = array_merge(
89 8
            (array)$this->data,
90 8
            array_intersect_key($data, array_flip(array_merge(['id'], $this->getFields())))
91 8
        );
92
93 8
        $this->id = $this->data['id'];
94
95 8
        if ($status) {
96 5
            $this->lastStatus = $status;
97 5
        }
98
99 8
        return $this;
100
    }
101
102 8
    public function getFields()
103
    {
104 8
        $class = get_class($this);
105
106 8
        return $this->fields ? $this->fields : $class::$defaultFields;
107
    }
108
109 8
    public function getId()
110
    {
111 8
        return $this->id;
112
    }
113
114 1
    public function post(array $data)
115
    {
116 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__), $data);
117
118 1
        $this->set($response['data'], $response['status']);
119
120 1
        return $this;
121
    }
122
123 1
    public function put(array $data)
124
    {
125 1
        $this->set($data);
126
127 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), $data);
128
129 1
        $this->set($response['data'], $response['status']);
130
131 1
        return $this;
132
    }
133
134 1
    public function delete($id = null)
135
    {
136 1
        if (empty($this->id)) {
137
            $this->set(['id' => $id]);
138
        }
139
140 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]));
141
142 1
        $this->set($response['data'], $response['status']);
143
144 1
        return $this;
145
    }
146
147 4
    public function __get($name)
148
    {
149 4
        $data = $this->get();
150
151 4
        if (!array_key_exists($name, $data)) {
152
            throw new Exception('Param ' . $name . ' not defined for ' . get_class($this));
153
        }
154
155 4
        return $data[$name];
156
    }
157
}