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 HttpClient 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
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $boardId; |
32
|
|
|
|
33
|
3 |
|
public function __construct(string $apiKey, string $token, string $boardId) |
34
|
|
|
{ |
35
|
3 |
|
$this->apiKey = $apiKey; |
36
|
3 |
|
$this->token = $token; |
37
|
3 |
|
$this->boardId = $boardId; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function findAllCards(): array |
41
|
|
|
{ |
42
|
1 |
|
$url = $this->urlBuilder(self::GET_ALL_CARDS_URL); |
43
|
|
|
|
44
|
1 |
|
return Response::validate($this->createRequest($url)); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function findCreationCard(string $cardId): array |
48
|
|
|
{ |
49
|
1 |
|
$url = $this->urlBuilder(self::GET_CREATION_CARD_INFO_URL, $cardId); |
50
|
|
|
|
51
|
1 |
|
return Response::validate($this->createRequest($url)); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function findAllCardHistory(string $cardId): array |
55
|
|
|
{ |
56
|
1 |
|
$url = $this->urlBuilder(self::GET_HISTORY_CARD_URL, $cardId); |
57
|
|
|
|
58
|
1 |
|
return Response::validate($this->createRequest($url)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function createRequest(string $url) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
$client = new GuzzleClient(); |
65
|
|
|
$response = $client->request('GET', $url); |
66
|
|
|
|
67
|
|
|
return $response->getBody()->getContents(); |
68
|
|
|
} catch (\Exception $e) { |
69
|
|
|
throw new RuntimeException($e->getMessage()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
protected function urlBuilder($url, $cardId = null) |
74
|
|
|
{ |
75
|
3 |
|
$placeholder = ['{boardId}', '{cardId}', '{apiKey}', '{token}']; |
76
|
3 |
|
$replaceWith = [$this->boardId, $cardId, $this->apiKey, $this->token]; |
77
|
|
|
|
78
|
3 |
|
return str_replace($placeholder, $replaceWith, $url); |
79
|
|
|
} |
80
|
|
|
} |