XmlWriter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 123
ccs 35
cts 45
cp 0.7778
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __clone() 0 5 1
A setFile() 0 4 1
A setIndent() 0 4 1
A setRootNode() 0 4 1
A start() 0 16 3
A write() 0 8 2
A flush() 0 8 2
A end() 0 12 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