Completed
Push — master ( aed37d...33193a )
by Denis
06:52
created

Model::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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