Passed
Pull Request — master (#30)
by Guillaume
38:09
created

Everhour::convertToPastTimers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
ccs 1
cts 3
cp 0.3333
crap 1.2963
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\CarbonInterval;
6
use GuzzleHttp\Client;
7
8
class Everhour extends TimerService
9
{
10
    private $client;
11
12
13
    public function __construct($apiToken)
14
    {
15 72
        $this->client = new Client([
16
            'base_uri' => 'https://api.everhour.com/',
17 72
            'headers' => [
18 72
                'X-Api-Key' => $apiToken
19
            ]
20 72
        ]);
21
    }
22
23 72
    public function projects()
24
    {
25 24
        try {
26
            $response = $this->client->get('projects');
27
            $projects = json_decode($response->getBody()->getContents());
28 24
29 18
            return array_column($projects, 'name', 'id');
30
        } catch (\Exception $e) {
31 18
            return [];
32 6
        }
33 6
    }
34
35
    public function tags()
36
    {
37 12
        try {
38
            $projectId = getenv('timer_project_id');
39
            $response = $this->client->get("projects/$projectId/tasks");
40 12
            $tasks = json_decode($response->getBody()->getContents());
41 12
42 6
            array_walk($tasks, function ($task) use (&$tags) {
43
                $tags[$task->id] = $task->name;
44
            });
45 6
46 6
            return $tags;
47
        } catch (\Exception $e) {
48 6
            return [];
49 6
        }
50 6
    }
51
52
    public function pastTimers()
53
    {
54 12
        try {
55
            $response = $this->client->get('users/me');
56
            $me = json_decode($response->getBody()->getContents());
57 12
58
            $response = $this->client->get("users/{$me->id}/time?limit=20&offset=0");
59 12
            $everhourTimers = json_decode($response->getBody()->getContents());
60 12
61
            return $this->convertToPastTimers($everhourTimers);
62 12
        } catch (\Exception $e) {
63 12
            return [];
64
        }
65 12
    }
66
67
    public function startTimer()
68
    {
69
        try {
70
            $response = $this->client->post('timers', [
71 30
                'json' => [
72
                    'task' => getenv('timer_tag_id'),
73
                    'comment' => getenv('timer_description')
74 30
                ]
75
            ]);
76 30
77 30
            $timer = json_decode($response->getBody()->getContents());
78
79
            if (! isset($timer->status) || $timer->status !== 'active') {
80
                return false;
81 30
            }
82
        } catch (\Exception $e) {
83 30
            return false;
84 30
        }
85
86
        return true;
87
    }
88
89
    public function stopCurrentTimer()
90 30
    {
91
        try {
92
            $response = $this->client->delete('timers/current');
93 30
94
            $timer = json_decode($response->getBody()->getContents());
95
96 30
            if (! isset($timer->taskTime) || $timer->status !== 'stopped') {
97
                return false;
98 30
            }
99
        } catch (\Exception $e) {
100 30
            return false;
101 30
        }
102
103
        return true;
104
    }
105
106
    public function runningTimer()
107 30
    {
108
        try {
109
            $response = $this->client->get('timers/current');
110 6
111
            $timer = json_decode($response->getBody()->getContents());
112
113 6
            if (! isset($timer->duration) || $timer->status !== 'active') {
114
                return false;
115 6
            }
116
        } catch (\Exception $e) {
117 6
            return false;
118 6
        }
119
120
        return true;
121
    }
122
123
    public function continueTimer($timerId = null)
124 6
    {
125
        /**
126
         * Timer attributes are stored in env variables
127 6
         * gathered in startTimer.
128
         */
129
130
        return $this->startTimer();
131
    }
132
133
    protected function convertToPastTimers($everhourTimers)
134 6
    {
135
        $projects = $this->projects();
136
137
        return array_map(function ($everhourTimer) use ($projects) {
138
            return $this->buildPastTimerObject($everhourTimer, $projects);
139
        }, $everhourTimers);
140
    }
141
142 12
    protected function buildPastTimerObject($everhourTimer, $projects)
143
    {
144 12
        $pastTimer['id'] = $everhourTimer->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...
145
        $pastTimer['description'] = $everhourTimer->comment ?? '';
146
147 12
        if (isset($everhourTimer->task)) {
148 12
            $pastTimer['project_id'] = $everhourTimer->task->projects[0];
149
            $pastTimer['project_name'] = $projects[$everhourTimer->task->projects[0]];
150
            $pastTimer['tag_id'] = $everhourTimer->task->id;
151 12
            $pastTimer['tags'] = $everhourTimer->task->name;
152
        }
153 12
154 12
        $pastTimer['duration'] = CarbonInterval::seconds(
155
            floor($everhourTimer->time)
156 12
        )->cascade()->format('%H:%I:%S');
157 12
158 12
        return (object) $pastTimer;
159 12
    }
160
}
161