Passed
Push — master ( c21964...371444 )
by Alessandro
01:05 queued 18s
created

TrelloApiClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 19.44 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 14
loc 72
ccs 21
cts 27
cp 0.7778
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findAllCards() 0 6 1
A findCreationCard() 0 6 1
A findAllCardHistory() 0 6 1
A createRequest() 0 11 2
A urlBuilderWithCardId() 7 7 1
A urlBuilderWithBoardId() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}