Passed
Push — toggl-hide-archived-projects ( b5b807...531582 )
by Guillaume
38:10
created

filterOutServerwiseDeletedAndArchivedItemsFromData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\Carbon;
6
use Carbon\CarbonInterval;
7
use Exception;
8
use MorningTrain\TogglApi\TogglApi;
9
10
class Toggl extends TimerService
11
{
12
    private $client;
13
14
    private $data = null;
15
16
17 104
    public function __construct($apiToken)
18
    {
19 104
        $this->client = new TogglApi($apiToken);
20 104
    }
21
22 24
    public function projects()
23
    {
24 24
        return $this->extractFromData('projects');
25
    }
26
27 20
    public function tags()
28
    {
29 20
        return $this->extractFromData('tags');
30
    }
31
32
    public function pastTimers()
33
    {
34
        try {
35
            $togglTimers = $this->client->getTimeEntriesInRange(Carbon::today(), Carbon::today()->subDays(30));
36
37
            return $this->convertToPastTimers($togglTimers);
38
        } catch (Exception $e) {
39
            return [];
40
        }
41
    }
42
43 12
    public function startTimer()
44
    {
45
        try {
46 12
            $timer = $this->client->startTimeEntry([
47 12
                'description' => getenv('timer_description'),
48 12
                'pid' => getenv('timer_project_id'),
49 12
                'tags' => getenv('timer_tag') ? [getenv('timer_tag')] : '',
50 12
                'created_with' => 'Alfred Time'
51
            ]);
52
53 12
            if (! isset($timer->id)) {
54 12
                return false;
55
            }
56
        } catch (Exception $e) {
57
            return false;
58
        }
59
60 12
        return $timer->id;
61
    }
62
63 8
    public function stopCurrentTimer()
64
    {
65 8
        if ($timerId = $this->runningTimer()) {
66 8
            $response = $this->client->stopTimeEntry($timerId);
67
68 8
            if (! isset($response->id)) {
69
                throw new Exception("Can't stop current running timer.", 1);
70
            }
71
72 8
            return true;
73
        }
74
75 2
        return false;
76
    }
77
78 68
    public function runningTimer()
79
    {
80 68
        $timer = $this->client->getRunningTimeEntry();
81
82 68
        return $timer->id ?? false;
83
    }
84
85 2
    public function continueTimer($timerId)
86
    {
87
        /**
88
         * Timer attributes are stored in env variables
89
         * gathered in startTimer.
90
         */
91 2
        return $this->startTimer();
92
    }
93
94 12
    public function deleteTimer($timerId)
95
    {
96
        try {
97 12
            $this->client->deleteTimeEntry($timerId);
98
        } catch (Exception $e) {
99
            return false;
100
        }
101
102 12
        return true;
103
    }
104
105 44
    private function extractFromData($needle)
106
    {
107 44
        $data = $this->getData();
108
109 44
        if (! isset($data->$needle)) {
110 36
            return [];
111
        }
112
113 8
        $choosableData = $this->filterOutServerwiseDeletedAndArchivedItemsFromData($data->$needle);
114 8
115
        return array_column($choosableData, 'name', 'id');
116 8
    }
117
118
    private function getData()
119 44
    {
120
        if (is_null($this->data)) {
121 44
            return $this->client->getMe(true);
122 44
        }
123
124
        return $this->data;
125
    }
126
127
    private function filterOutServerwiseDeletedAndArchivedItemsFromData($items = [])
128 8
    {
129
        return array_filter($items, function ($item) {
130
            return
131 8
                ! $this->itemIsDeletedServerwise($item)
132 8
                && ! $this->itemIsArchivedServerwise($item);
133
        });
134
    }
135 8
136
    private function itemIsDeletedServerwise($item)
137
    {
138 8
        return isset($item->server_deleted_at);
139 8
    }
140
141
    private function itemIsArchivedServerwise($item)
142
    {
143
        return
144
            isset($item->active)
145
            && $item->active === false;
146
    }
147
148
    protected function convertToPastTimers($togglTimers)
149
    {
150
        $projects = $this->projects();
151
152
        return array_reverse(
153
            array_map(function ($togglTimer) use ($projects) {
154
                return $this->buildPastTimerObject($togglTimer, $projects);
155
            }, $togglTimers)
156
        );
157
    }
158
159
    protected function buildPastTimerObject($togglTimer, $projects)
160
    {
161
        $pastTimer['id'] = $togglTimer->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...
162
        $pastTimer['description'] = $togglTimer->description ?? '';
163
        $pastTimer['duration'] = CarbonInterval::seconds($togglTimer->duration)->cascade()->format('%H:%I:%S');
164
165
        if (isset($togglTimer->pid)) {
166
            $pastTimer['project_id'] = $togglTimer->pid;
167
            $pastTimer['project_name'] = $projects[$togglTimer->pid] ?? $togglTimer->pid;
168
        }
169
170
        if (isset($togglTimer->tags)) {
171
            $pastTimer['tags'] = implode(', ', (array) $togglTimer->tags);
172
        }
173
174
        return (object) $pastTimer;
175
    }
176
}
177