|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace BEdita\WebTools\Controller\Component; |
|
16
|
|
|
|
|
17
|
|
|
use BEdita\SDK\BEditaClient; |
|
18
|
|
|
use Cake\Controller\Component; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Proxy component for BEdita API. |
|
22
|
|
|
* |
|
23
|
|
|
* @method void setupTokens(array $tokens) |
|
24
|
|
|
* @method array getDefaultHeaders() |
|
25
|
|
|
* @method string getApiBaseUrl() |
|
26
|
|
|
* @method array getTokens() |
|
27
|
|
|
* @method \Psr\Http\Message\ResponseInterface|null getResponse() |
|
28
|
|
|
* @method int|null getStatusCode() |
|
29
|
|
|
* @method string|null getStatusMessage() |
|
30
|
|
|
* @method array|null getResponseBody() |
|
31
|
|
|
* @method array|null authenticate(string $username, string $password) |
|
32
|
|
|
* @method array|null get(string $path, ?array $query = null, ?array $headers = null) |
|
33
|
|
|
* @method array|null getObjects(string $type = 'objects', ?array $query = null, ?array $headers = null) |
|
34
|
|
|
* @method array|null getObject($id, string $type = 'objects', ?array $query = null, ?array $headers = null) |
|
35
|
|
|
* @method array|null getRelated($id, string $type, string $relation, ?array $query = null, ?array $headers = null) |
|
36
|
|
|
* @method array|null addRelated($id, string $type, string $relation, array $data, ?array $headers = null) |
|
37
|
|
|
* @method array|null removeRelated($id, string $type, string $relation, array $data, ?array $headers = null) |
|
38
|
|
|
* @method array|null replaceRelated($id, string $type, string $relation, array $data, ?array $headers = null) |
|
39
|
|
|
* @method array|null save(string $type, array $data, ?array $headers = null) |
|
40
|
|
|
* @method array|null deleteObject($id, string $type) |
|
41
|
|
|
* @method array|null remove($id) |
|
42
|
|
|
* @method array|null upload(string $filename, string $filepath, ?array $headers = null) |
|
43
|
|
|
* @method array|null createMediaFromStream($streamId, string $type, array $body) |
|
44
|
|
|
* @method array|null thumbs($id = null, $query = []) |
|
45
|
|
|
* @method array|null schema(string $type) |
|
46
|
|
|
* @method array|null relationData(string $name) |
|
47
|
|
|
* @method array|null restoreObject($id, string $type) |
|
48
|
|
|
* @method array|null patch(string $path, $body, ?array $headers = null) |
|
49
|
|
|
* @method array|null post(string $path, $body, ?array $headers = null) |
|
50
|
|
|
* @method array|null delete(string $path, $body = null, ?array $headers = null) |
|
51
|
|
|
* @method void refreshTokens() |
|
52
|
|
|
*/ |
|
53
|
|
|
class ApiComponent extends Component |
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* Default component configuration. |
|
57
|
|
|
* |
|
58
|
|
|
* - apiClient => the BEditaClient instance |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_defaultConfig = [ |
|
63
|
|
|
'apiClient' => null, |
|
64
|
|
|
'exceptionsEnabled' => true, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Keep last response error |
|
69
|
|
|
* |
|
70
|
|
|
* @var \Throwable |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $error = null; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return the instance of BEditaClient |
|
76
|
|
|
* |
|
77
|
|
|
* @return \BEdita\SDK\BEditaClient |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getClient(): BEditaClient |
|
80
|
|
|
{ |
|
81
|
|
|
$client = $this->getConfigOrFail('apiClient'); |
|
82
|
|
|
if (!$client instanceof BEditaClient) { |
|
83
|
|
|
throw new \InvalidArgumentException(__('Not a valid api client class')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $client; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set if the client exceptions will be thrown. |
|
91
|
|
|
* |
|
92
|
|
|
* @param bool $value The value to set |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function enableExceptions(bool $value) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->setConfig('exceptionsEnabled', $value); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Say if there was error in the last API request. |
|
104
|
|
|
* Note that error is set if `exceptionsEnabled` conf is `false` |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function hasError(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->error !== null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the last API error. |
|
115
|
|
|
* Note that error is set if `exceptionsEnabled` conf is `false` |
|
116
|
|
|
* |
|
117
|
|
|
* @return \Throwable|null |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getError(): ?\Throwable |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->error; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Proxy to BEditaClient methods. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name The method invoked |
|
128
|
|
|
* @param array $arguments The arguments for the method |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function __call($name, array $arguments) |
|
132
|
|
|
{ |
|
133
|
|
|
$response = null; |
|
134
|
|
|
$this->error = null; |
|
135
|
|
|
try { |
|
136
|
|
|
$response = call_user_func_array([$this->getClient(), $name], $arguments); |
|
137
|
|
|
} catch (\Throwable $e) { |
|
138
|
|
|
if ($this->getConfig('exceptionsEnabled') === true) { |
|
139
|
|
|
throw $e; |
|
140
|
|
|
} |
|
141
|
|
|
$this->log($e->getMessage(), 'error'); |
|
142
|
|
|
|
|
143
|
|
|
$this->error = $e; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $response; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|