Passed
Push — master ( 419110...a41d9b )
by Dmitry
02:04
created

Response::uapay_public_key()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 1
nop 0
dl 22
loc 22
rs 8.9197
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 string
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 array $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_decode($json_string);
0 ignored issues
show
Bug introduced by
$json_string of type array is incompatible with the type string expected by parameter $json_string of UAPAY\Response::json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $this->json_decode(/** @scrutinizer ignore-type */ $json_string);
Loading history...
44
        $this->json_handle();
45
    }
46
47
    /**
48
     *      Decode JSON
49
     *
50
     *      @param string $json_string
51
     *      @throws Exception\Runtime
52
     */
53
    protected function json_decode($json_string)
54
    {
55
        $this->json = json_decode($json_string, true);
56
57
        if (json_last_error() != 0)
58
        {
59
            throw new Exception\Runtime('decoding error of the json response!');
60
        }
61
    }
62
63
    /**
64
     *      Handle decoded JSON
65
     *
66
     *      @throws Exception\JSON
67
     */
68
    protected function json_handle()
69
    {
70
        if (isset($this->json['error']))
71
        {
72
            throw new Exception\JSON('json response contain an error message!');
73
        }
74
        if ( ! isset($this->json['status']))
75
        {
76
            throw new Exception\JSON('invalid json response!');
77
        }
78
        if ($this->json['status'] == 0)
79
        {
80
            throw new Exception\JSON('json response contain an error status!');
81
        }
82
        $this->status = $this->json['status'];
83
84
        if ( ! isset($this->json['data']) || !is_array($this->json['data']))
85
        {
86
            throw new Exception\JSON('json does not contain the data field!');
87
        }
88
89
        if ($this->jwt['using'] === true)
90
        {
91
            if ( ! isset($this->json['data']['token']))
92
            {
93
                throw new Exception\JSON('data does not contain the token field!');
94
            }
95
96
            $this->json['data'] = $this->token_decode($this->json['data']['token']);
97
        }
98
    }
99
100
    /**
101
     *      Get UAPAY public key
102
     *
103
     *      @throws Exception\Runtime
104
     *      @return string Public key
105
     */
106 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...
107
    {
108
        // check public key file
109
        if ( ! file_exists($this->jwt['UAPAY_pubkey']))
110
        {
111
            throw new Exception\Runtime('The file with the public key was not find!');
112
        }
113
114
        // load public key file
115
        $fpkey = fopen($this->jwt['UAPAY_pubkey'], "rb");
116
        if ($fpkey === FALSE)
117
        {
118
            throw new Exception\Runtime('The file with the public key was not open!');
119
        }
120
        $public_key = fread($fpkey, 8192);
121
        if ($public_key === FALSE)
122
        {
123
            throw new Exception\Runtime('The file with the public key was not read!');
124
        }
125
        fclose($fpkey);
126
127
        return $public_key;
128
    }
129
130
    /**
131
     *      Decode token
132
     *
133
     *      @param string $token
134
     *      @throws Exception\Runtime
135
     *      @return array
136
     */
137 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...
138
    {
139
        try
140
        {
141
            $decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512'));
142
        }
143
        catch (\Exception $e)
144
        {
145
            Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString());
0 ignored issues
show
Bug introduced by
The method error() does not exist on UAPAY\Log. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            Log::instance()->/** @scrutinizer ignore-call */ error($e->getMessage().PHP_EOL.$e->getTraceAsString());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
            throw new Exception\JSON('unable to decode JWT token', $e);
147
        }
148
149
        Log::instance()->debug('decoded payload:');
0 ignored issues
show
Bug introduced by
The method debug() does not exist on UAPAY\Log. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        Log::instance()->/** @scrutinizer ignore-call */ debug('decoded payload:');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        Log::instance()->debug(print_r($decoded, true));
151
152
        return $decoded;
153
    }
154
155
    /**
156
     *      Get status code
157
     *
158
     *      @return int
159
     */
160
    public function status()
161
    {
162
        return $this->status;
163
    }
164
}
165