Completed
Push — master ( 3aaf31...f42553 )
by Lawrence
01:34
created

Client::decodeResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
/*
3
 +------------------------------------------------------------------------+
4
 | Plinker-RPC PHP                                                        |
5
 +------------------------------------------------------------------------+
6
 | Copyright (c)2017-2018 (https://github.com/plinker-rpc/core)           |
7
 +------------------------------------------------------------------------+
8
 | This source file is subject to MIT License                             |
9
 | that is bundled with this package in the file LICENSE.                 |
10
 |                                                                        |
11
 | If you did not receive a copy of the license and are unable to         |
12
 | obtain it through the world-wide-web, please send an email             |
13
 | to [email protected] so we can send you a copy immediately.        |
14
 +------------------------------------------------------------------------+
15
 | Authors: Lawrence Cherone <[email protected]>                     |
16
 +------------------------------------------------------------------------+
17
 */
18
19
namespace Plinker\Core;
20
21
/**
22
 * Plinker\Core\Client
23
 */
24
final class Client
25
{
26
    /**
27
     * @var
28
     */
29
    private $component;
30
31
    /**
32
     * @var
33
     */
34
    private $response;
35
36
    /**
37
     * @var
38
     */
39
    private $config;
40
41
    /**
42
     * @var
43
     */
44
    private $curl;
45
46
    /**
47
     * @var
48
     */
49
    private $signer;
50
51
    /**
52
     * Class construct
53
     *
54
     * @param  string $server  - server enpoint url
55
     * @param  array  $config  - config array which holds object configuration
56
     * @return void
57
     */
58
    public function __construct($server, array $config = [])
59
    {
60
        $this->config = array_merge([
61
            "server" => $server,
62
            "secret" => null
63
        ], $config);
64
65
        // check and set client timeout
66
        if (!isset($this->config["timeout"]) || !is_numeric($this->config["timeout"])) {
67
            $this->config["timeout"] = 10;
68
        }
69
    }
70
71
    /**
72
     * Magic getter method, which sets component
0 ignored issues
show
Bug introduced by
The type Plinker\Core\method was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
     *
74
     * @param  string method  - component name
75
     * @return <Plinker\Client>
76
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment <Plinker\Client> at position 0 could not be parsed: Unknown type name '<' at position 0 in <Plinker\Client>.
Loading history...
77
    public function __get($method)
78
    {
79
        $this->component = $method;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Magic caller method, which calls component
86
     *
87
     * @param string action
0 ignored issues
show
Bug introduced by
The type Plinker\Core\action was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
     * @param array  params
0 ignored issues
show
Bug introduced by
The type Plinker\Core\params was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
     */
90
    public function __call($action, array $params)
91
    {
92
        if (!is_scalar($action)) {
93
            throw new \Exception("Method name has no scalar value");
94
        }
95
96
        if (!is_array($params)) {
0 ignored issues
show
introduced by
The condition ! is_array($params) can never be true.
Loading history...
97
            throw new \Exception("Params must be given as array");
98
        }
99
100
        // load curl
101
        if (!$this->curl) {
102
            $this->curl = new Lib\Curl($this->config);
103
        }
104
105
        // load signer
106
        if (!$this->signer) {
107
            $this->signer = new Lib\Signer($this->config);
108
        }
109
110
        // change params array into numeric
111
        $params = array_values($params);
112
113
        // unset local private key
114
        unset($this->config["plinker"]["private_key"]);
115
116
        $payload = $this->signer->encode([
117
            "component" => $this->component,
118
            "config" => $this->config,
119
            "action" => $action,
120
            "params" => $params
121
        ]);
122
123
        $this->response = $this->curl->post($this->config["server"], $payload, [
124
            "PLINKER: ".$payload["token"]
125
        ]);
126
127
        // unserialize data
128
        return unserialize($this->response);
129
    }
130
}
131