Callback::json_handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 20
rs 9.9
1
<?php
2
3
namespace UAPAY;
4
5
use UAPAY\Log as Log;
6
use UAPAY\Exception;
7
8
class Callback extends Response
9
{
10
    /**
11
     *      @var string
12
     */
13
    protected $id;
14
15
    /**
16
     *      @var string
17
     */
18
    protected $orderId;
19
20
    /**
21
     *      @var int
22
     */
23
    protected $number;
24
25
    /**
26
     *      @var int
27
     */
28
    protected $amount;
29
30
    /**
31
     *      @var string
32
     */
33
    protected $createdAt;
34
35
    /**
36
     *      Constructor
37
     *
38
     *      @param array $jwt_options array of options
39
     */
40
    public function __construct($options=null)
41
    {
42
        $jo = new JWTOptions($options);
43
        parent::__construct($this->get_http_raw_post_data(), $jo->get());
44
    }
45
46
    /**
47
     *      Get data from the body of the http request
48
     *
49
     *      - with the appropriate configuration of php.ini they can be found
50
     *        in the global variable $HTTP_RAW_POST_DATA
51
     *
52
     *      - but it's easier just to read the data from the php://input stream,
53
     *        which does not depend on the php.ini directives and allows you to read
54
     *        raw data from the request body
55
     *
56
     *      @return string Http raw post data
57
     *
58
     */
59
    protected function get_http_raw_post_data()
60
    {
61
        Log::instance()->add('callback request from ' . $_SERVER['REMOTE_ADDR']);
62
63
        $raw_request = file_get_contents('php://input');
64
65
        Log::instance()->debug('got: ');
66
        Log::instance()->debug($raw_request);
67
        Log::instance()->debug(' ');
68
69
        return $raw_request;
70
    }
71
72
    /**
73
     *      Handle decoded JSON
74
     *
75
     *      @throws Exception\JSON
76
     */
77
    protected function json_handle()
78
    {
79
        $this->check_if_error();
80
81
        if ($this->jwt['using'] === true)
82
        {
83
            if ( ! isset($this->json['token']))
84
            {
85
                throw new Exception\JSON('json does not contain the token field!');
86
            }
87
88
            $this->json = $this->token_decode($this->json['token']);
89
        }
90
91
        $this->status = $this->json_value('status');
92
        $this->id = $this->json_value('id');
93
        $this->orderId = $this->json_value('orderId');
94
        $this->number = $this->json_value('number');
95
        $this->amount = $this->json_value('amount');
96
        $this->createdAt = $this->json_value('createdAt');
97
    }
98
99
    /**
100
     *      Get value from JSON
101
     *
102
     *      @param string $name
103
     *      @return mixed
104
     */
105
    protected function json_value($name)
106
    {
107
        if (!isset($this->json[$name]))
108
        {
109
            throw new Exception\JSON('invalid json callback!');
110
        }
111
112
        return $this->json[$name];
113
    }
114
115
    /**
116
     *      Get id
117
     *
118
     *      @return string
119
     */
120
    public function id()
121
    {
122
        return $this->id;
123
    }
124
125
    /**
126
     *      Get order id
127
     *
128
     *      @return string
129
     */
130
    public function orderId()
131
    {
132
        return $this->orderId;
133
    }
134
135
    /**
136
     *      Get number of paymnet
137
     *
138
     *      @return int
139
     */
140
    public function number()
141
    {
142
        return $this->number;
143
    }
144
145
    /**
146
     *      Get amount
147
     *
148
     *      @return int
149
     */
150
    public function amount()
151
    {
152
        return $this->amount;
153
    }
154
155
    /**
156
     *      Get createdAt
157
     *
158
     *      @return string
159
     */
160
    public function createdAt()
161
    {
162
        return $this->createdAt;
163
    }
164
165
    /**
166
     *      Get jsons array
167
     *
168
     *      @return array
169
     */
170
    public function json()
171
    {
172
        return $this->json;
173
    }
174
}
175