Completed
Pull Request — master (#31)
by Alessandro
07:44
created

CycleTimeCalculator::calculateFromCardHistory()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.0155

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 0
crap 8.0155
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TrelloCycleTime;
6
7
8
use TrelloCycleTime\Collection\HistoryCards;
9
10
class CycleTimeCalculator
11
{
12
    /**
13
     * @var array
14
     */
15
    private $timeCards;
16
    /**
17
     * @var HistoryCards
18
     */
19
    private $historyCards;
20
21 5
    public function __construct(array $timeCards, HistoryCards $historyCards)
22
    {
23 5
        $this->timeCards = $timeCards;
24 5
        $this->historyCards = $historyCards;
25 5
    }
26
27 5
    public function calculateFromCardHistory()
28
    {
29 5
        $cardHistoryCollection = $this->historyCards->getCardHistories();
30 5
        foreach ($cardHistoryCollection as $cardHistory) {
31 3
            foreach ($this->timeCards as $timeCard) {
32 3
                if ($timeCard->getId() !== $cardHistory->getId() ||
33 3
                    $cardHistory->getFrom() === null ||
34 3
                    $cardHistory->getTo() === null) {
35 2
                    continue;
36
                }
37
38 3
                $fromDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $cardHistory->getFrom());
39 3
                $toDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $cardHistory->getTo());
40
41 3
                if ($fromDate === null || $toDate === null) {
42
                    continue;
43
                }
44
45 3
                $timeCard->calculateDayDifferenceBetweenColumns(
46 3
                    $cardHistory->getFrom(),
47
                    $fromDate,
48 3
                    $cardHistory->getTo(),
49
                    $toDate
50
                );
51
            }
52
        }
53 5
    }
54
55
    public function calculateWithStaticFromAndTo(string $from, string $to)
56
    {
57
        foreach ($this->timeCards as $timeCard) {
58
            $fromDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $from);
59
            $toDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $to);
60
61
            if ($fromDate === null || $toDate === null) {
62
                continue;
63
            }
64
65
            $timeCard->calculateDayDifferenceBetweenColumns(
66
                $from,
67
                $fromDate,
68
                $to,
69
                $toDate
70
            );
71
        }
72
    }
73
74 4
    public function getTimeCards(): array
75
    {
76 4
        return json_decode(json_encode($this->timeCards), true);
77
    }
78
}