Completed
Push — master ( 589fec...539028 )
by Milan
07:58
created

XMLResponse   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 85
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isOk() 0 4 2
A getXml() 0 4 1
A code() 0 4 1
A getIdInstruction() 0 4 1
A status() 0 4 1
A errorMessages() 0 16 4
A getValue() 0 5 1
A saveXML() 0 4 1
A __toString() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio\Response\Pay;
4
5
use SimpleXMLElement;
6
7
class XMLResponse implements IResponse
8
{
9
10
	/** @var SimpleXMLElement */
11
	private $xml;
12
13
14
	public function __construct(string $xml)
15
	{
16
		$this->xml = new SimpleXMLElement($xml);
17
	}
18
19
20
	public function isOk(): bool
21
	{
22
		return $this->status() === 'ok' && $this->code() === 0;
23
	}
24
25
26
	/**
27
	 * READ XML ****************************************************************
28
	 * *************************************************************************
29
	 */
30
31
	public function getXml(): SimpleXMLElement
32
	{
33
		return $this->xml;
34
	}
35
36
37
	public function code(): int
38
	{
39
		return (int) $this->getValue('result/errorCode');
40
	}
41
42
43
	public function getIdInstruction()
44
	{
45
		return $this->getValue('result/idInstruction');
46
	}
47
48
49
	public function status(): string
50
	{
51
		return $this->getValue('result/status');
52
	}
53
54
55
	public function errorMessages(): array
56
	{
57
		$errorMessages = [];
58
		/** @var SimpleXMLElement[] $messages */
59
		$messages = $this->getXml()->xpath('ordersDetails/detail/messages');
60
		foreach ($messages as $message) {
61
			foreach ($message as $item) {
62
				if (isset($item['errorCode'])) {
63
					$errorMessages[(string) $item['errorCode']] = (string) $item;
64
				} else {
65
					$errorMessages[] = (string) $item;
66
				}
67
			}
68
		}
69
		return $errorMessages;
70
	}
71
72
73
	private function getValue(string $path): string
74
	{
75
		$val = $this->getXml()->xpath($path . '/text()');
76
		return (string) $val[0];
77
	}
78
79
80
	public function saveXML(string $fileName): void
81
	{
82
		$this->getXml()->saveXML($fileName);
83
	}
84
85
86
	public function __toString()
87
	{
88
		return (string) $this->xml->asXML();
89
	}
90
91
}
92