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 | 23 | public function __construct($attributes = []) { |
|
36 | |||
37 | /** |
||
38 | * Getter for internal object data. |
||
39 | */ |
||
40 | 5 | public function __get($k) { |
|
45 | |||
46 | /** |
||
47 | * Setter for internal object data. |
||
48 | */ |
||
49 | 9 | public function __set($k, $v) { |
|
52 | |||
53 | 8 | public function attributes() { |
|
56 | |||
57 | 18 | public function isNew() { |
|
60 | |||
61 | 7 | public function isPersisted() { |
|
64 | |||
65 | 16 | public function path($action = null) { |
|
73 | |||
74 | 11 | public function save() { |
|
78 | |||
79 | 7 | public function parseResponse($response) { |
|
80 | 7 | $status = $response->getStatusCode(); |
|
81 | 7 | if ($status >= 200 && $status <= 299) { |
|
82 | 4 | if($response->json()) { |
|
83 | 4 | $this->_attributes = $response->json(); |
|
84 | 4 | } |
|
85 | 4 | return true; |
|
86 | } else { |
||
87 | 3 | if (isset($response->json()['errors'])) { |
|
88 | 4 | $this->response_errors = $response->json()['errors']; |
|
89 | 3 | } |
|
90 | 4 | return false; |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 16 | private function _request($action) { |
|
95 | 16 | $class = get_called_class(); |
|
96 | 16 | $method = self::methodFor($action); |
|
97 | 16 | $path = $this->path(); |
|
98 | 16 | $options = []; |
|
99 | 16 | if ($action == 'create' || $action == 'update') { |
|
100 | 11 | $attributes = [$class::element_name() => $this->_attributes]; |
|
101 | 11 | $options = ['json' => $attributes]; |
|
102 | 11 | } |
|
103 | |||
104 | 16 | $response = self::sendRequest($method, $path, $options); |
|
|
|||
105 | 7 | return $this->parseResponse($response); |
|
106 | } |
||
107 | |||
108 | 17 | public static function methodFor($action) { |
|
118 | |||
119 | 9 | private static function _find($id) { |
|
120 | 9 | if (!$id) { |
|
121 | 3 | throw new \Exception("Couldn't find ".get_called_class()." without an ID."); |
|
122 | } |
||
123 | 6 | $class = get_called_class(); |
|
124 | 6 | $object = new $class(['id' => $id]); |
|
125 | 6 | $object->_request('find'); |
|
126 | 2 | 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 | 3 | return $object; |
|
134 | } |
||
135 | |||
136 | 8 | 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 | 1 | $collection = []; |
|
140 | 1 | foreach ($response->json() as $attributes) { |
|
141 | 1 | $collection[] = new $class($attributes); |
|
142 | 8 | } |
|
143 | 1 | return $collection; |
|
144 | } |
||
145 | |||
146 | 20 | private static function _sendRequest($method, $path, $options = []) { |
|
147 | 20 | $options = array_merge(self::$default_options, $options); |
|
148 | 20 | $request = self::$client->createRequest($method, $path, $options); |
|
149 | 20 | $response = self::$client->send($request); |
|
150 | 11 | \BoletoSimples::$last_request = new LastRequest($request, $response); |
|
151 | 11 | if ($response->getStatusCode() >= 400 && $response->getStatusCode() <= 599) { |
|
152 | 6 | new ResponseError($response); |
|
153 | 4 | } |
|
154 | 9 | return $response; |
|
155 | } |
||
156 | |||
157 | 23 | public static function element_name() { |
|
160 | |||
161 | 23 | 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() { |
|
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.