1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report\Xml;
12
13class File
14{
15    /**
16     * @var \DOMDocument
17     */
18    protected $dom;
19
20    /**
21     * @var \DOMElement
22     */
23    protected $contextNode;
24
25    public function __construct(\DOMElement $context)
26    {
27        $this->dom         = $context->ownerDocument;
28        $this->contextNode = $context;
29    }
30
31    public function getTotals()
32    {
33        $totalsContainer = $this->contextNode->firstChild;
34
35        if (!$totalsContainer) {
36            $totalsContainer = $this->contextNode->appendChild(
37                $this->dom->createElementNS(
38                    'http://schema.phpunit.de/coverage/1.0',
39                    'totals'
40                )
41            );
42        }
43
44        return new Totals($totalsContainer);
45    }
46
47    public function getLineCoverage($line)
48    {
49        $coverage = $this->contextNode->getElementsByTagNameNS(
50            'http://schema.phpunit.de/coverage/1.0',
51            'coverage'
52        )->item(0);
53
54        if (!$coverage) {
55            $coverage = $this->contextNode->appendChild(
56                $this->dom->createElementNS(
57                    'http://schema.phpunit.de/coverage/1.0',
58                    'coverage'
59                )
60            );
61        }
62
63        $lineNode = $coverage->appendChild(
64            $this->dom->createElementNS(
65                'http://schema.phpunit.de/coverage/1.0',
66                'line'
67            )
68        );
69
70        return new Coverage($lineNode, $line);
71    }
72}
73