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
|
|
|
( $historyCard->getDate() === null || |
80
|
|
|
strtotime($historyCard->getDate()) < strtotime($this->filter->getFromDate()) |
81
|
|
|
)) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
private function isInToDateRange(HistoryCard $historyCard) :bool |
89
|
|
|
{ |
90
|
3 |
|
if ($this->filter->isToDateEnabled() && |
91
|
|
|
( $historyCard->getDate() === null || |
92
|
|
|
strtotime($historyCard->getDate()) > strtotime($this->filter->getToDate()) |
93
|
|
|
)) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $cardId |
102
|
|
|
* @param string $to |
103
|
|
|
* @return null|string |
104
|
|
|
*/ |
105
|
2 |
|
public function getByCardIdAndTo(string $cardId, string $to): ?string |
106
|
|
|
{ |
107
|
2 |
|
foreach ($this->cardHistories as $history) { |
108
|
2 |
|
if ($cardId === $history->getId() && $to === $history->getTo()) { |
109
|
2 |
|
return $history->getDate(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
public function getCardHistories(): array |
117
|
|
|
{ |
118
|
5 |
|
return $this->cardHistories; |
119
|
|
|
} |
120
|
|
|
} |