Response::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 15
cts 15
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2
1
<?php
2
namespace Loevgaard\AltaPay\Response;
3
4
use Loevgaard\AltaPay\Exception\XmlException;
5
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
6
7
abstract class Response implements ResponseInterface
8
{
9
    /**
10
     * Holds the response object
11
     *
12
     * @var PsrResponseInterface
13
     */
14
    protected $response;
15
16
    /**
17
     * Holds the entire XML response string, i.e.
18
     *
19
     * <APIResponse version="20110831">
20
     *   <Header>
21
     *     <Date>2011-08-29T23:48:32+02:00</Date>
22
     *     <Path>API/xxx</Path>
23
     *     <ErrorCode>0</ErrorCode>
24
     *     <ErrorMessage/>
25
     *   </Header>
26
     *   <Body>
27
     *     [.....]
28
     *   </Body>
29
     * </APIResponse>
30
     *
31
     * @var string
32
     */
33
    protected $xml;
34
35
    /**
36
     * Holds an XML object based on the Response::$xml string
37
     *
38
     * @var \SimpleXMLElement
39
     */
40
    protected $xmlDoc;
41
42
    /**
43
     * @var string
44
     */
45
    protected $version;
46
47
    /**
48
     * @var \DateTimeImmutable
49
     */
50
    protected $date;
51
52
    /**
53
     * @var string
54
     */
55
    protected $path;
56
57
    /**
58
     * @var int
59
     */
60
    protected $errorCode;
61
62
    /**
63
     * @var string
64
     */
65
    protected $errorMessage;
66
67
    /**
68
     * @param PsrResponseInterface $response
69
     * @throws XmlException
70
     */
71 27 View Code Duplication
    public function __construct(PsrResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73 27
        $this->response = $response;
74 27
        $this->xml = (string)$this->response->getBody();
75 27
        $this->xmlDoc = new \SimpleXMLElement($this->xml);
76 27
        $this->version = (string)$this->xmlDoc['version'];
77 27
        $this->date = \DateTimeImmutable::createFromFormat(DATE_RFC3339, (string)$this->xmlDoc->Header->Date);
78 27
        if ($this->date === false) {
79 3
            $exception = new XmlException('The date format is wrong in xml header');
80 3
            $exception->setXmlElement($this->xmlDoc);
81 3
            throw $exception;
82
        }
83 24
        $this->path = (string)$this->xmlDoc->Header->Path;
84 24
        $this->errorCode = (int)$this->xmlDoc->Header->ErrorCode;
85 24
        $this->errorMessage = (string)$this->xmlDoc->Header->ErrorMessage;
86 24
        $this->init();
87 24
    }
88
89
    /**
90
     * @return bool
91
     */
92 12
    public function isSuccessful() : bool
93
    {
94 12
        return $this->errorCode === 0;
95
    }
96
97
    /**
98
     * @return PsrResponseInterface
99
     */
100 12
    public function getResponse() : PsrResponseInterface
101
    {
102 12
        return $this->response;
103
    }
104
105
    /**
106
     * @return string
107
     */
108 3
    public function getXml() : string
109
    {
110 3
        return $this->xml;
111
    }
112
113
    /**
114
     * @return \SimpleXMLElement
115
     */
116 3
    public function getXmlDoc() : \SimpleXMLElement
117
    {
118 3
        return $this->xmlDoc;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 3
    public function getVersion() : string
125
    {
126 3
        return $this->version;
127
    }
128
129
    /**
130
     * @return \DateTimeImmutable
131
     */
132 3
    public function getDate() : \DateTimeImmutable
133
    {
134 3
        return $this->date;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 3
    public function getPath() : string
141
    {
142 3
        return $this->path;
143
    }
144
145
    /**
146
     * @return int
147
     */
148 3
    public function getErrorCode() : int
149
    {
150 3
        return $this->errorCode;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 3
    public function getErrorMessage() : string
157
    {
158 3
        return $this->errorMessage;
159
    }
160
161
    /**
162
     * Is called after the contructor has initialized the properties
163
     * Use this to do any initialization you need
164
     */
165 3
    protected function init()
166
    {
167 3
    }
168
}
169