Passed
Push — master ( a03e81...128513 )
by Lawrence
01:39
created

Client   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 21
Bugs 3 Features 1
Metric Value
dl 0
loc 114
ccs 29
cts 32
cp 0.9063
rs 10
c 21
b 3
f 1
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 5 1
A __construct() 0 10 3
B __call() 0 52 6
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([
91 1
                'server' => $this->config['server'],
92 1
                'timeout' => $this->config['timeout']
93
            ]);
94
        }
95
96
        // load signer
97 1
        if (!$this->signer) {
98 1
            $this->signer = new Lib\Signer([
99 1
                'secret' => $this->config['secret']
100
            ]);
101
        }
102
103
        // change params array into numeric
104 1
        $params = array_values($params);
105
106
        // unset local private key
107 1
        unset($this->config["plinker"]["private_key"]);
108
109
        // encode payload
110 1
        $payload = $this->signer->encode([
111 1
            "component" => $this->component,
112 1
            "config" => $this->config,
113 1
            "action" => $action,
114 1
            "params" => $params
115
        ]);
116
117
        // post request to plinker server
118 1
        $response = $this->curl->post($this->config["server"], $payload, [
119 1
            "PLINKER: ".$payload["token"]
120
        ]);
121
122
        // json decode (unpack) response body
123 1
        if (empty($response['body']) || !($response['body'] = json_decode($response['body'], true))) {
124 1
            return $response;
125
        }
126
127
        // verify and decode response
128
        if (!($response['body'] = $this->signer->decode($response['body']))) {
129
            return [
130
                'body' => null,
131
                "code" => 422,
132
                "error" => 'Failed to decode payload, check secret'
133
            ];
134
        }
135
136
        //
137
        return $response['body'];
138
    }
139
}
140