Passed
Push — master ( 1dab8b...f743f0 )
by Dmitry
02:07
created

General::get_pub_key()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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\Provider31\Request\RAW as RAW;
17
18
class General
19
{
20
    /**
21
     *      @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...
22
     */
23
    protected $raw_request;
24
25
    /**
26
     *      @var string 'DateTime' node
27
     */
28
    protected $DateTime;
29
30
    /**
31
     *      @var string 'Sign' node
32
     */
33
    protected $Sign;
34
35
    /**
36
     *      @var string 'Operation' type
37
     */
38
    protected $Operation;
39
40
    /**
41
     *      @var string 'ServiceId' node
42
     */
43
    protected $ServiceId;
44
45
    /**
46
     *      @var array list of possible operations
47
     */
48
    protected $operations = array('Check','Payment','Confirm','Cancel');
49
50
    /**
51
     *      General constructor
52
     *
53
     *      @param EasyPay\Provider31\Request\RAW $raw Raw request data
54
     */
55
    public function __construct($raw)
56
    {
57
        $this->raw_request = $raw;
58
59
        $this->parse_request_data();
60
    }
61
62
    /**
63
     *      Get DateTime
64
     *
65
     *      @return string
66
     */
67
    public function DateTime()
68
    {
69
        return $this->DateTime;
70
    }
71
72
    /**
73
     *      Get Sign
74
     *
75
     *      @return string
76
     */
77
    public function Sign()
78
    {
79
        return $this->Sign;
80
    }
81
82
    /**
83
     *      Get Operation type
84
     *
85
     *      @return string
86
     */
87
    public function Operation()
88
    {
89
        return $this->Operation;
90
    }
91
92
    /**
93
     *      Get ServiceId
94
     *
95
     *      @return string
96
     */
97
    public function ServiceId()
98
    {
99
        return $this->ServiceId;
100
    }
101
102
    /**
103
     *      Parse xml-request, which was previously "extracted" from the body of the http request
104
     *
105
     *      @throws Exception\Structure
106
     */
107
    protected function parse_request_data()
108
    {
109
        // process <Request> group
110
        $r = $this->raw_request->get_nodes_from_request('Request');
111
112
        if (count($r) < 1)
113
        {
114
            throw new Exception\Structure('The xml-query does not contain any element Request!', -52);
115
        }
116
117
        foreach ($r[0]->childNodes as $child)
118
        {
119
            $this->check_and_parse_request_node($child, 'DateTime');
120
            $this->check_and_parse_request_node($child, 'Sign');
121
122
            if (in_array($child->nodeName, $this->operations))
123
            {
124
                if ( ! isset($this->Operation))
125
                {
126
                    $this->Operation = $child->nodeName;
127
                }
128
                else
129
                {
130
                    throw new Exception\Structure('There is more than one Operation type element in the xml-query!', -53);
131
                }
132
            }
133
        }
134
135
        if ( ! isset($this->Operation))
136
        {
137
            throw new Exception\Structure('There is no Operation type element in the xml request!', -55);
138
        }
139
140
        // process <Operation> group
141
        $r = $this->raw_request->get_nodes_from_request($this->Operation);
142
143
        foreach ($r[0]->childNodes as $child)
144
        {
145
            $this->check_and_parse_request_node($child, 'ServiceId');
146
        }
147
    }
148
149
    /**
150
     *      Check if present node of request and then parse it
151
     *
152
     *      @param \DOMNode $n
153
     *      @param string $name
154
     */
155
    protected function check_and_parse_request_node($n, $name)
156
    {
157
        if ($n->nodeName == $name)
158
        {
159
            $this->parse_request_node($n, $name);
160
        }
161
    }
162
163
    /**
164
     *      Parse node of request
165
     *
166
     *      @param \DOMNode $n
167
     *      @param string $name
168
     *
169
     *      @throws Exception\Structure
170
     */
171
    protected function parse_request_node($n, $name)
172
    {
173
        if ( ! isset($this->$name))
174
        {
175
            $this->$name = $n->nodeValue;
176
        }
177
        else
178
        {
179
            throw new Exception\Structure('There is more than one '.$name.' element in the xml-query!', -56);
180
        }
181
    }
182
183
    /**
184
     *      "Rough" validation of the received xml request
185
     *
186
     *      @param array $options
187
     *      @throws Exception\Data
188
     *      @throws Exception\Structure
189
     */
190
    public function validate_request($options)
191
    {
192
        $this->validate_element('DateTime');
193
        $this->validate_element('Sign');
194
        $this->validate_element('ServiceId');
195
196
        // compare received value ServiceId with option ServiceId
197
        if (intval($options['ServiceId']) != intval($this->ServiceId))
198
        {
199
            throw new Exception\Data('This request is not for our ServiceId!', -58);
200
        }
201
    }
202
203
    /**
204
     *      Validation of xml-element
205
     *
206
     *      @param string $name
207
     */
208
    public function validate_element($name)
209
    {
210
        if ( ! isset($this->$name))
211
        {
212
            throw new Exception\Structure('There is no '.$name.' element in the xml request!', -57);
213
        }
214
    }
215
216
    /**
217
     *      Verify signature of request
218
     *
219
     *      @param array $options
220
     */
221
    public function verify_sign($options)
222
    {
223
        $sign = new \EasyPay\Sign();
224
        $sign->verify(
225
            $this->raw_request->str(),
226
            $this->Sign,
227
            $options
228
        );
229
    }
230
231
}
232