Passed
Push — master ( 8821e7...5919b1 )
by Lawrence
01:36
created

Client::__call()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0798

Importance

Changes 20
Bugs 2 Features 1
Metric Value
cc 6
eloc 25
c 20
b 2
f 1
nc 12
nop 2
dl 0
loc 53
ccs 20
cts 23
cp 0.8696
crap 6.0798
rs 8.7155

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 $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']) || !($body = json_decode($response['body'], true))) {
124 1
            $response['error'] = 'Failed to decode payload, invalid json';
125 1
            return $response;
126
        }
127
128
        // verify and decode response
129
        if (!($body = $this->signer->decode($body))) {
130
            return [
131
                'body' => null,
132
                "code" => 422,
133
                "error" => 'Failed to decode payload, check secret'
134
            ];
135
        }
136
137
        //
138
        return $body;
139
    }
140
}
141