Completed
Pull Request — master (#33)
by Alessandro
08:32
created

HistoryCards::getByCardIdAndTo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4.128
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 3
    private function __construct(TrelloApiClient $client, array $cardsId, Filter $filter)
20
    {
21 3
        $this->cardHistories = [];
22 3
        $creationCards = [];
23 3
        $historyCards = [];
24 3
        $this->filter = $filter;
25
26 3
        foreach ($cardsId as $cardId) {
27 2
            sleep(5);
28
29 2
            $creationCard = $client->findCreationCard($cardId->getId());
30 2
            $creationCards = array_merge($creationCards, $creationCard);
31
32 2
            $historyCard = $client->findAllCardHistory($cardId->getId());
33 2
            $historyCards = array_merge($historyCards, $historyCard);
34
        }
35
36 3
        $this->createFromArray($historyCards);
37 3
        $this->addCreationCards($creationCards);
38 3
    }
39
40 3
    public static function createFromCards(TrelloApiClient $client, array $cards, Filter $filter)
41
    {
42 3
        return new self($client, $cards, $filter);
43
    }
44
45 3
    private function createFromArray(array $cardHistoryData)
46
    {
47 3
        foreach ($cardHistoryData as $histories) {
48 2
            if ([] === $histories) {
49
                continue;
50
            }
51
52 2
            $this->addHistoryCard(HistoryCard::createFromArray($histories));
53
        }
54 3
    }
55
56 3
    private function addCreationCards(array $cardCreationData)
57
    {
58 3
        foreach ($cardCreationData as $creation) {
59 2
            if ([] === $creation) {
60
                continue;
61
            }
62
63 2
            $this->addHistoryCard(HistoryCard::createFromCreationArray($creation));
64
        }
65 3
    }
66
67 2
    private function addHistoryCard(HistoryCard $historyCard) :void
68
    {
69 2
        if (!$this->isInFromDateRange($historyCard) || !$this->isInToDateRange($historyCard)) {
70
            return;
71
        }
72
73 2
        $this->cardHistories[] = $historyCard;
74 2
    }
75
76 2
    private function isInFromDateRange(HistoryCard $historyCard) :bool
77
    {
78 2
        if ($this->filter->isFromDateEnabled() &&
79
            (   $historyCard->getDate() === null ||
80
                strtotime($historyCard->getDate()) < strtotime($this->filter->getFromDate())
81
            )) {
82
            return false;
83
        }
84
85 2
        return true;
86
    }
87
88 2
    private function isInToDateRange(HistoryCard $historyCard) :bool
89
    {
90 2
        if ($this->filter->isToDateEnabled() &&
91
            (   $historyCard->getDate() === null ||
92
                strtotime($historyCard->getDate()) > strtotime($this->filter->getToDate())
93
            )) {
94
            return false;
95
        }
96
97 2
        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 3
    public function getCardHistories(): array
117
    {
118 3
        return $this->cardHistories;
119
    }
120
}