PohodaResponse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 31
c 2
b 0
f 0
dl 0
loc 82
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B open() 0 45 6
A addItem() 0 12 2
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(
34
        ?string $filename,
35
        string $id,
36
        string $note = '',
37
        string $state = 'ok',
38
        ?string $programVersion = null,
39
        ?string $key = null,
40
    ): bool {
41 4
        $this->xmlWriter = new \XMLWriter();
42
43 4
        if (is_null($filename)) {
44 3
            $this->isInMemory = true;
45 3
            $this->xmlWriter->openMemory();
46
        } else {
47 1
            $this->isInMemory = false;
48
49 1
            if (!$this->xmlWriter->openUri($filename)) {
50
                // @codeCoverageIgnoreStart
51
                // I cannot test this, because it needs source file somewhere online
52
                return false;
53
            }
54
            // @codeCoverageIgnoreEnd
55
        }
56
57 4
        $this->xmlWriter->startDocument('1.0', $this->dependenciesFactory->getSanitizeEncoding()->getEncoding());
58 4
        $this->xmlWriter->startElementNs('rsp', 'responsePack', null);
59
60 4
        $this->xmlWriter->writeAttribute('id', $id);
61 4
        $this->xmlWriter->writeAttribute('ico', $this->companyRegistrationNumber->getCompanyNumber());
62 4
        $this->xmlWriter->writeAttribute('version', '2.0');
63 4
        $this->xmlWriter->writeAttribute('note', $note);
64 4
        $this->xmlWriter->writeAttribute('state', $state);
65 4
        if (!is_null($programVersion)) {
66 1
            $this->xmlWriter->writeAttribute('programVersion', $programVersion);
67
        }
68 4
        if (!is_null($key)) {
69 1
            $this->xmlWriter->writeAttribute('key', $key);
70
        }
71
72 4
        foreach ($this->dependenciesFactory->getNamespacePaths()->allNamespaces() as $k => $v) {
73
            // put all known namespaces into base element attributes
74 4
            $this->xmlWriter->writeAttributeNs('xmlns', $k, null, $v);
75
        }
76
77 4
        return true;
78
    }
79
80
    /**
81
     * Add item.
82
     *
83
     * @param string $id
84
     * @param AbstractAgenda $agenda
85
     * @param string $state
86
     *
87
     * @return void
88
     */
89 4
    public function addItem(string $id, AbstractAgenda $agenda, string $state = 'ok'): void
90
    {
91 4
        $this->xmlWriter->startElementNs('rsp', 'responsePackItem', null);
92
93 4
        $this->xmlWriter->writeAttribute('id', $id);
94 4
        $this->xmlWriter->writeAttribute('version', '2.0');
95 4
        $this->xmlWriter->writeAttribute('state', $state);
96 4
        $this->xmlWriter->writeRaw((string) $agenda->getXML()->asXML());
97 4
        $this->xmlWriter->endElement();
98
99 4
        if (!$this->isInMemory) {
100 1
            $this->xmlWriter->flush();
101
        }
102
    }
103
}
104