PohodaResponse::open()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

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