Completed
Push — master ( d20c63...0b7335 )
by Guillaume
02:17
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  $projectId
49
     * @return mixed
50
     */
51
    public function getProjectName($projectId, array $data = [])
52
    {
53
        $projectName = '';
54
        $projects = $this->getProjects($data);
55
56
        foreach ($projects as $project) {
57
            if ($project['id'] === $projectId) {
58
                $projectName = $project['name'];
59
                break;
60
            }
61
        }
62
63
        return $projectName;
64
    }
65
66
    /**
67
     * @param  $data
68
     * @return mixed
69
     */
70
    public function getProjects($data)
71
    {
72
        return $this->getItems('projects', $data);
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78 View Code Duplication
    public function getRecentTimers()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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