Passed
Pull Request — master (#20)
by Alessandro
02:09
created

TrelloApiClient::setBoardId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TrelloCycleTime\Client;
6
7
8
use TrelloCycleTime\Client\Message\Response;
9
use TrelloCycleTime\Exception\RuntimeException;
10
use GuzzleHttp\Client as GuzzleClient;
11
12
class TrelloApiClient implements HttpClientInterface
13
{
14
    const GET_ALL_CARDS_URL = 'https://api.trello.com/1/boards/{boardId}/cards/?key={apiKey}&token={token}';
15
16
    const GET_CREATION_CARD_INFO_URL = 'https://api.trello.com/1/cards/{cardId}/actions?filter=createCard&key={apiKey}&token={token}';
17
18
    const GET_HISTORY_CARD_URL = 'https://api.trello.com/1/cards/{cardId}/actions?filter=updateCard:idList&key={apiKey}&token={token}';
19
20
    /**
21
     * @var string
22
     */
23
    private $apiKey;
24
    /**
25
     * @var string
26
     */
27
    private $token;
28
29
    /**
30
     * @var string
31
     */
32
    private $boardId;
33
34 4
    public function __construct(string $apiKey, string $token)
35
    {
36 4
        $this->apiKey = $apiKey;
37 4
        $this->token = $token;
38 4
    }
39
40 1
    public function setBoardId(string $boardId)
41
    {
42 1
        $this->boardId = $boardId;
43 1
    }
44
45 1
    public function getBoardId() :?string
46
    {
47 1
        return $this->boardId;
48
    }
49
50 1
    public function findAllCards(): array
51
    {
52 1
        $url = $this->urlBuilder(self::GET_ALL_CARDS_URL);
53
54 1
        return Response::validate($this->createRequest($url));
55
    }
56
57 1
    public function findCreationCard(string $cardId): array
58
    {
59 1
        $url = $this->urlBuilder(self::GET_CREATION_CARD_INFO_URL, $cardId);
60
61 1
        return Response::validate($this->createRequest($url));
62
    }
63
64 1
    public function findAllCardHistory(string $cardId): array
65
    {
66 1
        $url = $this->urlBuilder(self::GET_HISTORY_CARD_URL, $cardId);
67
68 1
        return Response::validate($this->createRequest($url));
69
    }
70
71
    public function createRequest(string $url)
72
    {
73
        try {
74
            $client = new GuzzleClient();
75
            $response = $client->request('GET', $url);
76
77
            return $response->getBody()->getContents();
78
        } catch (\Exception $e) {
79
            throw new RuntimeException($e->getMessage());
80
        }
81
    }
82
83 3
    protected function urlBuilder($url, $cardId = null)
84
    {
85 3
        $placeholder = ['{boardId}', '{cardId}', '{apiKey}', '{token}'];
86 3
        $replaceWith = [$this->boardId, $cardId, $this->apiKey, $this->token];
87
88 3
        return str_replace($placeholder, $replaceWith, $url);
89
    }
90
}