Completed
Push — 2.0 ( 7b6aa1...d47467 )
by Marco
35:53
created

HttpTransport   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 54.05%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 113
ccs 20
cts 37
cp 0.5405
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B performCall() 0 45 4
A can() 0 19 3
A uncan() 0 17 4
A checkEncryptedResponseConsistency() 0 5 2
1
<?php namespace Comodojo\RpcClient\Components;
2
3
use \Comodojo\RpcClient\Interfaces\Transport as TransportInterface;
4
use \Comodojo\Httprequest\Httprequest;
5
use \phpseclib\Crypt\AES;
6
use \Psr\Log\LoggerInterface;
7
use \Comodojo\Exception\HttpException;
8
use \Comodojo\Exception\RpcException;
9
use \Exception;
10
11
class HttpTransport extends Httprequest implements TransportInterface {
12
13
    private $aes = null;
14
15
    /**
16
     * Send pre-econded request to server
17
     *
18
     * @param   string   $data
19
     * @param   string   $content_type
20
     *
21
     * @return  string
22
     *
23
     * @throws \Comodojo\Exception\RpcException
24
     * @throws \Comodojo\Exception\HttpException
25
     */
26 51
    public function performCall(LoggerInterface $logger, $data, $content_type, $encrypt=false) {
27
28 51
        $this->setHttpMethod("POST");
29
30
        try {
31
32 51
            $logger->notice("Sending RPC data");
33
34 51
            $logger->debug("Original request data dump: ".$data);
35
36 51
            $data = $this->can($data, $encrypt);
37
38 51
            $logger->debug("Real request data dump: ".$data);
39
40 51
            $response = $this->setContentType($content_type)->send($data);
41
42 51
            $logger->debug("Real response data dump: ".$response);
43
44 51
            $return = $this->uncan($response, $encrypt);
45
46 51
            $logger->debug("Decoded response data dump: ".$return);
47
48 51
        } catch (HttpException $he) {
49
50
            $logger->error("HTTP Transport error: ".$he->getMessage());
51
52
            throw $he;
53
54
        } catch (RpcException $re) {
55
56
            $logger->error("RPC Client error: ".$re->getMessage());
57
58
            throw $re;
59
60
        } catch (Exception $e) {
61
62
            $logger->critical("Generic Client error: ".$e->getMessage());
63
64
            throw $e;
65
66
        }
67
68 51
        return $return;
69
70
    }
71
72 51
    private function can($data, $key) {
73
74 51
        if ( !empty($key) && is_string($key) ) {
75
76
            $this->aes = new AES();
77
78
            $this->aes->setKey($key);
79
80
            $return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) );
81
82
        } else {
83
84 51
            $return = $data;
85
86
        }
87
88 51
        return $return;
89
90
    }
91
92 51
    private function uncan($data, $key) {
93
94 51
        if ( !empty($key) && is_string($key) ) {
95
96
            if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received");
97
98
            $return = $this->aes->decrypt(base64_decode(substr($data, 28)));
99
100
        } else {
101
102 51
            $return = $data;
103
104
        }
105
106 51
        return $return;
107
108
    }
109
110
    /**
111
     * Check if an encrypted envelope is consisent or not
112
     *
113
     * @param   string    $data
114
     *
115
     * @return  bool
116
     */
117
    private static function checkEncryptedResponseConsistency($data) {
118
119
        return substr($data, 0, 27) == 'comodojo_encrypted_response' ? true : false;
120
121
    }
122
123
}
124