Completed
Push — master ( c011fc...384d9c )
by Milan
01:49
created

src/Request/Pay/XMLFile.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace h4kuna\Fio\Request\Pay;
4
5
use h4kuna\Fio\Request\Pay\Payment,
6
	XMLWriter;
7
8
/**
9
 * @author Milan Matějček
10
 */
11
class XMLFile
12
{
13
14
	/** @var XMLWriter */
15
	private $xml;
16
17
	/** @var string */
18
	private $content = true;
19
20
	/** @var string */
21
	private $temp;
22
23
	public function __construct($temp)
24
	{
25
		$this->temp = $temp;
26
	}
27
28
	/**
29
	 * @param Payment\Property $data
30
	 * @return self
31
	 */
32
	public function setData(Payment\Property $data)
33
	{
34
		if ($this->content) {
35
			$this->createEmptyXml();
36
		}
37
		return $this->setBody($data);
38
	}
39
40
	/** @return string */
41
	public function getPathname()
42
	{
43
		$filename = $this->temp . DIRECTORY_SEPARATOR . md5(microtime(true)) . '.xml';
44
		file_put_contents($filename, $this->getXml());
45
		register_shutdown_function(function () use ($filename) {
46
			@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...
47
		});
48
		return $filename;
49
	}
50
51
	/** @return string XML */
52
	public function getXml()
53
	{
54
		if ($this->content) {
55
			return $this->content;
56
		}
57
58
		return $this->content = $this->endDocument();
59
	}
60
61
	/** @return bool */
62
	public function isReady()
63
	{
64
		return $this->content === null;
65
	}
66
67
	/**
68
	 * Prepare XML.
69
	 */
70
	private function createEmptyXml()
71
	{
72
		$this->xml = new XMLWriter;
73
		$this->xml->openMemory();
74
		$this->xml->startDocument('1.0', 'UTF-8');
75
		$this->xml->startElement('Import');
76
		$this->xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
77
		$this->xml->writeAttribute('xsi:noNamespaceSchemaLocation', 'http://www.fio.cz/schema/importIB.xsd');
78
		$this->xml->startElement('Orders');
79
		$this->content = null;
80
	}
81
82
	private function setBody(Payment\Property $data)
83
	{
84
		$this->xml->startElement($data->getStartXmlElement());
85
		foreach ($data as $node => $value) {
86
			if ($value === false) {
87
				continue;
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
	private function endDocument()
99
	{
100
		$this->xml->endDocument();
101
		return $this->xml->outputMemory();
102
	}
103
104
}
105