Completed
Push — master ( da6153...10ffd2 )
by Guillaume
02:11
created

Toggl::lastApiCall()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
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 = null;
15
    private $code = 0;
16
    private $message = '';
17
    private $data = [];
18
19 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...
20
    {
21
        $this->client = new Client([
22
            'base_uri' => 'https://www.toggl.com/api/v8/',
23
            'headers' => [
24
                'Content-type' => 'application/json',
25
                'Accept' => 'application/json',
26
                'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'),
27
            ],
28
        ]);
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 View Code Duplication
        if ($this->sendApiCall('post', 'time_entries/start', ['json' => $item]) === true) {
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...
45
            if ($this->lastApiCall('success') === true) {
46
                $this->setMessage('timer started');
47
                $togglId = $this->data['data']['id'];
48
            } else {
49
                $this->setMessage('cannot start timer!');
50
            }
51
        }
52
53
        return $togglId;
54
    }
55
56 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...
57
    {
58
        $res = false;
59
60
        if ($this->sendApiCall('put', 'time_entries/' . $timerId . '/stop') === true) {
61
            if ($this->lastApiCall('success') === true) {
62
                $this->setMessage('timer stopped');
63
                $res = true;
64
            } else {
65
                $this->setMessage('could not stop timer!');
66
            }
67
        }
68
69
        return $res;
70
    }
71
72 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...
73
    {
74
        $res = false;
75
76
        if ($this->sendApiCall('delete', 'time_entries/' . $timerId) === true) {
77
            if ($this->lastApiCall('success') === true) {
78
                $this->setMessage('timer deleted');
79
                $res = true;
80
            } else {
81
                $this->setMessage('could not delete timer!');
82
            }
83
        }
84
85
        return $res;
86
    }
87
88
    public function getRecentTimers()
89
    {
90
        $timers = [];
91
92
        if ($this->sendApiCall('get', 'time_entries') === true) {
93
            if ($this->lastApiCall('success') === true) {
94
                $timers = $this->data;
95
            }
96
        }
97
98
        return array_reverse($timers);
99
    }
100
101
    public function getOnlineData()
102
    {
103
        $data = [];
104
105
        if ($this->sendApiCall('get', 'me?with_related_data=true') === true) {
106
            if ($this->lastApiCall('success') === true) {
107
                $this->setMessage('data cached');
108
                $data = $this->data;
109
            } else {
110
                $this->setMessage('cannot get online data!');
111
            }
112
        }
113
114
        return $data;
115
    }
116
117
    public function getLastMessage()
118
    {
119
        return $this->message;
120
    }
121
122
    private function setMessage($message = null)
123
    {
124
        $this->message = '- Toggl: ' . $message;
125
    }
126
127 View Code Duplication
    private function sendApiCall($method, $uri = '', array $options = [])
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...
128
    {
129
        $res = true;
130
131
        try {
132
            $response = $this->client->request(strtoupper($method), $uri, $options);
133
            $this->code = $response->getStatusCode();
134
            $this->data = json_decode($response->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($response->getBody(), true) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135
        } catch (ConnectException $e) {
136
            $this->setMessage('cannot connect to api!');
137
            $res = false;
138
        } catch (ClientException $e) {
139
            $this->setMessage($e->getResponse()->getBody());
140
        }
141
142
        return $res;
143
    }
144
145 View Code Duplication
    private function lastApiCall($status = '')
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...
146
    {
147
        $res = false;
148
149
        switch ($status) {
150
            case 'success':
151
                if ($this->code >= 200 || $this->code <= 299) {
152
                    $res = true;
153
                }
154
                break;
155
        }
156
157
        return $res;
158
    }
159
}
160