Passed
Push — master ( 40b0ee...7fc7d6 )
by Lawrence
01:28
created

Client   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 89.66%

Importance

Changes 21
Bugs 3 Features 1
Metric Value
dl 0
loc 109
ccs 26
cts 29
cp 0.8966
rs 10
c 21
b 3
f 1
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 5 1
B __call() 0 47 6
A __construct() 0 10 3
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 $config;
35
36
    /**
37
     * @var
38
     */
39
    private $curl;
40
41
    /**
42
     * @var
43
     */
44
    private $signer;
45
46
    /**
47
     * Class construct
48
     *
49
     * @param  string $server  - server enpoint url
50
     * @param  array  $config  - config array which holds object configuration
51
     * @return void
52
     */
53 3
    public function __construct($server, array $config = [])
54
    {
55 3
        $this->config = array_merge([
56 3
            "server" => $server,
57
            "secret" => null
58 3
        ], $config);
59
60
        // check and set client timeout
61 3
        if (!isset($this->config["timeout"]) || !is_numeric($this->config["timeout"])) {
62 3
            $this->config["timeout"] = 10;
63
        }
64 3
    }
65
66
    /**
67
     * Magic getter method, which sets component
68
     *
69
     * @param  string $component
70
     * @return object
71
     */
72 1
    public function __get($component)
73
    {
74 1
        $this->component = $component;
75
76 1
        return $this;
77
    }
78
79
    /**
80
     * Magic caller method, which calls component
81
     *
82
     * @param string $action
83
     * @param array  $params
84
     * @return array
85
     */
86 1
    public function __call($action, $params)
87
    {
88
        // load curl
89 1
        if (!$this->curl) {
90 1
            $this->curl = new Lib\Curl($this->config);
91
        }
92
93
        // load signer
94 1
        if (!$this->signer) {
95 1
            $this->signer = new Lib\Signer($this->config);
96
        }
97
98
        // change params array into numeric
99 1
        $params = array_values($params);
100
101
        // unset local private key
102 1
        unset($this->config["plinker"]["private_key"]);
103
104
        // encode payload
105 1
        $payload = $this->signer->encode([
106 1
            "component" => $this->component,
107 1
            "config" => $this->config,
108 1
            "action" => $action,
109 1
            "params" => $params
110
        ]);
111
112
        // post request to plinker server
113 1
        $response = $this->curl->post($this->config["server"], $payload, [
114 1
            "PLINKER: ".$payload["token"]
115
        ]);
116
117
        // json decode (unpack) response body
118 1
        if (empty($response['body']) || !($response['body'] = json_decode($response['body'], true))) {
119 1
            return $response;
120
        }
121
122
        // verify and decode response
123
        if (!($response['body'] = $this->signer->decode($response['body']))) {
124
            return [
125
                'body' => null,
126
                "code" => 422,
127
                "error" => 'Failed to decode payload, check secret'
128
            ];
129
        }
130
131
        //
132
        return $response['body'];
133
    }
134
}
135