Passed
Push — master ( 2bc77d...2cd501 )
by Dmitry
01:47
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 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 = $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 = $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
     *      @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 file contents
105
     *
106
     *      @param string $fname
107
     *      @return string
108
     */
109
    protected function file_get_contents($fname)
110
    {
111
        return file_get_contents($fname);
112
    }
113
114
    /**
115
     *      Get UAPAY public key
116
     *
117
     *      @throws Exception\Runtime
118
     *      @return string Public key
119
     */
120 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...
121
    {
122
        // check public key file
123
        if ( ! file_exists($this->jwt['UAPAY_pubkey']))
124
        {
125
            throw new Exception\Runtime('The file with the public key was not find!');
126
        }
127
128
        // load public key file
129
        $public_key = $this->file_get_contents($this->jwt['UAPAY_pubkey']);
130
        if ($public_key === FALSE)
131
        {
132
            throw new Exception\Runtime('The file with the public key was not read!');
133
        }
134
135
        return $public_key;
136
    }
137
138
    /**
139
     *      Decode token
140
     *
141
     *      @param string $token
142
     *      @throws Exception\Runtime
143
     *      @return array
144
     */
145 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...
146
    {
147
        try
148
        {
149
            $decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512'));
150
        }
151
        catch (\Exception $e)
152
        {
153
            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

153
            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...
154
            throw new Exception\JSON('unable to decode JWT token', $e);
155
        }
156
157
        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

157
        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...
158
        Log::instance()->debug(print_r($decoded, true));
159
160
        return $decoded;
161
    }
162
163
    /**
164
     *      Get status code
165
     *
166
     *      @return int
167
     */
168
    public function status()
169
    {
170
        return $this->status;
171
    }
172
}
173