Completed
Push — master ( 157295...918236 )
by Sébastien
02:44
created

StoryPointBurndown::getTheoreticalBurndown()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
rs 8.5906
ccs 13
cts 14
cp 0.9286
cc 5
eloc 14
nc 5
nop 0
crap 5.009
1
<?php
2
3
namespace TrelloBurndown\Model;
4
5
use TrelloBurndown\Helper\DateHelper;
6
7
/**
8
 * Class StoryPointBurndown.
9
 */
10
class StoryPointBurndown
11
{
12
    use DateHelper;
13
    /**
14
     * @var float
15
     */
16
    private $averageSP;
17
    /**
18
     * @var array
19
     */
20
    private $doneSP;
21
    /**
22
     * @var float
23
     */
24
    private $totalSP;
25
    /**
26
     * @var Sprint
27
     */
28
    private $sprint;
29
30
    /**
31
     * @var string
32
     */
33
    private static $dateFormat = 'Y-m-d';
34
35
    /**
36
     * StoryPointBurndown constructor.
37
     *
38
     * @param Sprint $sprint
39
     * @param float  $totalSP
40
     * @param array  $doneSP
41
     * @param float  $averageSP
42
     */
43 5
    public function __construct(Sprint $sprint, float $totalSP, array $doneSP, float $averageSP)
44
    {
45 5
        $this->sprint = $sprint;
46 5
        $this->totalSP = $totalSP;
47 5
        $this->doneSP = $doneSP;
48 5
        $this->averageSP = $averageSP;
49 5
    }
50
51
    /**
52
     * @return float
53
     */
54 1
    public function getAverageSP()
55
    {
56 1
        return $this->averageSP;
57
    }
58
59
    /**
60
     * @param float $averageSP
61
     */
62 1
    public function setAverageSP($averageSP)
63
    {
64 1
        $this->averageSP = $averageSP;
65 1
    }
66
67
    /**
68
     * @return array
69
     */
70 1
    public function getDoneSP()
71
    {
72 1
        return $this->doneSP;
73
    }
74
75
    /**
76
     * @param array $doneSP
77
     */
78 1
    public function setDoneSP($doneSP)
79
    {
80 1
        $this->doneSP = $doneSP;
81 1
    }
82
83
    /**
84
     * @return float
85
     */
86 1
    public function getTotalSP()
87
    {
88 1
        return $this->totalSP;
89
    }
90
91
    /**
92
     * @param float $totalSP
93
     */
94 1
    public function setTotalSP($totalSP)
95
    {
96 1
        $this->totalSP = $totalSP;
97 1
    }
98
99
    /**
100
     * @return Sprint
101
     */
102
    public function getSprint()
103
    {
104
        return $this->sprint;
105
    }
106
107
    /**
108
     * @param Sprint $sprint
109
     */
110
    public function setSprint($sprint)
111
    {
112
        $this->sprint = $sprint;
113
    }
114
115 3
    public function formatDate(\DateTime $date)
116
    {
117 3
        return $date->format(self::$dateFormat);
118
    }
119
120
    /**
121
     * @return array
122
     */
123 2
    public function getRealBurndown()
124
    {
125 2
        $realBurndown = [];
126 2
        $total = $this->totalSP;
127 2
        $realBurndown[$this->formatDate($this->sprint->getStart())] = $total;
128 2
        foreach ($this->doneSP as $sp) {
129 2
            $total = $total - $sp['count'];
130 2
            $realBurndown[$this->formatDate($sp['date'])] = $total;
131
        }
132
133 2
        return $realBurndown;
134
    }
135
136
    /**
137
     * @return array|null
138
     */
139 2
    public function getTheoreticalBurndown()
140
    {
141 2
        $theoreticalBurndown = [];
142 2
        $theoreticalBurndown[$this->sprint->getStart()->format('Y-m-d')] = $this->totalSP;
143
144 2
        $sprintDays = $this->sprint->getSprintDays();
145 2
        if (!$sprintDays instanceof \DatePeriod) {
146
            return;
147
        }
148
149 2
        foreach ($sprintDays as $day) {
150 2
            if ($this->isWeekend($day)) {
151 2
                continue;
152
            }
153
154 2
            $rest = end($theoreticalBurndown) != false ? end($theoreticalBurndown) : $this->totalSP;
155 2
            $formatedDate = $this->formatDate($day);
156 2
            $doneSP = $rest - $this->averageSP;
157 2
            $theoreticalBurndown[$formatedDate] = round($doneSP, 2);
158
        }
159
160 2
        return $theoreticalBurndown;
161
    }
162
163
    /**
164
     * @return array
165
     */
166 1
    public function generate()
167
    {
168 1
        $real = $this->getRealBurndown();
169 1
        $ideal = $this->getTheoreticalBurndown();
170
171 1
        return ['real' => $real, 'theorical' => $ideal];
172
    }
173
}
174