Completed
Push — master ( 973219...7e7602 )
by Lawrence
02:29
created

Client::__call()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 61
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8.1426

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 16
nop 2
dl 0
loc 61
ccs 20
cts 28
cp 0.7143
crap 8.1426
rs 7.399
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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