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
|
3 |
|
public function __construct(string $apiKey, string $token) |
30
|
|
|
{ |
31
|
3 |
|
$this->apiKey = $apiKey; |
32
|
3 |
|
$this->token = $token; |
33
|
3 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function findAllCards(string $boardId): array |
36
|
|
|
{ |
37
|
1 |
|
$url = $this->urlBuilderWithBoardId(self::GET_ALL_CARDS_URL, $boardId); |
38
|
|
|
|
39
|
1 |
|
return Response::validate($this->createRequest($url)); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function findCreationCard(string $cardId): array |
43
|
|
|
{ |
44
|
1 |
|
$url = $this->urlBuilderWithCardId(self::GET_CREATION_CARD_INFO_URL, $cardId); |
45
|
|
|
|
46
|
1 |
|
return Response::validate($this->createRequest($url)); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function findAllCardHistory(string $cardId): array |
50
|
|
|
{ |
51
|
1 |
|
$url = $this->urlBuilderWithCardId(self::GET_HISTORY_CARD_URL, $cardId); |
52
|
|
|
|
53
|
1 |
|
return Response::validate($this->createRequest($url)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function createRequest(string $url) |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$client = new GuzzleClient(); |
60
|
|
|
$response = $client->request('GET', $url); |
61
|
|
|
|
62
|
|
|
return $response->getBody()->getContents(); |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
throw new RuntimeException($e->getMessage()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
View Code Duplication |
protected function urlBuilderWithCardId($url, $cardId = null) |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
$placeholder = ['{cardId}', '{apiKey}', '{token}']; |
71
|
2 |
|
$replaceWith = [$cardId, $this->apiKey, $this->token]; |
72
|
|
|
|
73
|
2 |
|
return str_replace($placeholder, $replaceWith, $url); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
View Code Duplication |
protected function urlBuilderWithBoardId($url, $boardId = null) |
|
|
|
|
77
|
|
|
{ |
78
|
1 |
|
$placeholder = ['{boardId}', '{apiKey}', '{token}']; |
79
|
1 |
|
$replaceWith = [$boardId, $this->apiKey, $this->token]; |
80
|
|
|
|
81
|
1 |
|
return str_replace($placeholder, $replaceWith, $url); |
82
|
|
|
} |
83
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.