Completed
Push — master ( 458df2...a26ef0 )
by Guillaume
02:45
created

Toggl::stopTimer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require 'vendor/autoload.php';
4
5
use GuzzleHttp\Client;
6
7
/**
8
 *
9
 */
10
class Toggl
11
{
12
    private $client;
13
    private $message;
14
    private $apiToken;
0 ignored issues
show
Unused Code introduced by
The property $apiToken is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    public function __construct($apiToken = null)
17
    {
18
        $this->client = new Client([
19
            'base_uri' => 'https://www.toggl.com/api/v8/',
20
            'headers' => [
21
                'Content-type' => 'application/json',
22
                'Accept' => 'application/json',
23
                'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'),
24
            ],
25
        ]);
26
        $this->message = '';
27
    }
28
29
    public function startTimer($description, $projectId, $tagNames)
30
    {
31
        $togglId = null;
32
33
        $item = [
34
            'time_entry' => [
35
                'description' => $description,
36
                'pid' => $projectId,
37
                'tags' => explode(', ', $tagNames),
38
                'created_with' => 'Alfred Time Workflow',
39
            ],
40
        ];
41
42
        $response = $this->client->post('time_entries/start', [
43
            'json' => $item,
44
        ]);
45
46
        $code = $response->getStatusCode();
47
48
        if ($code < 200 || $code > 299) {
49
            $this->message = '- Cannot start Toggl timer!';
50
        } else {
51
            $data = json_decode($response->getBody(), true);
52
            $togglId = $data['data']['id'];
53
            $this->message = '- Toggl timer started';
54
        }
55
56
        return $togglId;
57
    }
58
59 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...
60
    {
61
        $res = false;
62
63
        $response = $this->client->put('time_entries/' . $timerId . '/stop');
64
65
        if ($response->getStatusCode() !== 200) {
66
            $this->message = '- Could not stop Toggl timer!';
67
        } else {
68
            $this->message = '- Toggl timer stopped';
69
            $res = true;
70
        }
71
72
        return $res;
73
    }
74
75
    public function getLastMessage()
76
    {
77
        return $this->message;
78
    }
79
80 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...
81
    {
82
        $res = false;
83
84
        $response = $this->client->delete('time_entries/' . $timerId);
85
86
        if ($response->getStatusCode() !== 200) {
87
            $this->message = '- Could not delete Toggl timer!';
88
        } else {
89
            $this->message = '- Toggl timer deleted';
90
            $res = true;
91
        }
92
93
        return $res;
94
    }
95
96
    public function getRecentTimers()
97
    {
98
        $timers = [];
99
100
        $response = $this->client->get('time_entries');
101
102
        if ($response->getStatusCode() === 200) {
103
            $timers = json_decode($response->getBody(), true);
104
        }
105
106
        return array_reverse($timers);
107
    }
108
109
    public function getOnlineData()
110
    {
111
        $data = [];
112
113
        $response = $this->client->get('me?with_related_data=true');
114
115
        $code = $response->getStatusCode();
116
117
        if ($code < 200 || $code > 299) {
118
            $this->message = '- Cannot get Toggl online data!';
119
        } else {
120
            $data = json_decode($response->getBody(), true);
121
            $this->message = '- Toggl data cached';
122
        }
123
124
        return $data;
125
    }
126
}
127