Completed
Push — master ( 37dc4f...12f138 )
by Guillaume
02:12
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 extends Service
11
{
12
    /**
13
     * @param $apiToken
14
     */
15
    public function __construct($apiToken = null)
16
    {
17
        parent::__construct('https://www.toggl.com/api/v8/', base64_encode($apiToken . ':api_token'));
18
    }
19
20
    /**
21
     * @param  $timerId
22
     * @return mixed
23
     */
24
    public function deleteTimer($timerId = null)
25
    {
26
        return $this->timerAction('delete', 'time_entries/' . $timerId);
27
    }
28
29
    /**
30
     * @return mixed
31
     */
32
    public function getOnlineData()
33
    {
34
        return $this->timerAction('get_online_data', 'me?with_related_data=true');
35
    }
36
37
    /**
38
     * @param  $projectId
39
     * @return mixed
40
     */
41
    public function getProjectName($projectId, array $data = [])
42
    {
43
        $projectName = '';
44
        $projects = $this->getProjects($data);
45
46
        foreach ($projects as $project) {
47
            if ($project['id'] === $projectId) {
48
                $projectName = $project['name'];
49
                break;
50
            }
51
        }
52
53
        return $projectName;
54
    }
55
56
    /**
57
     * @param  $data
58
     * @return mixed
59
     */
60
    public function getProjects($data)
61
    {
62
        return $this->getItems('projects', $data);
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68 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...
69
    {
70
        $timers = [];
71
72
        foreach ($this->timerAction('get_recent_timers', 'time_entries') as $timeEntry) {
73
            $timers[] = [
74
                'id'           => $timeEntry['id'],
75
                'description'  => $timeEntry['description'],
76
                'project_id'   => $timeEntry['pid'],
77
                'project_name' => $timeEntry['pid'],
78
                'tags'         => empty($timeEntry['tags']) ? '' : implode(', ', $timeEntry['tags']),
79
                'duration'     => $timeEntry['duration'],
80
            ];
81
        }
82
83
        return array_reverse($timers);
84
    }
85
86
    /**
87
     * @param  $data
88
     * @return mixed
89
     */
90
    public function getTags($data)
91
    {
92
        return $this->getItems('tags', $data);
93
    }
94
95
    /**
96
     * @param  $description
97
     * @param  $projectId
98
     * @param  $tagNames
99
     * @return mixed
100
     */
101
    public function startTimer($description, $projectId, $tagData)
102
    {
103
        $togglId = null;
104
        $item = [
105
            'time_entry' => [
106
                'description'  => $description,
107
                'pid'          => $projectId,
108
                'tags'         => explode(', ', $tagData),
109
                'created_with' => 'Alfred Time Workflow',
110
            ],
111
        ];
112
113
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
114
115
        if (isset($data['data']['id']) === true) {
116
            $togglId = $data['data']['id'];
117
        }
118
119
        return $togglId;
120
    }
121
122
    /**
123
     * @param  $timerId
124
     * @return mixed
125
     */
126
    public function stopTimer($timerId = null)
127
    {
128
        return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
129
    }
130
131
    /**
132
     * @param  string  $action
133
     * @param  string  $apiUri
134
     * @return mixed
135
     */
136
    public function timerAction($action, $apiUri, array $options = [])
137
    {
138
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
139
        $methods = [
140
            'start'             => 'post',
141
            'stop'              => 'put',
142
            'delete'            => 'delete',
143
            'get_recent_timers' => 'get',
144
            'get_online_data'   => 'get',
145
        ];
146
        $method = isset($methods[$action]) ? $methods[$action] : '';
147
148
        return $this->serviceApiCall->send(
149
            $method,
150
            $apiUri,
151
            $options,
152
            in_array($action, $returnDataFor)
153
        );
154
    }
155
156
    /**
157
     * @param  $needle
158
     * @param  array     $haystack
159
     * @return mixed
160
     */
161
    private function getItems($needle, array $haystack = [])
162
    {
163
        $items = [];
164
165
        if (isset($haystack['data'][$needle]) === false) {
166
            return [];
167
        }
168
169
        /**
170
         * To only show projects that are currently active
171
         * The Toggl API is slightly weird on that
172
         */
173
        foreach ($haystack['data'][$needle] as $key => $item) {
174
            if (isset($item['server_deleted_at']) === true) {
175
                unset($haystack['data'][$needle][$key]);
176
            }
177
178
            $items[] = [
179
                'name' => $item['name'],
180
                'id'   => ($needle === 'tags') ? $item['name'] : $item['id'],
181
            ];
182
        }
183
184
        return $items;
185
    }
186
}
187