Passed
Push — master ( 5ee49d...ede548 )
by Alessandro
01:05 queued 11s
created

CycleTimeCalculator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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