Completed
Push — master ( f743f0...1a0e57 )
by Dmitry
01:59
created

Response::get_priv_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
 *      Abstract class for the family of response classes
5
 *
6
 *      @package php_EasyPay
7
 *      @version 1.1
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace EasyPay\Provider31;
13
14
use EasyPay\Log as Log;
15
use EasyPay\Sign as Sign;
16
17
abstract class Response extends \DomDocument
18
{
19
    /**
20
     *      @var string
21
     *
22
     */
23
    const TEMPLATE = '<Response><StatusCode></StatusCode><StatusDetail></StatusDetail><DateTime></DateTime></Response>';
24
25
    /**
26
     *      @var \DOMNode
27
     */
28
    protected $Response;
29
30
    /**
31
     *      @var \DOMElement
32
     */
33
    protected $Sign;
34
35
    /**
36
     *      Response constructor
37
     *
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct('1.0', 'UTF-8');
42
43
        $this->loadXML(self::TEMPLATE);
44
45
        $this->Response = $this->firstChild;
46
        $this->set_DateTime();
47
    }
48
49
    /**
50
     *      Set DateTime element value by current time
51
     */
52
    public function set_DateTime()
53
    {
54
        $this->setElementValue('DateTime', date('Y-m-d\TH:i:s', time()));
55
    }
56
57
    /**
58
     *      Create new element node
59
     *
60
     *      @param string $name
61
     *      @param string $value (optional)
62
     */
63
    public function createElement($name, $value=NULL)
64
    {
65
        return parent::createElement($name, $value);
66
    }
67
68
    /**
69
     *      Set node value
70
     *
71
     *      @param string $name
72
     *      @param string $value
73
     */
74
    public function setElementValue($name, $value)
75
    {
76
        foreach ($this->Response->childNodes as $child)
77
        {
78
            if ($child->nodeName == $name)
79
            {
80
                $child->nodeValue = $value;
81
            }
82
        }
83
    }
84
85
    /**
86
     *      Dumps response into a string
87
     *
88
     *      @return string XML
89
     */
90
    public function friendly()
91
    {
92
        $this->encoding = 'UTF-8';
93
        $this->formatOutput = true;
94
95
        return $this->saveXML(NULL, LIBXML_NOEMPTYTAG);
96
    }
97
98
    /**
99
     *      Sign and send response
100
     *
101
     *      @param array $options
102
     */
103
    public function sign_and_out($options)
104
    {
105
        $this->sign($options);
106
        $this->out_header();
107
        $this->out_body($this->friendly());
108
    }
109
110
    /**
111
     *      Send header
112
     */
113
    protected function out_header()
114
    {
115
        ob_clean();
116
        @header("Content-Type: text/xml; charset=utf-8");
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Content-Type: text/xml; charset=utf-8') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for header(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

116
        /** @scrutinizer ignore-unhandled */ @header("Content-Type: text/xml; charset=utf-8");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
117
    }
118
119
    /**
120
     *      Send body
121
     *
122
     *      @param string $body
123
     */
124
    protected function out_body($body)
125
    {
126
        Log::instance()->debug('response sends: ');
127
        Log::instance()->debug($body);
128
129
        echo $body;
130
    }
131
132
    /**
133
     *      Add Sign (if hasn't yet done)
134
     *
135
     *      @param array $options
136
     */
137
    protected function sign($options)
138
    {
139
        if (isset($this->Sign)) return;
140
141
        if (isset($options['UseSign']) && ($options['UseSign'] === true))
142
        {
143
            $this->Sign = $this->createElement('Sign');
144
            $this->Response->appendChild($this->Sign);
145
146
            $this->Sign->nodeValue = (new Sign())->generate($this->friendly(), $options);
147
        }
148
    }
149
}
150