Passed
Push — clockify ( 5f6c37...9600d6 )
by Guillaume
35:46
created

Clockify::convertToPastTimers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\Carbon;
6
use JDecool\Clockify\ClientBuilder;
7
8
class Clockify extends TimerService
9
{
10
    private $client;
11
12
    private $data = null;
0 ignored issues
show
introduced by
The private property $data is not used, and could be removed.
Loading history...
13
14 22
15
    public function __construct($apiToken)
16 22
    {
17 22
        $this->client = (new ClientBuilder())->createClientV1($apiToken);
18
    }
19 11
20
    public function workspaces()
21
    {
22 11
        try {
23 11
            return $this->client->get('workspaces');
24 11
        } catch (\Exception $e) {
25
            return [];
26
        }
27
    }
28 11
29
    public function projects()
30
    {
31 11
        try {
32
            $workspaceId = getenv('timer_workspace_id');
33 11
34 11
            $projects = $this->client->get("workspaces/$workspaceId/projects");
35 11
36
            return array_column($projects, 'name', 'id');
37
        } catch (\Exception $e) {
38
            return [];
39
        }
40
    }
41
42
    public function tags()
43
    {
44
        try {
45
            $workspaceId = getenv('timer_workspace_id');
46
47
            $tags = $this->client->get("workspaces/$workspaceId/tags");
48
49
            return array_column($tags, 'name', 'id');
50
        } catch (\Exception $e) {
51
            return [];
52
        }
53
    }
54
55
    public function startTimer()
56
    {
57
        try {
58
            $workspaceId = getenv('timer_workspace_id');
59 22
60
            $timer = $this->client->post("workspaces/$workspaceId/time-entries", [
61 22
                'start' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
62
                'description' => getenv('timer_description'),
63
                'projectId' => getenv('timer_project_id'),
64
                'tagIds' => getenv('timer_tag_id') ? [getenv('timer_tag_id')] : [''],
65
            ]);
66
67
            if (! isset($timer['id'])) {
68
                return false;
69
            }
70
        } catch (Exception $e) {
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...
71
            return false;
72
        }
73
74
        return $timer['id'];
75
    }
76
77
    public function stopCurrentTimer()
78
    {
79
        $workspaceId = getenv('timer_workspace_id');
80
        $userId = getenv('timer_user_id');
81
82
        if ($timerId = $this->runningTimer()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $timerId is dead and can be removed.
Loading history...
83
            $timer = $this->client->patch("workspaces/$workspaceId/user/$userId/time-entries", [
84
                'end' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
85
            ]) ;
86
87
            if (! isset($timer['timeInterval']['end'])) {
88
                throw new Exception("Can't stop current running timer.", 1);
89
90
                return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
91
            }
92
93
            return true;
94
        }
95
96
        return false;
97
    }
98
99
    public function runningTimer()
100
    {
101
        $workspaceId = getenv('timer_workspace_id');
102
        $userId = getenv('timer_user_id');
103
104
        $timer = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries?in-progress=true");
105
106
        return $timer[0]['id'] ?? false;
107
    }
108
109
    public function pastTimers()
110
    {
111
        try {
112
            $workspaceId = getenv('timer_workspace_id');
113
            $userId = getenv('timer_user_id');
114
            $pastTimers = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $pastTimers is dead and can be removed.
Loading history...
115
116
            $clockifyTimers = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries", [
117
                'start' => Carbon::today(),
118
                'end' => Carbon::today()->subDays(30),
119
            ]);
120
121
            return $this->convertToPastTimers($clockifyTimers);
122
        } catch (Exception $e) {
123
            return [];
124
        }
125
    }
126
127
    public function continueTimer($timerId = null)
128
    {
129
        return false;
130
    }
131
132
    public function deleteTimer($timerId)
133
    {
134
        return false;
135
    }
136
137
    protected function convertToPastTimers($clockifyTimers)
138
    {
139
        $projects = $this->projects();
140
        $tags = $this->tags();
141
142
        return array_map(function ($clockifyTimer) use ($projects, $tags) {
143
            return $this->buildPastTimerObject($clockifyTimer, $projects, $tags);
144
        }, $clockifyTimers);
145
    }
146
147
    protected function buildPastTimerObject($clockifyTimer, $projects, $tags)
148
    {
149
        $pastTimer['id'] = $clockifyTimer['id'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pastTimer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pastTimer = array(); before regardless.
Loading history...
150
        $pastTimer['description'] = $clockifyTimer['description'];
151
152
        if (isset($clockifyTimer['projectId'])) {
153
            $pastTimer['project_id'] = $clockifyTimer['projectId'];
154
            $pastTimer['project_name'] = $projects[$clockifyTimer['projectId']];
155
        }
156
157
        if (isset($clockifyTimer['tagIds'][0])) {
158
            $pastTimer['tag_id'] = $clockifyTimer['tagIds'][0];
159
            $pastTimer['tags'] = $tags[$clockifyTimer['tagIds'][0]];
160
        }
161
162
        $pastTimer['duration'] = $clockifyTimer['timeInterval']['duration'];
163
164
        return (object) $pastTimer;
165
    }
166
}
167