1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TrelloCycleTime\Collection; |
6
|
|
|
|
7
|
|
|
use TrelloCycleTime\Client\TrelloApiClient; |
8
|
|
|
use TrelloCycleTime\Filter; |
9
|
|
|
use TrelloCycleTime\ValueObject\HistoryCard; |
10
|
|
|
|
11
|
|
|
class HistoryCards |
12
|
|
|
{ |
13
|
|
|
private $cardHistories; |
14
|
|
|
/** |
15
|
|
|
* @var Filter |
16
|
|
|
*/ |
17
|
|
|
private $filter; |
18
|
|
|
|
19
|
5 |
|
private function __construct(TrelloApiClient $client, array $cardsId, Filter $filter) |
20
|
|
|
{ |
21
|
5 |
|
$this->cardHistories = []; |
22
|
5 |
|
$creationCards = []; |
23
|
5 |
|
$historyCards = []; |
24
|
5 |
|
$this->filter = $filter; |
25
|
|
|
|
26
|
5 |
|
foreach ($cardsId as $cardId) { |
27
|
3 |
|
sleep(5); |
28
|
|
|
|
29
|
3 |
|
$creationCard = $client->findCreationCard($cardId->getId()); |
30
|
3 |
|
$creationCards = array_merge($creationCards, $creationCard); |
31
|
|
|
|
32
|
3 |
|
$historyCard = $client->findAllCardHistory($cardId->getId()); |
33
|
3 |
|
$historyCards = array_merge($historyCards, $historyCard); |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
$this->createFromArray($historyCards); |
37
|
5 |
|
$this->addCreationCards($creationCards); |
38
|
5 |
|
} |
39
|
|
|
|
40
|
5 |
|
public static function createFromCards(TrelloApiClient $client, array $cards, Filter $filter) |
41
|
|
|
{ |
42
|
5 |
|
return new self($client, $cards, $filter); |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
private function createFromArray(array $cardHistoryData) |
46
|
|
|
{ |
47
|
5 |
|
foreach ($cardHistoryData as $histories) { |
48
|
3 |
|
if ([] === $histories) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$this->addHistoryCard(HistoryCard::createFromArray($histories)); |
53
|
|
|
} |
54
|
5 |
|
} |
55
|
|
|
|
56
|
5 |
|
private function addCreationCards(array $cardCreationData) |
57
|
|
|
{ |
58
|
5 |
|
foreach ($cardCreationData as $creation) { |
59
|
2 |
|
if ([] === $creation) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$this->addHistoryCard(HistoryCard::createFromCreationArray($creation)); |
64
|
|
|
} |
65
|
5 |
|
} |
66
|
|
|
|
67
|
3 |
|
private function addHistoryCard(HistoryCard $historyCard) :void |
68
|
|
|
{ |
69
|
3 |
|
if (!$this->isInFromDateRange($historyCard) || !$this->isInToDateRange($historyCard)) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$this->cardHistories[] = $historyCard; |
74
|
3 |
|
} |
75
|
|
|
|
76
|
3 |
|
private function isInFromDateRange(HistoryCard $historyCard) :bool |
77
|
|
|
{ |
78
|
3 |
|
if ($this->filter->isFromDateEnabled()) { |
79
|
|
|
if ($this->filter->getFromDate() === null) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (strtotime($historyCard->getDate()) < strtotime($this->filter->getFromDate() ?? '1970-01-01')) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
private function isInToDateRange(HistoryCard $historyCard) :bool |
92
|
|
|
{ |
93
|
3 |
|
if ($this->filter->isToDateEnabled()) { |
94
|
|
|
if ($this->filter->getToDate() === null) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (strtotime($historyCard->getDate()) > strtotime($this->filter->getToDate() ?? '1970-01-01')) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $cardId |
108
|
|
|
* @param string $to |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
2 |
|
public function getByCardIdAndTo(string $cardId, string $to): ?string |
112
|
|
|
{ |
113
|
2 |
|
foreach ($this->cardHistories as $history) { |
114
|
2 |
|
if ($cardId === $history->getId() && $to === $history->getTo()) { |
115
|
2 |
|
return $history->getDate(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
public function getCardHistories(): array |
123
|
|
|
{ |
124
|
5 |
|
return $this->cardHistories; |
125
|
|
|
} |
126
|
|
|
} |