Completed
Push — master ( 8e8a04...31fb68 )
by Marco
38:43 queued 23:45
created

HttpTransport::performCall()   B

Complexity

Conditions 4
Paths 25

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 25
nop 4
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
    public function performCall(LoggerInterface $logger, $data, $content_type, $encrypt=false) {
27
28
        $this->setHttpMethod("POST");
29
30
        try {
31
32
            $logger->debug("Sending RPC data");
33
34
            $logger->debug("Original request data dump: ".$data);
35
36
            $data = $this->can($data, $encrypt);
37
38
            $logger->debug("Real request data dump: ".$data);
39
40
            $response = $this->setContentType($content_type)->send($data);
41
42
            $logger->debug("Real response data dump: ".$response);
43
44
            $return = $this->uncan($response, $encrypt);
45
46
            $logger->debug("Decoded response data dump: ".$return);
47
48
        } 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
        return $return;
69
70
    }
71
72
    private function can($data, $key) {
73
74
        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
            $return = $data;
85
86
        }
87
88
        return $return;
89
90
    }
91
92
    private function uncan($data, $key) {
93
94
        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
            $return = $data;
103
104
        }
105
106
        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