Total Complexity | 4 |
Total Lines | 62 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
7 | trait Creatable |
||
8 | { |
||
9 | /** |
||
10 | * Abstract functions to imppose requirements for the exhibiting class. |
||
11 | */ |
||
12 | abstract public function getResourceName($object = null); |
||
13 | |||
14 | abstract public function getAttributes($sanitized = true); |
||
15 | |||
16 | abstract public function processPath($path = null, array $values = null); |
||
17 | |||
18 | abstract public function postRequest($path, array $options = []); |
||
19 | |||
20 | abstract public function getClient(); |
||
21 | |||
22 | abstract public function setValues($values); |
||
23 | |||
24 | abstract public function setAttributes(array $values); |
||
25 | |||
26 | protected $createPath; |
||
27 | |||
28 | /** |
||
29 | * Post a resource and update this parameters with the response from the API. |
||
30 | * |
||
31 | * @throws ResourceNotCreated |
||
32 | * |
||
33 | * @return $this |
||
34 | */ |
||
35 | public function create() |
||
36 | { |
||
37 | $path = $this->getCreatePath() ?? $this->getResourceName(); |
||
38 | |||
39 | $response = $this->postRequest($path); |
||
40 | $this->setAttributes([]); |
||
41 | $this->setValues($response); |
||
42 | if ($this->getClient()->getLastResponse()->getStatusCode() !== 201) { |
||
43 | throw new ResourceNotCreated($this->getClient()->getLastResponse()->getBody()); |
||
44 | } |
||
45 | |||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the create path of the current object. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getCreatePath() |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Set the save path of the current object. |
||
61 | * |
||
62 | * @param $path |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | public function setCreatePath($path) |
||
69 | } |
||
70 | } |
||
71 |