Completed
Push — master ( 537205...0fe0f9 )
by Guillaume
02:15
created

Toggl   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 150
Duplicated Lines 40.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 61
loc 150
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
B startTimer() 0 33 4
A stopTimer() 21 21 4
A deleteTimer() 21 21 4
A getRecentTimers() 0 18 4
B getOnlineData() 6 23 5
A getLastMessage() 0 4 1
A setMessage() 0 4 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
require __DIR__ . '/../vendor/autoload.php';
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
9
/**
10
 *
11
 */
12
class Toggl
13
{
14
    private $client;
15
    private $message;
16
17 View Code Duplication
    public function __construct($apiToken = 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...
18
    {
19
        $this->client = new Client([
20
            'base_uri' => 'https://www.toggl.com/api/v8/',
21
            'headers' => [
22
                'Content-type' => 'application/json',
23
                'Accept' => 'application/json',
24
                'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'),
25
            ],
26
        ]);
27
28
        $this->setMessage('Just init');
29
    }
30
31
    public function startTimer($description, $projectId, $tagNames)
32
    {
33
        $togglId = null;
34
35
        $item = [
36
            'time_entry' => [
37
                'description' => $description,
38
                'pid' => $projectId,
39
                'tags' => explode(', ', $tagNames),
40
                'created_with' => 'Alfred Time Workflow',
41
            ],
42
        ];
43
44
        try {
45
            $response = $this->client->post('time_entries/start', [
46
                'json' => $item,
47
            ]);
48
49
            $code = $response->getStatusCode();
50
51
            if ($code < 200 || $code > 299) {
52
                $this->setMessage('cannot start timer!');
53
            } else {
54
                $data = json_decode($response->getBody(), true);
55
                $togglId = $data['data']['id'];
56
                $this->setMessage('timer started');
57
            }
58
        } catch (ConnectException $e) {
59
            $this->setMessage('cannot connect to api!');
60
        }
61
62
        return $togglId;
63
    }
64
65 View Code Duplication
    public function stopTimer($timerId = 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...
66
    {
67
        $res = false;
68
69
        try {
70
            $response = $this->client->put('time_entries/' . $timerId . '/stop');
71
72
            if ($response->getStatusCode() !== 200) {
73
                $this->setMessage('could not stop timer!');
74
            } else {
75
                $this->setMessage('timer stopped');
76
                $res = true;
77
            }
78
        } catch (ConnectException $e) {
79
            $this->setMessage('cannot connect to api!');
80
        } catch (ClientException $e) {
81
            $this->setMessage($e->getResponse()->getBody());
82
        }
83
84
        return $res;
85
    }
86
87 View Code Duplication
    public function deleteTimer($timerId = 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...
88
    {
89
        $res = false;
90
91
        try {
92
            $response = $this->client->delete('time_entries/' . $timerId);
93
94
            if ($response->getStatusCode() !== 200) {
95
                $this->setMessage('could not delete timer!');
96
            } else {
97
                $this->setMessage('timer deleted');
98
                $res = true;
99
            }
100
        } catch (ConnectException $e) {
101
            $this->setMessage('cannot connect to api!');
102
        } catch (ClientException $e) {
103
            $this->setMessage($e->getResponse()->getBody());
104
        }
105
106
        return $res;
107
    }
108
109
    public function getRecentTimers()
110
    {
111
        $timers = [];
112
113
        try {
114
            $response = $this->client->get('time_entries');
115
116
            if ($response->getStatusCode() === 200) {
117
                $timers = json_decode($response->getBody(), true);
118
            }
119
        } catch (ConnectException $e) {
120
            $this->setMessage('cannot connect to api!');
121
        } catch (ClientException $e) {
122
            $this->setMessage($e->getResponse()->getBody());
123
        }
124
125
        return array_reverse($timers);
126
    }
127
128
    public function getOnlineData()
129
    {
130
        $data = [];
131
132
        try {
133
            $response = $this->client->get('me?with_related_data=true');
134
135
            $code = $response->getStatusCode();
136
137 View Code Duplication
            if ($code < 200 || $code > 299) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
138
                $this->setMessage('cannot get online data!');
139
            } else {
140
                $data = json_decode($response->getBody(), true);
141
                $this->setMessage('data cached');
142
            }
143
        } catch (ConnectException $e) {
144
            $this->setMessage('cannot connect to api!');
145
        } catch (ClientException $e) {
146
            $this->setMessage($e->getResponse()->getBody());
147
        }
148
149
        return $data;
150
    }
151
152
    public function getLastMessage()
153
    {
154
        return $this->message;
155
    }
156
157
    private function setMessage($message = null)
158
    {
159
        $this->message = '- Toggl: ' . $message;
160
    }
161
}
162