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

StoryPointManager   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 98.11%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 22
c 4
b 2
f 2
lcom 1
cbo 6
dl 0
loc 168
rs 10
ccs 52
cts 53
cp 0.9811

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A setPattern() 0 4 1
A getPattern() 0 4 1
A parseStoryPoints() 0 6 2
A countListStoryPoints() 0 13 3
D getDoneStoryPoints() 0 36 9
A getTotalSprintStoryPoints() 0 19 3
A getAverageStoryPointsPerDay() 0 4 1
1
<?php
2
3
namespace TrelloBurndown\Manager;
4
5
use Trello\Model\Cardlist;
6
use TrelloBurndown\Client\TrelloClient;
7
use TrelloBurndown\Helper\DateHelper;
8
use TrelloBurndown\Model\Sprint;
9
10
/**
11
 * Class StoryPointManager.
12
 */
13
class StoryPointManager
14
{
15
    use DateHelper;
16
    /**
17
     * @var ActionManager
18
     */
19
    private $actionManager;
20
21
    /**
22
     * @var string
23
     */
24
    private $pattern = '/\(([0-9]*.?[0-9]*)\)/';
25
26
    /**
27
     * @var \Trello\Client
28
     */
29
    private $client;
30
31
    /**
32
     * StoryPointsManager constructor.
33
     *
34
     * @param TrelloClient  $trelloClient
35
     * @param ActionManager $actionManager
36
     * @param string        $pattern
37
     */
38 25
    public function __construct(TrelloClient $trelloClient, ActionManager $actionManager, String $pattern = null)
39
    {
40 25
        $this->client = $trelloClient->getClient();
41 25
        $this->actionManager = $actionManager;
42
43 25
        if (!is_null($pattern)) {
44 1
            $this->pattern = $pattern;
45
        }
46 25
    }
47
48
    /**
49
     * @param string $pattern
50
     */
51 1
    public function setPattern(String $pattern)
52
    {
53 1
        $this->pattern = $pattern;
54 1
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getPattern()
60
    {
61 1
        return $this->pattern;
62
    }
63
64
    /**
65
     * @param $name
66
     *
67
     * @return float|int
68
     */
69 13
    public function parseStoryPoints($name)
70
    {
71 13
        preg_match($this->pattern, $name, $matches);
72
73 13
        return isset($matches[1]) ? floatval($matches[1]) : 0;
74
    }
75
76
    /**
77
     * @param Cardlist $list
78
     *
79
     * @return float|int
80
     */
81 4
    public function countListStoryPoints(Cardlist $list)
82
    {
83 4
        $cards = $this->client->api('list')->cards()->all($list->getId());
84 4
        $totalSP = 0;
85
86 4
        foreach ($cards as $card) {
87 4
            if (!$card['closed']) {
88 4
                $totalSP += $this->parseStoryPoints($card['name']);
89
            }
90
        }
91
92 4
        return $totalSP;
93
    }
94
95
    /**
96
     * Return an array of done story points per day.
97
     *
98
     * @param array  $todoLists
99
     * @param array  $wipLists
100
     * @param array  $doneLists
101
     * @param Sprint $sprint
102
     *
103
     * @return array|null
104
     */
105 4
    public function getDoneStoryPoints(array $todoLists, array $wipLists, array $doneLists, Sprint $sprint)
106
    {
107 4
        $doneCards = $this->actionManager->getCardsMovedFromTodoToDone($todoLists, $wipLists, $doneLists);
108 4
        $sprintDays = $sprint->getSprintDays();
109 4
        $sp = [];
110
111 4
        if (!$sprintDays instanceof \DatePeriod) {
112
            return;
113
        }
114
115 4
        foreach ($sprintDays as $day) {
116 4
            $countSP = 0;
117 4
            if ($day instanceof \DateTime &&
118 4
                ($day->getTimestamp() > $sprint->getNextDayInSprint()->getTimestamp())) {
119 4
                break;
120
            }
121
122 4
            if ($this->isWeekend($day)) {
123 4
                continue;
124
            }
125
126 4
            foreach ($doneCards as $card) {
127 4
                $actionDate = new \DateTime($card['date']);
128
                if (
129 4
                    $actionDate->getTimestamp() > $sprint->getStart()->getTimestamp() &&
130 4
                    $actionDate->getTimestamp() < $day->getTimestamp()
131
                ) {
132 4
                    $countSP += $this->parseStoryPoints($card['card']);
133
                }
134
            }
135
136 4
            $sp[] = ['date' => $day, 'count' => $countSP];
137
        }
138
139 4
        return $sp;
140
    }
141
142
    /**
143
     * @param array  $todoLists
144
     * @param array  $wipLists
145
     * @param array  $doneLists
146
     * @param Sprint $sprint
147
     *
148
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
149
     */
150 3
    public function getTotalSprintStoryPoints(array $todoLists, array $wipLists, array $doneLists, Sprint $sprint)
151
    {
152 3
        $todoSP = 0;
153 3
        $wipSP = 0;
154
155 3
        foreach ($todoLists as $list) {
156 3
            $todoSP += $this->countListStoryPoints($list);
157
        }
158
159 3
        foreach ($wipLists as $list) {
160 3
            $wipSP += $this->countListStoryPoints($list);
161
        }
162
163 3
        $doneSP = $this->getDoneStoryPoints($todoLists, $wipLists, $doneLists, $sprint);
164
165 3
        $doneSP = end($doneSP);
166
167 3
        return $todoSP + $wipSP + $doneSP['count'];
168
    }
169
170
    /**
171
     * @param $totalOfSprint
172
     * @param Sprint $sprint
173
     *
174
     * @return float
175
     */
176 2
    public function getAverageStoryPointsPerDay($totalOfSprint, Sprint $sprint)
177
    {
178 2
        return round($totalOfSprint / $sprint->getTotalWorkDays(), 2);
179
    }
180
}
181