XmlWriter::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace TreeHouse\Feeder\Writer;
4
5
use XmlWriter as BaseXmlWriter;
6
7
class XmlWriter implements WriterInterface
8
{
9
    /**
10
     * @var \SplFileObject
11
     */
12
    protected $file;
13
14
    /**
15
     * @var BaseXmlWriter
16
     */
17
    protected $writer;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $indent = false;
23
24
    /**
25
     * @var string
26
     */
27
    protected $rootNode = 'feed';
28
29
    /**
30
     * @inheritdoc
31
     */
32 14
    public function __construct(\SplFileObject $file = null)
33
    {
34 14
        $this->file = $file;
35 14
    }
36
37
    /**
38
     * The clone magic method.
39
     */
40
    public function __clone()
41
    {
42
        $this->file = null;
43
        $this->writer = null;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function setFile(\SplFileObject $file)
50
    {
51
        $this->file = $file;
52
    }
53
54
    /**
55
     * @param bool $ident
56
     */
57
    public function setIndent($ident)
58
    {
59
        $this->indent = (boolean) $ident;
60
    }
61
62
    /**
63
     * @param string $node
64
     */
65 2
    public function setRootNode($node)
66
    {
67 2
        $this->rootNode = $node;
68 2
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 8
    public function start()
74
    {
75 8
        if (!$this->file) {
76 2
            throw new \LogicException('Set a file first');
77
        }
78
79 6
        if ($this->writer) {
80 2
            throw new \LogicException('Writer already started');
81
        }
82
83 6
        $this->writer = new BaseXmlWriter();
84 6
        $this->writer->openUri($this->file->getPathname());
85 6
        $this->writer->setIndent($this->indent);
86 6
        $this->writer->startDocument('1.0', 'UTF-8');
87 6
        $this->write(sprintf('<%s>', $this->rootNode));
88 6
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 8
    public function write($data)
94
    {
95 8
        if (!$this->writer) {
96 2
            throw new \LogicException('Start writer first');
97
        }
98
99 6
        $this->writer->writeRaw($data);
100 6
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 6
    public function flush()
106
    {
107 6
        if (!$this->writer) {
108 2
            throw new \LogicException('Start writer first');
109
        }
110
111 4
        $this->writer->flush();
112 4
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 6
    public function end()
118
    {
119 6
        if (!$this->writer) {
120 2
            throw new \LogicException('Start writer first');
121
        }
122
123 4
        $this->write(sprintf('</%s>', $this->rootNode));
124 4
        $this->writer->endDocument();
125 4
        $this->flush();
126
127 4
        $this->writer = null;
128 4
    }
129
}
130