Completed
Push — master ( 1a0e57...bc084a )
by Dmitry
02:08
created

General::check_and_parse_operation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
3
/**
4
 *      General class for all request types
5
 *
6
 *      @package php_EasyPay
7
 *      @version 1.1
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace EasyPay\Provider31\Request;
13
14
use EasyPay\Log as Log;
15
use EasyPay\Exception;
16
use EasyPay\Sign as Sign;
17
use EasyPay\Provider31\Request\RAW as RAW;
18
19
class General
20
{
21
    /**
22
     *      @var EasyPay\Provider31\Request\RAW raw request
0 ignored issues
show
Bug introduced by
The type EasyPay\Provider31\Reque...\Provider31\Request\RAW was not found. Did you mean EasyPay\Provider31\Request\RAW? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    protected $raw_request;
25
26
    /**
27
     *      @var string 'DateTime' node
28
     */
29
    protected $DateTime;
30
31
    /**
32
     *      @var string 'Sign' node
33
     */
34
    protected $Sign;
35
36
    /**
37
     *      @var string 'Operation' type
38
     */
39
    protected $Operation;
40
41
    /**
42
     *      @var string 'ServiceId' node
43
     */
44
    protected $ServiceId;
45
46
    /**
47
     *      @var array list of possible operations
48
     */
49
    protected $operations = array('Check','Payment','Confirm','Cancel');
50
51
    /**
52
     *      General constructor
53
     *
54
     *      @param \EasyPay\Provider31\Request\RAW $raw Raw request data
55
     */
56
    public function __construct($raw)
57
    {
58
        $this->raw_request = $raw;
0 ignored issues
show
Documentation Bug introduced by
It seems like $raw of type EasyPay\Provider31\Request\RAW is incompatible with the declared type EasyPay\Provider31\Reque...\Provider31\Request\RAW of property $raw_request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
60
        $this->parse_request_data();
61
    }
62
63
    /**
64
     *      Get DateTime
65
     *
66
     *      @return string
67
     */
68
    public function DateTime()
69
    {
70
        return $this->DateTime;
71
    }
72
73
    /**
74
     *      Get Sign
75
     *
76
     *      @return string
77
     */
78
    public function Sign()
79
    {
80
        return $this->Sign;
81
    }
82
83
    /**
84
     *      Get Operation type
85
     *
86
     *      @return string
87
     */
88
    public function Operation()
89
    {
90
        return $this->Operation;
91
    }
92
93
    /**
94
     *      Get ServiceId
95
     *
96
     *      @return string
97
     */
98
    public function ServiceId()
99
    {
100
        return $this->ServiceId;
101
    }
102
103
    /**
104
     *      Parse xml-request, which was previously "extracted" from the body of the http request
105
     *
106
     *      @throws Exception\Structure
107
     */
108
    protected function parse_request_data()
109
    {
110
        // process <Request> group
111
        $r = $this->raw_request->get_nodes_from_request('Request');
112
113
        if (count($r) < 1)
114
        {
115
            throw new Exception\Structure('The xml-query does not contain any element Request!', -52);
116
        }
117
118
        foreach ($r[0]->childNodes as $child)
119
        {
120
            $this->check_and_parse_request_node($child, 'DateTime');
121
            $this->check_and_parse_request_node($child, 'Sign');
122
123
            $this->check_and_parse_operation($child);
124
        }
125
126
        if ( ! isset($this->Operation))
127
        {
128
            throw new Exception\Structure('There is no Operation type element in the xml request!', -55);
129
        }
130
131
        // process <Operation> group
132
        $r = $this->raw_request->get_nodes_from_request($this->Operation);
133
134
        foreach ($r[0]->childNodes as $child)
135
        {
136
            $this->check_and_parse_request_node($child, 'ServiceId');
137
        }
138
    }
139
140
    /**
141
     *      Check if present node of request and then parse it
142
     *
143
     *      @param \DOMNode $n
144
     *      @param string $name
145
     */
146
    protected function check_and_parse_request_node($n, $name)
147
    {
148
        if ($n->nodeName == $name)
149
        {
150
            $this->parse_request_node($n, $name);
151
        }
152
    }
153
154
    /**
155
     *      Check if given node is Operation
156
     *
157
     *      @throws Exception\Structure
158
     */
159
    protected function check_and_parse_operation($n)
160
    {
161
        if (in_array($n->nodeName, $this->operations))
162
        {
163
            if ( ! isset($this->Operation))
164
            {
165
                $this->Operation = $n->nodeName;
166
            }
167
            else
168
            {
169
                throw new Exception\Structure('There is more than one Operation type element in the xml-query!', -53);
170
            }
171
        }
172
    }
173
174
    /**
175
     *      Parse node of request
176
     *
177
     *      @param \DOMNode $n
178
     *      @param string $name
179
     *
180
     *      @throws Exception\Structure
181
     */
182
    protected function parse_request_node($n, $name)
183
    {
184
        if ( ! isset($this->$name))
185
        {
186
            $this->$name = $n->nodeValue;
187
        }
188
        else
189
        {
190
            throw new Exception\Structure('There is more than one '.$name.' element in the xml-query!', -56);
191
        }
192
    }
193
194
    /**
195
     *      "Rough" validation of the received xml request
196
     *
197
     *      @param array $options
198
     *      @throws Exception\Data
199
     *      @throws Exception\Structure
200
     */
201
    public function validate_request($options)
202
    {
203
        $this->validate_element('DateTime');
204
        $this->validate_element('Sign');
205
        $this->validate_element('ServiceId');
206
207
        // compare received value ServiceId with option ServiceId
208
        if (intval($options['ServiceId']) != intval($this->ServiceId))
209
        {
210
            throw new Exception\Data('This request is not for our ServiceId!', -58);
211
        }
212
    }
213
214
    /**
215
     *      Validation of xml-element
216
     *
217
     *      @param string $name
218
     */
219
    public function validate_element($name)
220
    {
221
        if ( ! isset($this->$name))
222
        {
223
            throw new Exception\Structure('There is no '.$name.' element in the xml request!', -57);
224
        }
225
    }
226
227
    /**
228
     *      Verify signature of request
229
     *
230
     *      @param array $options
231
     */
232
    public function verify_sign($options)
233
    {
234
        $sign = new Sign();
235
        $sign->verify(
236
            $this->raw_request->str(),
237
            $this->Sign,
238
            $options
239
        );
240
    }
241
242
}
243