Completed
Push — master ( eb5562...b637ab )
by Pol
02:27
created

src/Plugin/MethodPluginBase.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace drupol\Yaroc\Plugin;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
abstract class MethodPluginBase implements MethodPluginInterface {
8
9
  /**
10
   * The method name.
11
   */
12
  const METHOD = 'BASE';
13
14
  /**
15
   * The parameters.
16
   *
17
   * @var array
18
   */
19
  protected $parameters;
20
21
  /**
22
   * The API Key.
23
   *
24
   * @var string
25
   */
26
  protected $apiKey;
27
28
  /**
29
   * The API Version.
30
   *
31
   * @var int
32
   */
33
  protected $apiVersion;
34
35
  /**
36
   * {@inheritdoc}
37
   */
38 25
  public function getMethod() {
39 25
    return $this::METHOD;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45 25
  public function setParameters(array $parameters = array()) {
46 25
    $this->parameters = $parameters;
47
48 25
    return $this;
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54 13
  public function getDefaultParameters() {
55 13
    return [];
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61 25
  public function getParameters() {
62 25
    $defaultParameters = $this->getDefaultParameters();
63
64 25
    $params = [];
65 25
    foreach ($this->getDefaultParameters() as $key => $parameter) {
66 25
      if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) {
67 25
        if (!is_null($parameter['value'])) {
68 4
          $params[$key] = $parameter['value'];
69 4
        }
70 25
      }
71 25
    }
72
73 25
    foreach ((array) $this->parameters as $key => $value) {
74 25
      if (array_key_exists($key, $defaultParameters)) {
75 25
        if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) {
76 25
          $params[$key] = $value;
77 25
        }
78 25
      }
79 25
    }
80
81 25
    $this->setParameters($params);
82
83 25
    return array_filter([
84 25
      'jsonrpc' => '2.0',
85 25
      'id'      => $this->getRandomId(),
86 25
      'method'  => $this->getMethod(),
87 25
      'params'  => $params,
88 25
    ]);
89
  }
90
91
  /**
92
   * Get a random ID.
93
   *
94
   * @return string
95
   */
96 13
  public function getRandomId() {
97 13
    return $this->getMethod() . '_' . mt_rand(1, mt_getrandmax()) . '_' . str_replace('.', '', microtime(TRUE));
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103 13
  public function setApiVersion($apiVersion) {
104 13
    $this->apiVersion = $apiVersion;
105
106 13
    return $this;
107
  }
108
109
  /**
110
   * {@inheritdoc}
111
   */
112 13
  public function getApiVersion() {
113 13
    return $this->apiVersion;
114
  }
115
116
  /**
117
   * {@inheritdoc}
118
   */
119 13
  public function get(ResponseInterface $response, $key = NULL) {
120 13
    if ($content = $this->getContent($response)) {
121 13
      if (is_array($content)) {
122 13
        if ($key && isset($content[$key])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
123 13
          return $content[$key];
124
        }
125 12
        if (is_null($key)) {
126 12
          return $content;
127
        }
128 12
      }
129 12
    }
130
131 12
    return FALSE;
132
  }
133
134
  /**
135
   * {@inheritdoc}
136
   */
137 13
  public function getContent(ResponseInterface $response) {
138 13
    $body = $response->getBody()->__toString();
139 13
    if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
140 13
      $content = json_decode($body, true);
141 13
      if (JSON_ERROR_NONE === json_last_error()) {
142 13
        return $content;
143
      }
144
    }
145
146
    return FALSE;
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152
  public function getHeader(ResponseInterface $response, $name) {
153
    $headers = $response->getHeader($name);
154
    return array_shift($headers);
155
  }
156
157
}
158