Completed
Push — master ( fad20c...da5feb )
by Lawrence
01:25
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 71.74%

Importance

Changes 0
Metric Value
dl 0
loc 147
ccs 33
cts 46
cp 0.7174
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 11 2
A __construct() 0 10 3
C __call() 0 74 7
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
     * Plinker\Core\Client
22
     */
23
    final class Client
24
    {
25
        /**
26
         * @var
27
         */
28
        private $component = [];
29
30
        /**
31
         * @var
32
         */
33
        private $chaining = false;
34
35
        /**
36
         * @var
37
         */
38
        private $config;
39
40
        /**
41
         * @var
42
         */
43
        private $curl;
44
45
        /**
46
         * @var
47
         */
48
        private $signer;
49
50
        /**
51
         * Class construct
52
         *
53
         * @param  string $server  - server enpoint url
54
         * @param  array  $config  - config array which holds object configuration
55
         * @return void
56
         */
57 3
        public function __construct($server, array $config = [])
58
        {
59 3
            $this->config = array_merge([
60 3
                "server" => $server,
61
                "secret" => null
62 3
            ], $config);
63
64
            // check and set client timeout
65 3
            if (!isset($this->config["timeout"]) || !is_numeric($this->config["timeout"])) {
66 3
                $this->config["timeout"] = 10;
67
            }
68 3
        }
69
70
        /**
71
         * Magic getter method, which sets component
72
         *
73
         * @param  string $component
74
         * @return object
75
         */
76 1
        public function __get($component)
77
        {
78
            // on first call reset component array and enable chaining
79 1
            if (!$this->chaining) {
80 1
                $this->component = [];
81 1
                $this->chaining = true;
82
            }
83
84 1
            $this->component[] = ucfirst($component);
85
86 1
            return $this;
87
        }
88
89
        /**
90
         * Magic caller method, which calls component
91
         *
92
         * @param string $action
93
         * @param array  $params
94
         * @return array
95
         */
96 1
        public function __call($action, $params)
97
        {
98
            // set chaining state
99 1
            $this->chaining = false;
100
101
            // load curl
102 1
            if (!$this->curl) {
103 1
                $this->curl = new Lib\Curl([
104 1
                    'server' => $this->config['server'],
105 1
                    'timeout' => $this->config['timeout']
106
                ]);
107
            }
108
109
            // load signer
110 1
            if (!$this->signer) {
111 1
                $this->signer = new Lib\Signer([
112 1
                    'secret' => $this->config['secret']
113
                ]);
114
            }
115
116
            // change params array into numeric
117 1
            $params = array_values($params);
118
119
            // unset local private key
120 1
            unset($this->config["plinker"]["private_key"]);
121
122
            // encode payload
123 1
            $payload = $this->signer->encode([
124 1
                "component" => implode('\\', $this->component),
125 1
                "config" => $this->config,
126 1
                "action" => $action,
127 1
                "params" => $params
128
            ]);
129
130
            // post request to plinker server
131 1
            $response = $this->curl->post($this->config["server"], $payload, [
132 1
                "PLINKER: ".$payload["token"]
133
            ]);
134
135
            // check curl error
136 1
            if (!empty($response['error'])) {
137 1
                return $response;
138
            }
139
140
            // decode json
141
            $body = json_decode($response['body'], true);
142
143
            // json decode (unpack) response body
144
            if (empty($body['data'])) {
145
                return [
146
                    'body'  => $response['body'],
147
                    "code"  => 422,
148
                    "error" => 'Failed to decode payload, invalid response'
149
                ];
150
            }
151
152
            // verify and decode response
153
            try {
154
                $body = $this->signer->decode($body);
155
            } catch (\Exception $e) {
156
                return [
157
                    'body'  => $response['body'],
158
                    "code"  => 401,
159
                    "error" => $e->getMessage()
160
                ];
161
            }
162
163
            // check if $body is Exception
164
            if ($body instanceof \Exception) {
165
                throw $body;
166
            }
167
168
            //
169
            return $body;
170
        }
171
    }
172
173
}
174