Completed
Push — master ( 9e4e3b...542a9c )
by Lawrence
04:36
created

plinker_client()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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