Passed
Push — master ( 5b937c...6d705d )
by Dmitry
01:38
created

Response::file_get_contents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace UAPAY;
4
5
use UAPAY\Log as Log;
6
use UAPAY\Exception;
7
use Firebase\JWT\JWT;
8
9
abstract class Response
10
{
11
    /**
12
     *      @var array
13
     */
14
    protected $json;
15
16
    /**
17
     *      @var array
18
     */
19
    protected $jwt=array(
20
        'using'         => false,
21
        'UAPAY_pubkey'  => '',
22
        'our_privkey'   => '',
23
    );
24
25
    /**
26
     *      @var int
27
     */
28
    protected $status;
29
30
    /**
31
     *      Constructor
32
     *
33
     *      @param string $json_string JSON string
34
     *      @param array $jwt_options array of options
35
     */
36
    public function __construct($json_string, $jwt_options=null)
37
    {
38
        if (isset($jwt_options) && is_array($jwt_options))
39
        {
40
            $this->jwt = array_merge($this->jwt, $jwt_options);
41
        }
42
43
        $this->json = $this->json_decode($json_string);
44
        $this->json_handle();
45
    }
46
47
    /**
48
     *      Decode JSON
49
     *
50
     *      @param string $json_string
51
     *      @return array
52
     *      @throws Exception\Runtime
53
     */
54
    protected function json_decode($json_string)
55
    {
56
        $decoded = json_decode($json_string, true);
57
58
        if (json_last_error() != 0)
59
        {
60
            throw new Exception\Runtime('decoding error of the json response!');
61
        }
62
63
        return $decoded;
64
    }
65
66
    /**
67
     *      Handle decoded JSON
68
     *
69
     *      @throws Exception\JSON
70
     */
71
    protected function json_handle()
72
    {
73
        if (isset($this->json['error']))
74
        {
75
            throw new Exception\JSON('json response contain an error message!');
76
        }
77
        if ( ! isset($this->json['status']))
78
        {
79
            throw new Exception\JSON('invalid json response!');
80
        }
81
        if ($this->json['status'] == 0)
82
        {
83
            throw new Exception\JSON('json response contain an error status!');
84
        }
85
        $this->status = $this->json['status'];
86
87
        if ( ! isset($this->json['data']) || !is_array($this->json['data']))
88
        {
89
            throw new Exception\JSON('json does not contain the data field!');
90
        }
91
92
        if ($this->jwt['using'] === true)
93
        {
94
            if ( ! isset($this->json['data']['token']))
95
            {
96
                throw new Exception\JSON('data does not contain the token field!');
97
            }
98
99
            $this->json['data'] = $this->token_decode($this->json['data']['token']);
100
        }
101
    }
102
103
    /**
104
     *      Get UAPAY public key
105
     *
106
     *      @throws Exception\Runtime
107
     *      @return string Public key
108
     */
109 View Code Duplication
    protected function uapay_public_key()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        try
112
        {
113
            $key = new Key();
114
            $public_key = $key->get($this->jwt['UAPAY_pubkey']);
115
        }
116
        catch (\Exception $e)
117
        {
118
            throw new Exception\Runtime('The file with the public key was '.$e->getMessage().'!');
119
        }
120
121
        return $public_key;
122
    }
123
124
    /**
125
     *      Decode token
126
     *
127
     *      @param string $token
128
     *      @throws Exception\Runtime
129
     *      @return array
130
     */
131 View Code Duplication
    protected function token_decode($token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        try
134
        {
135
            $decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512'));
136
        }
137
        catch (\Exception $e)
138
        {
139
            Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString());
140
            throw new Exception\JSON('unable to decode JWT token', $e);
141
        }
142
143
        Log::instance()->debug('decoded payload:');
144
        Log::instance()->debug(print_r($decoded, true));
145
146
        return $decoded;
147
    }
148
149
    /**
150
     *      Get status code
151
     *
152
     *      @return int
153
     */
154
    public function status()
155
    {
156
        return $this->status;
157
    }
158
}
159