Completed
Pull Request — master (#31)
by Alessandro
05:11 queued 03:19
created

CycleTimeCalculator::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 3
crap 3.0261
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
        foreach ($this->timeCards as $timeCard) {
48
            $this->execute($timeCard, $from, $to);
49
        }
50
    }
51
52 3
    private function execute(TimeCard $timeCard, string $from, string $to)
53
    {
54 3
        $fromDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $from);
55 3
        $toDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $to);
56
57 3
        if ($fromDate === null || $toDate === null) {
58
            return;
59
        }
60
61 3
        $timeCard->calculateDayDifferenceBetweenColumns($from, $fromDate, $to, $toDate);
62 3
    }
63
64 4
    public function getTimeCards(): array
65
    {
66 4
        return json_decode(json_encode($this->timeCards), true);
67
    }
68
}