Completed
Pull Request — master (#3)
by thomas
17:25 queued 15:04
created

RequestFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 2
B response() 0 20 7
1
<?php
2
3
namespace BitWasp\Stratum\Request;
4
5
use BitWasp\Stratum\Exceptions\ApiError;
6
7
class RequestFactory
8
{
9
    /**
10
     * @var array
11
     */
12
    private $nonces = [];
13
14
    /**
15
     * @param $method
16
     * @param array $params
17
     * @param bool $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
18
     * @return Request
19
     */
20 3
    public function create($method, $params = array())
21
    {
22
        do {
23 3
            $id = mt_rand(0, PHP_INT_MAX);
24 3
        } while (in_array($id, $this->nonces));
25
26 3
        return new Request($id, $method, $params);
27
    }
28
29
    /**
30
     * @param $string
31
     * @return Response|Request
32
     * @throws \Exception
33
     */
34 20
    public function response($string)
35
    {
36 20
        $decoded = json_decode(trim($string), true);
37
38 20
        if (json_last_error() === JSON_ERROR_NONE) {
39 19
            $id = isset($decoded['id']) ? $decoded['id'] : null;
40
41 19
            if (isset($decoded['error'])) {
42 1
                throw new ApiError($id, $decoded['error']);
43 18
            } elseif (isset($decoded['method']) && isset($decoded['params'])) {
44 14
                return new Request($id, $decoded['method'], $decoded['params']);
45 5
            } elseif (isset($decoded['result'])) {
46 4
                return new Response($id, $decoded['result']);
47
            }
48
49 1
            throw new \Exception('Response missing error/params/result');
50
        }
51
52 1
        throw new \Exception('Invalid JSON');
53
    }
54
}
55