Completed
Push — master ( 919482...059413 )
by Pol
11:04
created

MethodPluginBase::getDefaultParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 24
  public function getMethod() {
39 24
    return $this::METHOD;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45 24
  public function setParameters(array $parameters = array()) {
46 24
    $this->parameters = $parameters;
47 24
  }
48
49
  /**
50
   * {@inheritdoc}
51
   */
52 12
  public function getDefaultParameters() {
53 12
    return [];
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59 24
  public function getParameters() {
60 24
    $defaultParameters = $this->getDefaultParameters();
61
62 24
    $params = [];
63 24
    foreach ($this->getDefaultParameters() as $key => $parameter) {
64 24
      if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) {
65 24
        if (!is_null($parameter['value'])) {
66 24
          $params[$key] = $parameter['value'];
67
        }
68
      }
69
    }
70
71 24
    foreach ((array) $this->parameters as $key => $value) {
72 24
      if (array_key_exists($key, $defaultParameters)) {
73 24
        if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) {
74 24
          $params[$key] = $value;
75
        }
76
      }
77
    }
78
79 24
    $this->setParameters($params);
80 24
81 24
    return array_filter([
82 24
      'jsonrpc' => '2.0',
83 24
      'id'      => $this->getRandomId(),
84
      'method'  => $this->getMethod(),
85
      'params'  => $params,
86
    ]);
87
  }
88
89
  /**
90 12
   * Get a random ID.
91 12
   *
92 12
   * @return int
93
   */
94
  public function getRandomId() {
95
    return mt_rand(1, 10^6);
96
  }
97 24
98 24
  /**
99
   * {@inheritdoc}
100
   */
101
  public function setApiVersion($apiVersion) {
102
    $this->apiVersion = $apiVersion;
103
  }
104 12
105 12
  /**
106 12
   * {@inheritdoc}
107
   */
108
  public function getApiVersion() {
109
    return $this->apiVersion;
110
  }
111 12
112 12
  /**
113
   * {@inheritdoc}
114
   */
115
  public function getResult(ResponseInterface $response) {
116
    $body = json_decode($response->getBody(), TRUE);
117
    $response->getBody()->rewind();
118 13
    return $body['result'];
119 13
  }
120 13
121
}
122