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
|
7 |
|
public function __construct(array $timeCards, HistoryCards $historyCards) |
23
|
|
|
{ |
24
|
7 |
|
$this->timeCards = $timeCards; |
25
|
7 |
|
$this->historyCards = $historyCards; |
26
|
7 |
|
} |
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
|
2 |
|
public function calculateWithStaticFromAndTo(?string $from, ?string $to) |
46
|
|
|
{ |
47
|
2 |
|
if ($from === null || $to === null) { |
48
|
1 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
foreach ($this->timeCards as $timeCard) { |
52
|
1 |
|
$this->execute($timeCard, $from, $to); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
4 |
|
private function execute(TimeCard $timeCard, string $from, string $to) |
57
|
|
|
{ |
58
|
4 |
|
$fromDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $from); |
59
|
4 |
|
$toDate = $this->historyCards->getByCardIdAndTo($timeCard->getId(), $to); |
60
|
|
|
|
61
|
4 |
|
if ($fromDate === null || $toDate === null) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$timeCard->calculateDayDifferenceBetweenColumns($from, $fromDate, $to, $toDate); |
66
|
4 |
|
} |
67
|
|
|
|
68
|
4 |
|
public function getTimeCards(): array |
69
|
|
|
{ |
70
|
4 |
|
return json_decode(json_encode($this->timeCards), true); |
71
|
|
|
} |
72
|
|
|
} |