Passed
Pull Request — master (#30)
by Guillaume
04:20 queued 02:40
created

Clockify::stopCurrentTimer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 23
rs 9.9
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\Carbon;
6
use Godbout\Alfred\Time\Workflow;
7
use GuzzleHttp\Client;
8
9
class Clockify extends TimerService
10
{
11
    private $client;
12
13
14
    public function __construct($apiToken)
15
    {
16
        $this->client = new Client([
17
            'base_uri' => 'https://api.clockify.me/api/v1/',
18
            'headers' => [
19
                'content-type' => 'application/json',
20
                'X-Api-Key' => $apiToken
21
            ]
22
        ]);
23
    }
24
25
    public function projects()
26
    {
27
        try {
28
            $user = json_decode(
29
                $this->client->get("user")->getBody()->getContents()
30
            );
31
32
            Workflow::getConfig()->write('clockify.active_workspace_id', $user->activeWorkspace);
33
34
            $response = $this->client->get("workspaces/{$user->activeWorkspace}/projects");
35
36
            $projects = json_decode($response->getBody()->getContents());
37
38
            return array_column($projects, 'name', 'id');
39
        } catch (\Exception $e) {
40
            return [];
41
        }
42
    }
43
44
    public function tags()
45
    {
46
        try {
47
            $workspaceId = Workflow::getConfig()->read('clockify.active_workspace_id');
48
49
            $response = $this->client->get("workspaces/$workspaceId/tags");
50
            $tags = json_decode($response->getBody()->getContents());
51
52
            return array_column($tags, 'name', 'id');
53
        } catch (\Exception $e) {
54
            return [];
55
        }
56
    }
57
58
    public function startTimer()
59
    {
60
        try {
61
            $workspaceId = Workflow::getConfig()->read('clockify.active_workspace_id');
62
63
            $response = $this->client->post("workspaces/$workspaceId/time-entries", [
64
                'json' => [
65
                    'start' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
66
                    'description' => getenv('timer_description'),
67
                    'projectId' => getenv('timer_project_id'),
68
                    'tagIds' => getenv('timer_tag_id') ? [getenv('timer_tag_id')] : [''],
69
                ]
70
            ]);
71
72
            $timer = json_decode($response->getBody()->getContents());
73
74
            if (! isset($timer->id)) {
75
                return false;
76
            }
77
        } catch (\Exception $e) {
78
            return false;
79
        }
80
81
        return $timer->id;
82
    }
83
84
    public function stopCurrentTimer()
85
    {
86
        $user = json_decode(
87
            $this->client->get("user")->getBody()->getContents()
88
        );
89
90
        if ($this->runningTimer()) {
91
            $response = $this->client->patch("workspaces/{$user->activeWorkspace}/user/{$user->id}/time-entries", [
92
                'json' => [
93
                    'end' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
94
                ]
95
            ]) ;
96
97
            $timer = json_decode($response->getBody()->getContents());
98
99
            if (! isset($timer->timeInterval->end)) {
100
                throw new Exception("Can't stop current running timer.", 1);
0 ignored issues
show
Bug introduced by
The type Godbout\Alfred\Time\Services\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
101
            }
102
103
            return true;
104
        }
105
106
        return false;
107
    }
108
109
    public function runningTimer()
110
    {
111
        try {
112
            $user = json_decode(
113
                $this->client->get("user")->getBody()->getContents()
114
            );
115
116
            $response = $this->client->get(
117
                "workspaces/{$user->activeWorkspace}/user/{$user->id}/time-entries?in-progress=true"
118
            );
119
120
            $timer = json_decode($response->getBody()->getContents());
121
122
            return $timer[0]->id ?? false;
123
        } catch (\Exception $e) {
124
            return false;
125
        }
126
    }
127
128
    public function pastTimers()
129
    {
130
        try {
131
            $user = json_decode(
132
                $this->client->get("user")->getBody()->getContents()
133
            );
134
135
            $response = $this->client->get("workspaces/{$user->activeWorkspace}/user/{$user->id}/time-entries", [
136
                'start' => Carbon::today(),
137
                'end' => Carbon::today()->subDays(30),
138
            ]);
139
            $clockifyTimers = json_decode($response->getBody()->getContents());
140
141
            return $this->convertToPastTimers($clockifyTimers);
142
        } catch (Exception $e) {
143
            return [];
144
        }
145
    }
146
147
    public function continueTimer($timerId = null)
148
    {
149
        /**
150
         * Timer attributes are stored in env variables
151
         * gathered in startTimer.
152
         */
153
        return $this->startTimer();
154
    }
155
156
    protected function convertToPastTimers($clockifyTimers)
157
    {
158
        $projects = $this->projects();
159
        $tags = $this->tags();
160
161
        return array_map(function ($clockifyTimer) use ($projects, $tags) {
162
            return $this->buildPastTimerObject($clockifyTimer, $projects, $tags);
163
        }, $clockifyTimers);
164
    }
165
166
    protected function buildPastTimerObject($clockifyTimer, $projects, $tags)
167
    {
168
        $pastTimer = [];
169
170
        $pastTimer['id'] = $clockifyTimer->id;
171
        $pastTimer['description'] = $clockifyTimer->description;
172
173
        if (isset($clockifyTimer->projectId)) {
174
            $pastTimer['project_id'] = $clockifyTimer->projectId;
175
            $pastTimer['project_name'] = $projects[$clockifyTimer->projectId];
176
        }
177
178
        if (isset($clockifyTimer->tagIds[0])) {
179
            $pastTimer['tag_id'] = $clockifyTimer->tagIds[0];
180
            $pastTimer['tags'] = $tags[$clockifyTimer->tagIds[0]];
181
        }
182
183
        $pastTimer['duration'] = $clockifyTimer->timeInterval->duration;
184
185
        return (object) $pastTimer;
186
    }
187
}
188