Completed
Push — master ( 25c087...a46420 )
by Lawrence
01:31
created

Client::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 1
b 0
f 0
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 2
    public function __construct($server, array $config = [])
54
    {
55 2
        $this->config = array_merge([
56 2
            "server" => $server,
57
            "secret" => null
58 2
        ], $config);
59
60
        // check and set client timeout
61 2
        if (!isset($this->config["timeout"]) || !is_numeric($this->config["timeout"])) {
62 2
            $this->config["timeout"] = 10;
63
        }
64 2
    }
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
    public function __call($action, $params)
87
    {
88
        // load curl
89
        if (!$this->curl) {
90
            $this->curl = new Lib\Curl($this->config);
91
        }
92
93
        // load signer
94
        if (!$this->signer) {
95
            $this->signer = new Lib\Signer($this->config);
96
        }
97
98
        // change params array into numeric
99
        $params = array_values($params);
100
101
        // unset local private key
102
        unset($this->config["plinker"]["private_key"]);
103
104
        // encode payload
105
        $payload = $this->signer->encode([
106
            "component" => $this->component,
107
            "config" => $this->config,
108
            "action" => $action,
109
            "params" => $params
110
        ]);
111
112
        // post request to plinker server
113
        $response = $this->curl->post($this->config["server"], $payload, [
114
            "PLINKER: ".$payload["token"]
115
        ]);
116
        
117
        print_r($response);
118
119
        // json decode (unpack) response body
120
        if (empty($response['body']) || !($response['body'] = json_decode($response['body'], true))) {
121
            return $response;
122
        }
123
124
        // verify and decode response
125
        if (!($response['body'] = $this->signer->decode($response['body']))) {
126
            return [
127
                'body' => null,
128
                "code" => 422,
129
                "error" => 'Failed to decode payload, check secret'
130
            ];
131
        }
132
133
        //
134
        return $response['body'];
135
    }
136
}
137