Completed
Push — master ( 7fc194...161aeb )
by Denis
01:53
created

Model::set()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 4
nop 2
crap 7.0178
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
     * @throws Exception
38
     */
39 8
    public function __construct(Client $client, array $fields)
40
    {
41 8
        if (!$client) {
42
            throw new Exception('Client not defined');
43
        }
44
45 8
        if (!is_array($fields)) {
46
            throw new Exception('Fields for model of collection mast be array');
47
        }
48
49 8
        $this->client = $client;
50 8
        $this->fields = $fields;
51 8
    }
52
53 2
    public function setId($id) {
54 2
        return $this->set(['id' => $id]);
55
    }
56
57 2
    public function get($id = null)
58
    {
59 2
        if ($id === null && $this->id !== null) {
60 1
            return $this->data;
61
        }
62
63 2
        $this->setId($id);
64
65 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__, [$this->getId()]), ['fields' => implode(',', $this->getFields())]);
66
67 1
        $this->set($response['data'], $response['status']);
68
69 1
        return $this->data;
70
    }
71
72
    /**
73
     * Set data to model
74
     *
75
     * @param  array $data
76
     * @param  null $status
77
     * @return $this
78
     * @throws Exception
79
     */
80 7
    public function set(array $data, $status = null)
81
    {
82 7
        if (empty($data['id']) && empty($this->id)) {
83 4
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
84
        }
85
86 4
        if (!empty($data['id']) && !empty($this->id) && $data['id'] != $this->id) {
87
            throw new Exception(Model::MESSAGE_ID_CAN_NOT_CHANGED);
88
        }
89
90 4
        $this->data = array_merge(
91 4
            (array)$this->data,
92 4
            array_intersect_key($data, array_flip(array_merge(['id'], $this->getFields())))
93 4
        );
94
95 4
        $this->id = $this->data['id'];
96
97 4
        if ($status) {
98 1
            $this->lastStatus = $status;
99 1
        }
100
101 4
        return $this;
102
    }
103
104 4
    public function getFields()
105
    {
106 4
        $class = get_class($this);
107
108 4
        return $this->fields ? $this->fields : $class::$defaultFields;
109
    }
110
111 4
    public function getId()
112
    {
113 4
        return $this->id;
114
    }
115
116 1
    public function post(array $data)
117
    {
118 1
        $response = $this->client->getResponse($this->getUrl(__FUNCTION__), $data);
119
120
        $this->set($response['data'], $response['status']);
121
122
        return $this;
123
    }
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
144
        $this->set($response['data'], $response['status']);
145
146
        return $this;
147
    }
148
149 1
    public function __get($name)
150
    {
151 1
        $data = $this->get();
152
153 1
        if (!array_key_exists($name, $data)) {
154
            throw new Exception('Param ' . $name . ' not defined for ' . get_class($this));
155
        }
156
157 1
        return $data[$name];
158
    }
159
}