Passed
Push — master ( 120832...1dab8b )
by Dmitry
02:12
created

RAW::getNodes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 18
rs 9.2
1
<?php
2
3
/**
4
 *      Class for raw request
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
17
class RAW
18
{
19
    /**
20
     *      @var string raw request
21
     */
22
    protected $raw_request;
23
24
    /**
25
     *      RAW constructor
26
     */
27
    public function __construct()
28
    {
29
        $this->get_http_raw_post_data();
30
        $this->check_presence_request();
31
    }
32
33
    /**
34
     *      Get request string content
35
     *
36
     *      @return string
37
     */
38
    public function str()
39
    {
40
        return strval($this->raw_request);
41
    }
42
43
    /**
44
     *      Get data from the body of the http request
45
     *
46
     *      - with the appropriate configuration of php.ini they can be found
47
     *        in the global variable $HTTP_RAW_POST_DATA
48
     *
49
     *      - but it's easier just to read the data from the php://input stream,
50
     *        which does not depend on the php.ini directives and allows you to read
51
     *        raw data from the request body
52
     */
53
    protected function get_http_raw_post_data()
54
    {
55
        Log::instance()->add('request from ' . $_SERVER['REMOTE_ADDR']);
56
57
        $this->raw_request = file_get_contents('php://input');
58
59
        Log::instance()->debug('request received: ');
60
        Log::instance()->debug($this->raw_request);
61
        Log::instance()->debug(' ');
62
    }
63
64
    /**
65
     *      Check if presence request
66
     *
67
     *      @throws Exception\Structure
68
     */
69
    protected function check_presence_request()
70
    {
71
        if (empty($this->raw_request))
72
        {
73
            throw new Exception\Structure('An empty xml request', -50);
74
        }
75
    }
76
77
    /**
78
     *      Get group of nodes from XML-request
79
     *
80
     *      @param string $name
81
     *      @return array
82
     */
83
    public function get_nodes_from_request($name)
84
    {
85
        libxml_use_internal_errors(true);
86
        $doc = new \DOMDocument();
87
        if ( ! $doc->loadXML($this->raw_request))
88
        {
89
            foreach(libxml_get_errors() as $e){
90
                Log::instance()->error($e->message);
91
            }
92
            throw new Exception\Structure('The wrong XML is received', -51);
93
        }
94
95
        return $this->getNodes($doc, $name);
96
    }
97
98
    /**
99
     *      Selects nodes by name
100
     *
101
     *      @param \DOMDocument $dom
102
     *      @param string $name
103
     *      @param array $ret
104
     *
105
     *      @return array nodes with the name
106
     */
107
    protected function getNodes($dom, $name, $ret=array())
108
    {
109
        foreach($dom->childNodes as $child)
110
        {
111
            if ($child->nodeName == $name)
112
            {
113
                array_push($ret, $child);
114
            }
115
            else
116
            {
117
                if (count($child->childNodes) > 0)
118
                {
119
                    $ret = $this->getNodes($child, $name, $ret);
120
                }
121
            }
122
        }
123
124
        return $ret;
125
    }
126
}
127