Completed
Push — master ( abe4a2...2abbc3 )
by Sam
05:40
created

InputErrorCumulative   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getCreated() 0 4 1
A accumulate() 0 7 1
A getAverageValue() 0 9 2
A getLastValue() 0 4 1
A addValue() 0 4 1
1
<?php
2
3
namespace Jalle19\StatusManager\Instance;
4
5
use Jalle19\tvheadend\model\InputStatus;
6
7
/**
8
 * Class InputErrorCumulative
9
 * @package   Jalle19\StatusManager\Instance
10
 * @copyright Copyright &copy; Sam Stenvall 2016-
11
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
12
 */
13
class InputErrorCumulative
14
{
15
16
	const VALUE_TYPE_BER = 'ber';
17
	const VALUE_TYPE_UNC = 'unc';
18
	const VALUE_TYPE_TE  = 'te';
19
	const VALUE_TYPE_CC  = 'cc';
20
21
	/**
22
	 * @var \DateTime
23
	 */
24
	private $_created;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $_counters;
30
31
32
	/**
33
	 * Initializes all value counters
34
	 */
35
	public function __construct()
36
	{
37
		$this->_created = new \DateTime();
38
39
		foreach ([self::VALUE_TYPE_BER, self::VALUE_TYPE_UNC, self::VALUE_TYPE_TE, self::VALUE_TYPE_CC] as $type)
40
			$this->_counters[$type] = [];
41
	}
42
43
44
	/**
45
	 * @return \DateTime
46
	 */
47
	public function getCreated()
48
	{
49
		return $this->_created;
50
	}
51
52
53
	/**
54
	 * @param InputStatus $inputStatus
55
	 */
56
	public function accumulate(InputStatus $inputStatus)
57
	{
58
		$this->addValue(InputErrorCumulative::VALUE_TYPE_BER, $inputStatus->ber);
59
		$this->addValue(InputErrorCumulative::VALUE_TYPE_UNC, $inputStatus->unc);
60
		$this->addValue(InputErrorCumulative::VALUE_TYPE_TE, $inputStatus->te);
61
		$this->addValue(InputErrorCumulative::VALUE_TYPE_CC, $inputStatus->cc);
62
	}
63
64
65
	/**
66
	 * @param string $type
67
	 *
68
	 * @return float
69
	 */
70
	public function getAverageValue($type)
71
	{
72
		$count = count($this->_counters[$type]);
73
74
		if ($count === 0)
75
			return 0;
76
77
		return array_sum($this->_counters[$type]) / $count;
78
	}
79
80
81
	/**
82
	 * @param string $type
83
	 *
84
	 * @return mixed
85
	 */
86
	public function getLastValue($type)
87
	{
88
		return end($this->_counters[$type]);
89
	}
90
91
92
	/**
93
	 * @param string $type
94
	 * @param int    $value
95
	 */
96
	private function addValue($type, $value)
97
	{
98
		$this->_counters[$type][] = $value;
99
	}
100
101
}
102