Passed
Push — master ( 09b005...3754d7 )
by Petr
13:20 queued 20s
created

PohodaResponse::addItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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