Passed
Push — master ( 353bbd...4a0bdb )
by Guillaume
34:41 queued 48s
created

Toggl::filterOutServerwiseArchivedItemsFromData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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