Completed
Push — master ( bdd854...9b3a32 )
by Mark
01:44
created

Laposta   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 22.02 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 13
c 2
b 1
f 0
lcom 1
cbo 3
dl 24
loc 109
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A getLists() 0 12 2
A getList() 0 8 1
A createList() 13 13 1
A updateList() 11 11 1
A get() 0 4 1
A post() 0 4 1
A request() 0 12 2

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
namespace Mrkj\Laposta;
4
5
use GuzzleHttp\Client;
6
use Mrkj\Laposta\Models\List_;
7
use GuzzleHttp\Exception\RequestException;
8
9
class Laposta
10
{
11
    /**
12
     * @var string
13
     */
14
    private $apiKey;
15
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    public function __construct($apiKey = null, $client = null)
22
    {
23
        if (empty(trim($apiKey))) {
24
            throw new \Exception('No API key provided');
25
        }
26
27
        $this->apiKey = $apiKey;
28
29
        if ($client && $client instanceof Client) {
30
            $this->client = $client;
31
        } else {
32
            $this->client = new Client([
33
                'base_uri' => 'https://api.laposta.nl/v2/',
34
                'auth' => [$this->apiKey, ''],
35
                'http_errors' => true,
36
            ]);
37
        }
38
    }
39
40
    /**
41
     * @return List_[]
42
     */
43
    public function getLists()
44
    {
45
        $data = $this->get('list');
46
47
        $lists = [];
48
49
        foreach ($data['data'] as $listResponse) {
50
            $lists[] = List_::createFromResponse($listResponse['list']);
51
        }
52
53
        return $lists;
54
    }
55
56
    /**
57
     * @param $listId
58
     * @return bool|List_
59
     */
60
    public function getList($listId)
61
    {
62
        $data = $this->get('list/'.$listId);
63
64
        $list = List_::createFromResponse($data['list']);
65
66
        return $list;
67
    }
68
69 View Code Duplication
    public function createList(List_ $list)
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...
70
    {
71
        $data = $this->post('list', [
72
            'form_params' => [
73
                'name' => $list->getName(),
74
                'remarks' => $list->getRemarks(),
75
                'subscribe_notification_email' => $list->getSubscribeNotificationEmail(),
76
                'unsubscribe_notification_email' => $list->getUnsubsribeNotificationEmail(),
77
            ],
78
        ]);
79
80
        $list->updateFromResponse($data['list']);
81
    }
82
83 View Code Duplication
    public function updateList(List_ $list)
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...
84
    {
85
        $this->client->post('list/'.$list->getListId(), [
86
            'form_params' => [
87
                'name' => $list->getName(),
88
                'remarks' => $list->getRemarks(),
89
                'subscribe_notification_email' => $list->getSubscribeNotificationEmail(),
90
                'unsubscribe_notification_email' => $list->getUnsubsribeNotificationEmail(),
91
            ],
92
        ]);
93
    }
94
95
    private function get($uri, $options = [])
96
    {
97
        return $this->request('get', $uri, $options);
98
    }
99
100
    private function post($uri, $options = [])
101
    {
102
        return $this->request('post', $uri, $options);
103
    }
104
105
    private function request($method, $uri, $options = [])
106
    {
107
        try {
108
            $response = $this->client->request($method, $uri, $options);
109
110
            $data = json_decode($response->getBody()->getContents(), true);
111
112
            return $data;
113
        } catch (RequestException $e) {
114
            throw new \Exception('Loading Laposta resource failed: '.$e->getMessage());
115
        }
116
    }
117
}
118