Completed
Push — master ( bc084a...68ddf4 )
by Dmitry
02:10
created

General::check_presence_operation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
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
        $this->check_presence_operation();
126
127
        // process <Operation> group
128
        $r = $this->raw_request->get_nodes_from_request($this->Operation);
129
130
        foreach ($r[0]->childNodes as $child)
131
        {
132
            $this->check_and_parse_request_node($child, 'ServiceId');
133
        }
134
    }
135
136
    /**
137
     *      Check if present node of request and then parse it
138
     *
139
     *      @param \DOMNode $n
140
     *      @param string $name
141
     */
142
    protected function check_and_parse_request_node($n, $name)
143
    {
144
        if ($n->nodeName == $name)
145
        {
146
            $this->parse_request_node($n, $name);
147
        }
148
    }
149
150
    /**
151
     *      Check if given node is Operation
152
     *
153
     *      @throws Exception\Structure
154
     */
155
    protected function check_and_parse_operation($n)
156
    {
157
        if (in_array($n->nodeName, $this->operations))
158
        {
159
            if ( ! isset($this->Operation))
160
            {
161
                $this->Operation = $n->nodeName;
162
            }
163
            else
164
            {
165
                throw new Exception\Structure('There is more than one Operation type element in the xml-query!', -53);
166
            }
167
        }
168
    }
169
170
    /**
171
     *      Check if Operation present in request
172
     *
173
     *      @throws Exception\Structure
174
     */
175
    protected function check_presence_operation()
176
    {
177
        if ( ! isset($this->Operation))
178
        {
179
            throw new Exception\Structure('There is no Operation type element in the xml request!', -55);
180
        }
181
    }
182
183
    /**
184
     *      Parse node of request
185
     *
186
     *      @param \DOMNode $n
187
     *      @param string $name
188
     *
189
     *      @throws Exception\Structure
190
     */
191
    protected function parse_request_node($n, $name)
192
    {
193
        if ( ! isset($this->$name))
194
        {
195
            $this->$name = $n->nodeValue;
196
        }
197
        else
198
        {
199
            throw new Exception\Structure('There is more than one '.$name.' element in the xml-query!', -56);
200
        }
201
    }
202
203
    /**
204
     *      "Rough" validation of the received xml request
205
     *
206
     *      @param array $options
207
     *      @throws Exception\Data
208
     *      @throws Exception\Structure
209
     */
210
    public function validate_request($options)
211
    {
212
        $this->validate_element('DateTime');
213
        $this->validate_element('Sign');
214
        $this->validate_element('ServiceId');
215
216
        // compare received value ServiceId with option ServiceId
217
        if (intval($options['ServiceId']) != intval($this->ServiceId))
218
        {
219
            throw new Exception\Data('This request is not for our ServiceId!', -58);
220
        }
221
    }
222
223
    /**
224
     *      Validation of xml-element
225
     *
226
     *      @param string $name
227
     */
228
    public function validate_element($name)
229
    {
230
        if ( ! isset($this->$name))
231
        {
232
            throw new Exception\Structure('There is no '.$name.' element in the xml request!', -57);
233
        }
234
    }
235
236
    /**
237
     *      Verify signature of request
238
     *
239
     *      @param array $options
240
     */
241
    public function verify_sign($options)
242
    {
243
        $sign = new Sign();
244
        $sign->verify(
245
            $this->raw_request->str(),
246
            $this->Sign,
247
            $options
248
        );
249
    }
250
251
}
252