Completed
Push — master ( 557ba8...ce1b3d )
by Guillaume
04:38
created

src/Toggl.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
57
     * @return mixed
58
     */
59
    public function getRecentTimers()
60
    {
61
        $timers = [];
62
63
        $i = 0;
64
        foreach ($this->timerAction('get_recent_timers', 'time_entries') as $timeEntry) {
65
            $i++;
66
            if ($i === 10) {
67
                break;
68
            }
69
            $timers[] = [
70
                'id'           => $timeEntry['id'],
71
                'description'  => $timeEntry['description'],
72
                'project_name' => $timeEntry['pid'],//$this->getProjectName($timeEntry['pid']),
0 ignored issues
show
Unused Code Comprehensibility introduced by
90% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
                'tags'         => empty($timeEntry['tags']) ? '' : implode(', ', $timeEntry['tags']),
74
                'duration'     => $timeEntry['duration'],
75
            ];
76
        }
77
78
        return array_reverse($timers);
79
    }
80
81
    /**
82
     * @param  $data
83
     * @return mixed
84
     */
85
    public function getTags($data)
86
    {
87
        return $this->getItems('tags', $data);
88
    }
89
90
    /**
91
     * @param  $description
92
     * @param  $projectId
93
     * @param  $tagNames
94
     * @return mixed
95
     */
96
    public function startTimer($description, $projectId, $tagData)
97
    {
98
        $togglId = null;
99
        $item = [
100
            'time_entry' => [
101
                'description'  => $description,
102
                'pid'          => $projectId,
103
                'tags'         => explode(', ', $tagData),
104
                'created_with' => 'Alfred Time Workflow',
105
            ],
106
        ];
107
108
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
109
110
        if (isset($data['data']['id']) === true) {
111
            $togglId = $data['data']['id'];
112
        }
113
114
        return $togglId;
115
    }
116
117
    /**
118
     * @param  $timerId
119
     * @return mixed
120
     */
121
    public function stopTimer($timerId = null)
122
    {
123
        return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
124
    }
125
126
    /**
127
     * @param  string  $action
128
     * @param  string  $apiUri
129
     * @return mixed
130
     */
131
    public function timerAction($action, $apiUri, array $options = [])
132
    {
133
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
134
        $methods = [
135
            'start'             => 'post',
136
            'stop'              => 'put',
137
            'delete'            => 'delete',
138
            'get_recent_timers' => 'get',
139
            'get_online_data'   => 'get',
140
        ];
141
        $method = isset($methods[$action]) ? $methods[$action] : '';
142
143
        return $this->serviceApiCall->send(
144
            $method,
145
            $apiUri,
146
            $options,
147
            in_array($action, $returnDataFor)
148
        );
149
    }
150
151
    /**
152
     * @param  $needle
153
     * @param  array     $haystack
154
     * @return mixed
155
     */
156
    private function getItems($needle, array $haystack = [])
157
    {
158
        $items = [];
159
160
        if (isset($haystack['data'][$needle]) === false) {
161
            return [];
162
        }
163
164
        /**
165
         * To only show projects that are currently active
166
         * The Toggl API is slightly weird on that
167
         */
168
        foreach ($haystack['data'][$needle] as $key => $item) {
169
            if (isset($item['server_deleted_at']) === true) {
170
                unset($haystack['data'][$needle][$key]);
171
            }
172
173
            $items[] = [
174
                'name' => $item['name'],
175
                'id'   => ($needle === 'tags') ? $item['name'] : $item['id'],
176
            ];
177
        }
178
179
        return $items;
180
    }
181
}
182