Completed
Push — master ( 24faf9...e9a21a )
by Guillaume
02:39
created

Toggl::getRecentTimers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
/**
8
 *
9
 */
10
class Toggl
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private $serviceApiCall = null;
16
17
    /**
18
     * @param $apiToken
19
     */
20
    public function __construct($apiToken = null)
21
    {
22
        $this->serviceApiCall = new ServiceApiCall([
23
            'base_uri' => 'https://www.toggl.com/api/v8/',
24
            'headers'  => [
25
                'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'),
26
            ],
27
        ]);
28
    }
29
30
    /**
31
     * @param  $timerId
32
     * @return mixed
33
     */
34
    public function deleteTimer($timerId = null)
35
    {
36
        return $this->timerAction('delete', 'time_entries/' . $timerId);
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function getOnlineData()
43
    {
44
        return $this->timerAction('get_online_data', 'me?with_related_data=true');
45
    }
46
47
    /**
48
     * @param  $data
49
     * @return mixed
50
     */
51
    public function getProjects($data)
52
    {
53
        return $this->getItems('projects', $data);
54
    }
55
56
    public function getRecentTimers()
57
    {
58
        return array_reverse($this->timerAction('get_recent_timers', 'time_entries'));
59
    }
60
61
    /**
62
     * @param  $data
63
     * @return mixed
64
     */
65
    public function getTags($data)
66
    {
67
        return $this->getItems('tags', $data);
68
    }
69
70
    /**
71
     * @param  $description
72
     * @param  $projectId
73
     * @param  $tagNames
74
     * @return mixed
75
     */
76
    public function startTimer($description, $projectId, $tagData)
77
    {
78
        $togglId = null;
79
        $item = [
80
            'time_entry' => [
81
                'description'  => $description,
82
                'pid'          => $projectId,
83
                'tags'         => explode(', ', $tagData),
84
                'created_with' => 'Alfred Time Workflow',
85
            ],
86
        ];
87
88
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
89
90
        if (isset($data['data']['id']) === true) {
91
            $togglId = $data['data']['id'];
92
        }
93
94
        return $togglId;
95
    }
96
97
    /**
98
     * @param  $timerId
99
     * @return mixed
100
     */
101
    public function stopTimer($timerId = null)
102
    {
103
        return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
104
    }
105
106
    /**
107
     * @param  string  $action
108
     * @param  string  $apiUri
109
     * @return mixed
110
     */
111
    public function timerAction($action, $apiUri, array $options = [])
112
    {
113
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
114
        $methods = [
115
            'start'             => 'post',
116
            'stop'              => 'put',
117
            'delete'            => 'delete',
118
            'get_recent_timers' => 'get',
119
            'get_online_data'   => 'get',
120
        ];
121
        $method = isset($methods[$action]) ? $methods[$action] : '';
122
123
        return $this->serviceApiCall->send(
124
            $method,
125
            $apiUri,
126
            $options,
127
            in_array($action, $returnDataFor)
128
        );
129
    }
130
131
    /**
132
     * @param  $needle
133
     * @param  array     $haystack
134
     * @return mixed
135
     */
136
    private function getItems($needle, array $haystack = [])
137
    {
138
        $items = [];
139
140
        if (isset($haystack['data'][$needle]) === false) {
141
            return [];
142
        }
143
144
        /**
145
         * To only show projects that are currently active
146
         * The Toggl API is slightly weird on that
147
         */
148
        foreach ($haystack['data'][$needle] as $key => $item) {
149
            if (isset($item['server_deleted_at']) === true) {
150
                unset($haystack['data'][$needle][$key]);
151
            }
152
153
            $items[] = [
154
                'name' => $item['name'],
155
                'id'   => ($needle === 'tags') ? $item['name'] : $item['id'],
156
            ];
157
        }
158
159
        return $items;
160
    }
161
}
162