Completed
Push — develop ( 8a0cb6...d5d9d0 )
by Tom
04:26
created

JUnitSession   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 76
wmc 9
lcom 2
cbo 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDocument() 0 8 2
A getName() 0 4 1
A getDuration() 0 8 2
A addTestSuite() 0 4 1
A save() 0 8 2
1
<?php
2
/*
3
 * @author Tom Klingenberg <https://github.com/ktomk>
4
 */
5
6
namespace N98\Util;
7
8
use N98\JUnitXml\Document as JUnitXmlDocument;
9
use N98\JUnitXml\TestSuiteElement;
10
11
/**
12
 * Helper class as companion for JUnitXmlDocument based logging
13
 *
14
 * @see JUnitXmlDocument
15
 */
16
class JUnitSession
17
{
18
    /**
19
     * @var JUnitXmlDocument
20
     */
21
    private $document;
22
23
    /**
24
     * @var float
25
     */
26
    private $starTime;
27
    private $stopTime;
28
29
    /**
30
     * @var string
31
     */
32
    private $name;
33
34
    public function __construct($name)
35
    {
36
        $this->starTime = microtime(true);
37
        $this->name = $name;
38
    }
39
40
    /**
41
     * getter for JUnitXmlDocument associated wit this session
42
     *
43
     * @return JUnitXmlDocument
44
     */
45
    public function getDocument()
46
    {
47
        if (!$this->document) {
48
            $this->document = new JUnitXmlDocument();
49
        }
50
51
        return $this->document;
52
    }
53
54
    public function getName()
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * @return float
61
     */
62
    public function getDuration()
63
    {
64
        if (null === $this->stopTime) {
65
            $this->stopTime = microtime(true);
66
        }
67
68
        return $this->stopTime - $this->starTime;
69
    }
70
71
    /**
72
     * @return TestSuiteElement
73
     */
74
    public function addTestSuite()
75
    {
76
        return $this->getDocument()->addTestSuite();
77
    }
78
79
    /**
80
     * @param string $path
81
     * @return int|false the number of bytes written or false if an error occured
82
     */
83
    public function save($path)
84
    {
85
        if (!$this->document) {
86
            return 0;
87
        }
88
89
        return $this->document->save($path);
90
    }
91
}
92