PohodaResponse::open()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 45
c 0
b 0
f 0
ccs 21
cts 21
cp 1
rs 8.9457
cc 6
nc 17
nop 6
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis;
6
7
use kalanis\Pohoda\AbstractAgenda;
8
9
/**
10
 * Factory for Pohoda objects.
11
 */
12
class PohodaResponse extends Pohoda
13
{
14
    /**
15
     * Open new XML file for writing.
16
     *
17
     * @param string|null $filename path to output file or null for memory
18
     * @param string      $id
19
     * @param string      $note
20
     * @param string      $state
21
     * @param string|null $programVersion
22
     * @param string|null $key
23
     *
24
     * @return bool
25
     */
26 4
    public function open(
27
        ?string $filename,
28
        string $id,
29
        string $note = '',
30
        string $state = 'ok',
31
        ?string $programVersion = null,
32
        ?string $key = null,
33
    ): bool {
34 4
        $this->xmlWriter = new \XMLWriter();
35
36 4
        if (is_null($filename)) {
37 3
            $this->isInMemory = true;
38 3
            $this->xmlWriter->openMemory();
39
        } else {
40 1
            $this->isInMemory = false;
41
42 1
            if (!$this->xmlWriter->openUri($filename)) {
43
                // @codeCoverageIgnoreStart
44
                // I cannot test this, because it needs source file somewhere online
45
                return false;
46
            }
47
            // @codeCoverageIgnoreEnd
48
        }
49
50 4
        $this->xmlWriter->startDocument('1.0', $this->dependenciesFactory->getSanitizeEncoding()->getEncoding());
51 4
        $this->xmlWriter->startElementNs('rsp', 'responsePack', null);
52
53 4
        $this->xmlWriter->writeAttribute('id', $id);
54 4
        $this->xmlWriter->writeAttribute('ico', $this->companyRegistrationNumber->getCompanyNumber());
55 4
        $this->xmlWriter->writeAttribute('version', '2.0');
56 4
        $this->xmlWriter->writeAttribute('note', $note);
57 4
        $this->xmlWriter->writeAttribute('state', $state);
58 4
        if (!is_null($programVersion)) {
59 1
            $this->xmlWriter->writeAttribute('programVersion', $programVersion);
60
        }
61 4
        if (!is_null($key)) {
62 1
            $this->xmlWriter->writeAttribute('key', $key);
63
        }
64
65 4
        foreach ($this->dependenciesFactory->getNamespacePaths()->allNamespaces() as $k => $v) {
66
            // put all known namespaces into base element attributes
67 4
            $this->xmlWriter->writeAttributeNs('xmlns', $k, null, $v);
68
        }
69
70 4
        return true;
71
    }
72
73
    /**
74
     * Add item.
75
     *
76
     * @param string $id
77
     * @param AbstractAgenda $agenda
78
     * @param string $state
79
     *
80
     * @return void
81
     */
82 4
    public function addItem(string $id, AbstractAgenda $agenda, string $state = 'ok'): void
83
    {
84 4
        $this->xmlWriter->startElementNs('rsp', 'responsePackItem', null);
85
86 4
        $this->xmlWriter->writeAttribute('id', $id);
87 4
        $this->xmlWriter->writeAttribute('version', '2.0');
88 4
        $this->xmlWriter->writeAttribute('state', $state);
89 4
        $this->xmlWriter->writeRaw((string) $agenda->getXML()->asXML());
90 4
        $this->xmlWriter->endElement();
91
92 4
        if (!$this->isInMemory) {
93 1
            $this->xmlWriter->flush();
94
        }
95
    }
96
}
97