1 | <?php |
||
8 | class BaseResource { |
||
9 | /** |
||
10 | * The GuzzleHttp\Client object |
||
11 | */ |
||
12 | public static $client = null; |
||
13 | |||
14 | /** |
||
15 | * Default options used on Guzzle requests |
||
16 | */ |
||
17 | public static $default_options = null; |
||
18 | |||
19 | /** |
||
20 | * The attributes of the current object, accessed via the anonymous get/set methods. |
||
21 | */ |
||
22 | private $_attributes = array(); |
||
23 | |||
24 | /** |
||
25 | * Array with all errors returned from last request. |
||
26 | */ |
||
27 | public $response_errors = array(); |
||
28 | |||
29 | /** |
||
30 | * Constructor method. |
||
31 | */ |
||
32 | 20 | public function __construct($attributes = []) { |
|
36 | |||
37 | /** |
||
38 | * Getter for internal object data. |
||
39 | */ |
||
40 | 4 | public function __get($k) { |
|
41 | 4 | if (isset ($this->_attributes[$k])) { |
|
42 | 4 | return $this->_attributes[$k]; |
|
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Setter for internal object data. |
||
48 | */ |
||
49 | 8 | public function __set($k, $v) { |
|
52 | |||
53 | 4 | public function attributes() { |
|
56 | |||
57 | 16 | public function isNew() { |
|
60 | |||
61 | 1 | public function isPersisted() { |
|
64 | |||
65 | 14 | public function path($action = null) { |
|
66 | 14 | $class = get_called_class(); |
|
67 | 14 | $path = $this->isNew() ? $class::element_name_plural() : $class::element_name_plural()."/".$this->_attributes['id']; |
|
68 | 14 | if ($action) { |
|
69 | $path .= '/'.$action; |
||
70 | } |
||
71 | 14 | return $path; |
|
72 | } |
||
73 | |||
74 | 10 | public function save() { |
|
78 | |||
79 | 1 | public function parseResponse($response) { |
|
80 | $status = $response->getStatusCode(); |
||
81 | if ($status >= 200 && $status <= 299) { |
||
82 | 1 | if($response->json()) { |
|
83 | $this->_attributes = $response->json(); |
||
84 | } |
||
85 | return true; |
||
86 | } else { |
||
87 | if (isset($response->json()['errors'])) { |
||
88 | $this->response_errors = $response->json()['errors']; |
||
89 | } |
||
90 | return false; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | 14 | private function _request($action) { |
|
95 | 14 | $class = get_called_class(); |
|
96 | 14 | $method = self::methodFor($action); |
|
97 | 14 | $path = $this->path(); |
|
98 | 14 | $options = []; |
|
99 | 14 | if ($action == 'create' || $action == 'update') { |
|
100 | 10 | $attributes = [$class::element_name() => $this->_attributes]; |
|
101 | 10 | $options = ['json' => $attributes]; |
|
102 | 10 | } |
|
103 | |||
104 | 14 | $response = self::sendRequest($method, $path, $options); |
|
|
|||
105 | return $this->parseResponse($response); |
||
106 | } |
||
107 | |||
108 | 15 | public static function methodFor($action) { |
|
109 | $methods = array( |
||
110 | 15 | 'create' => 'POST', |
|
111 | 15 | 'update' => 'PUT', |
|
112 | 15 | 'find' => 'GET', |
|
113 | 15 | 'destroy' => 'DELETE', |
|
114 | 'new' => 'GET' |
||
115 | 15 | ); |
|
116 | 15 | return $methods[$action]; |
|
117 | } |
||
118 | |||
119 | 9 | private static function _find($id) { |
|
120 | 9 | if (!$id) { |
|
121 | 5 | throw new \Exception("Couldn't find ".get_called_class()." without an ID."); |
|
122 | } |
||
123 | 4 | $class = get_called_class(); |
|
124 | 4 | $object = new $class(['id' => $id]); |
|
125 | 4 | $object->_request('find'); |
|
126 | return $object; |
||
127 | } |
||
128 | |||
129 | 6 | private static function _create($attributes = array()) { |
|
130 | 6 | $class = get_called_class(); |
|
131 | 6 | $object = new $class($attributes); |
|
132 | 6 | $object->save(); |
|
133 | return $object; |
||
134 | } |
||
135 | |||
136 | 3 | private static function _all($params = array()) { |
|
137 | 3 | $class = get_called_class(); |
|
138 | 3 | $response = self::sendRequest('GET', $class::element_name_plural(), ['query' => $params]); |
|
139 | $collection = []; |
||
140 | foreach ($response->json() as $attributes) { |
||
141 | $collection[] = new $class($attributes); |
||
142 | } |
||
143 | return $collection; |
||
144 | } |
||
145 | |||
146 | 18 | private static function _sendRequest($method, $path, $options = []) { |
|
147 | 18 | $options = array_merge(self::$default_options, $options); |
|
148 | 18 | $request = self::$client->createRequest($method, $path, $options); |
|
149 | 18 | $response = self::$client->send($request); |
|
150 | \BoletoSimples::$last_request = new LastRequest($request, $response); |
||
151 | if ($response->getStatusCode() >= 400 && $response->getStatusCode() <= 599) { |
||
152 | new ResponseError($response); |
||
153 | } |
||
154 | return $response; |
||
155 | } |
||
156 | |||
157 | 21 | public static function element_name() { |
|
160 | |||
161 | 21 | public static function element_name_plural() { |
|
164 | |||
165 | 23 | public static function __callStatic($name, $arguments) { |
|
169 | |||
170 | /** |
||
171 | * Configure the GuzzleHttp\Client with default options. |
||
172 | */ |
||
173 | 29 | public static function configure() { |
|
174 | 29 | $config = \BoletoSimples::$configuration; |
|
175 | |||
176 | 29 | $oauth2 = new Oauth2Subscriber(); |
|
177 | 29 | $oauth2->setAccessToken($config->access_token); |
|
178 | |||
193 | |||
194 | } |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.