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

XMLFile   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 108
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setData() 0 7 2
A getPathname() 0 11 2
A getXml() 0 8 2
A isReady() 0 4 1
A createEmptyXml() 0 10 1
A setBody() 0 22 5
A endDocument() 0 10 2
A exceptionFirstCallSetData() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio\Request\Pay;
4
5
use h4kuna\Fio\Exceptions\InvalidState;
6
use h4kuna\Fio\Request\Pay\Payment;
7
use XMLWriter;
8
9
class XMLFile
10
{
11
12
	/** @var XMLWriter|null */
13
	private $xml;
14
15
	/** @var string */
16
	private $temp;
17
18
19
	public function __construct(string $temp)
20
	{
21
		$this->temp = $temp;
22
	}
23
24
25
	public function setData(Payment\Property $data): self
26
	{
27
		if ($this->isReady() === false) {
28
			$this->createEmptyXml();
29
		}
30
		return $this->setBody($data);
31
	}
32
33
34
	public function getPathname(bool $keepFile = false): string
35
	{
36
		$filename = $this->temp . DIRECTORY_SEPARATOR . md5((string) microtime(true)) . '.xml';
37
		file_put_contents($filename, $this->getXml());
38
		if ($keepFile === false) {
39
			register_shutdown_function(function () use ($filename) {
40
				@unlink($filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
41
			});
42
		}
43
		return $filename;
44
	}
45
46
47
	public function getXml(): string
48
	{
49
		if ($this->isReady() === false) {
50
			throw new InvalidState('You can read only onetime.');
51
		}
52
53
		return $this->endDocument();
54
	}
55
56
57
	public function isReady(): bool
58
	{
59
		return $this->xml !== null;
60
	}
61
62
63
	private function createEmptyXml(): void
64
	{
65
		$this->xml = new XMLWriter;
66
		$this->xml->openMemory();
67
		$this->xml->startDocument('1.0', 'UTF-8');
68
		$this->xml->startElement('Import');
69
		$this->xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
70
		$this->xml->writeAttribute('xsi:noNamespaceSchemaLocation', 'http://www.fio.cz/schema/importIB.xsd');
71
		$this->xml->startElement('Orders');
72
	}
73
74
75
	private function setBody(Payment\Property $data): self
76
	{
77
		if ($this->xml === null) {
78
			throw self::exceptionFirstCallSetData();
79
		}
80
		$elements = $data->getExpectedProperty();
81
		$this->xml->startElement($data->getStartXmlElement());
82
		foreach ($data as $node => $value) {
83
			if ($value == false) { // intentionally ==
84
				if ($elements[$node] === false) {
85
					continue;
86
				}
87
				$value = '';
88
			}
89
90
			$this->xml->startElement($node);
91
			$this->xml->text((string) $value);
92
			$this->xml->endElement();
93
		}
94
		$this->xml->endElement();
95
		return $this;
96
	}
97
98
99
	private function endDocument(): string
100
	{
101
		if ($this->xml === null) {
102
			throw self::exceptionFirstCallSetData();
103
		}
104
		$this->xml->endDocument();
105
		$xml = $this->xml->outputMemory();
106
		$this->xml = null;
107
		return $xml;
108
	}
109
110
111
	private static function exceptionFirstCallSetData(): InvalidState
112
	{
113
		return new InvalidState('First you must call setData().');
114
	}
115
116
}
117