GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#8)
by
unknown
02:40
created

BaseResource::replace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace BoletoSimples;
4
5
use GuzzleHttp\Client;
6
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber;
7
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 = []) {
33 20
    $this->_attributes = $attributes;
34 20
    self::configure();
35 20
  }
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) {
50 8
    $this->_attributes[$k] = $v;
51 8
  }
52
53 4
  public function attributes() {
54 4
    return $this->_attributes;
55
  }
56
57 16
  public function isNew() {
58 16
    return !isset($this->_attributes['id']) || $this->_attributes['id'] == null;
59
  }
60
61 1
  public function isPersisted() {
62 1
    return !$this->isNew();
63
  }
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($action = null)
75 10
    {
76 10
        if (!isset($action)) {
77
            $action = $this->isNew() ? 'create' : 'update';
78
        }
79 1
80
        return $this->_request($action);
81
    }
82
83
  public function parseResponse($response) {
84
    $status = $response->getStatusCode();
85
    if ($status >= 200 && $status <= 299) {
86
      if($response->json()) {
87
        $this->_attributes = $response->json();
88 1
      }
89
      return true;
90 1
    } else {
91
      if (isset($response->json()['errors'])) {
92
        $this->response_errors = $response->json()['errors'];
93
      }
94 14
      return false;
95 14
    }
96 14
  }
97 14
98 14
  private function _request($action) {
99 14
    $class = get_called_class();
100 10
    $method = self::methodFor($action);
101 10
    $path = $this->path();
102 10
    $options = [];
103
    if (in_array($action, ['create', 'update'])) {
104 14
      $attributes = [$class::element_name() => $this->_attributes];
105
      $options = ['json' => $attributes];
106
    }
107
108 15
    $response = self::sendRequest($method, $path, $options);
0 ignored issues
show
Bug introduced by
The method sendRequest() does not exist on BoletoSimples\BaseResource. Did you maybe mean _sendRequest()?

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.

Loading history...
109
    return $this->parseResponse($response);
110 15
  }
111 15
112 15
  public static function methodFor($action) {
113 15
    $methods = array(
114
      'create' => 'POST',
115 15
      'update' => 'PUT',
116 15
      'find' => 'GET',
117
      'destroy' => 'DELETE',
118
      'new' => 'GET',
119 9
      'patch' => 'PATCH'
120 9
    );
121 5
    return $methods[$action];
122
  }
123 4
124 4
  private static function _find($id) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
125 4
    if (!$id) {
126
      throw new \Exception("Couldn't find ".get_called_class()." without an ID.");
127
    }
128
    $class = get_called_class();
129 6
    $object = new $class(['id' => $id]);
130 6
    $object->_request('find');
131 6
    return $object;
132 6
  }
133
134
  private static function _create($attributes = array()) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
135
    $class = get_called_class();
136 3
    $object = new $class($attributes);
137 3
    $object->save();
138 3
    return $object;
139
  }
140
141 View Code Duplication
  public static function update(array $attributes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
  {
143
      if (!isset($attributes['id'])) {
144
          throw new \InvalidArgumentException('Id is required for this action');
145
      }
146 18
147 18
      $class = get_called_class();
148 18
      $object = new $class($attributes);
149 18
      $object->save('patch');
150
      return $obj;
0 ignored issues
show
Bug introduced by
The variable $obj does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
151
  }
152
153 1 View Code Duplication
  public static function replace(array $attributes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
  {
155
      if (!isset($attributes['id'])) {
156
          throw new \InvalidArgumentException('Id is required for this action');
157 21
      }
158 21
159
      $class = get_called_class();
160
      $object = new $class($attributes);
161 21
      $object->save('update');
162 21
      return $obj;
0 ignored issues
show
Bug introduced by
The variable $obj does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
163
  }
164
165 23
  private static function _all($params = array()) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
166 23
    $class = get_called_class();
167 23
    $response = self::sendRequest('GET', $class::element_name_plural(), ['query' => $params]);
0 ignored issues
show
Bug introduced by
The method sendRequest() does not exist on BoletoSimples\BaseResource. Did you maybe mean _sendRequest()?

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.

Loading history...
168
    $collection = [];
169
    foreach ($response->json() as $attributes) {
170
      $collection[] = new $class($attributes);
171
    }
172
    return $collection;
173 29
  }
174 29
175
  private static function _sendRequest($method, $path, $options = []) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
176 29
    $options = array_merge(self::$default_options, $options);
177 29
    $request = self::$client->createRequest($method, $path, $options);
178
    $response = self::$client->send($request);
179 29
    \BoletoSimples::$last_request = new LastRequest($request, $response);
180 29
    if ($response->getStatusCode() >= 400 && $response->getStatusCode() <= 599) {
181
      new ResponseError($response);
182
    }
183 29
    return $response;
184 29
  }
185 29
186 29
  public static function element_name() {
187 29
    return Util::underscorize(get_called_class());
188
  }
189 29
190
  public static function element_name_plural() {
191 29
    return Util::pluralize(self::element_name());
192 29
  }
193
194
  public static function __callStatic($name, $arguments) {
195
    self::configure();
196
    return call_user_func_array("self::_".$name, $arguments);
197
  }
198
199
  /**
200
   * Configure the GuzzleHttp\Client with default options.
201
   */
202
  public static function configure() {
203
    $config = \BoletoSimples::$configuration;
204
205
    $oauth2 = new Oauth2Subscriber();
206
    $oauth2->setAccessToken($config->access_token);
207
208
    self::$client = new Client([
209
      'base_url' => $config->baseUri(),
210
      'defaults' => [
211
        'headers' => [
212
          'User-Agent' => $config->userAgent()
213
        ],
214
        'auth' => 'oauth2',
215
        'subscribers' => [$oauth2]
216
      ],
217
      'verify' => false
218
    ]);
219
220
    self::$default_options = ['headers' => ['Content-Type'=> 'application/json'], 'exceptions' => false];
221
  }
222
223
}
224