Completed
Push — master ( e9a21a...557ba8 )
by Guillaume
02:41
created

Harvest::getRecentTimers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
/**
8
 *
9
 */
10
class Harvest
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private $serviceApiCall = null;
16
17
    /**
18
     * @param $domain
19
     * @param null      $apiToken
20
     */
21
    public function __construct($domain = null, $apiToken = null)
22
    {
23
        $this->serviceApiCall = new ServiceApiCall([
24
            'base_uri' => 'https://' . $domain . '.harvestapp.com/',
25
            'headers'  => [
26
                'Authorization' => 'Basic ' . $apiToken,
27
            ],
28
        ]);
29
    }
30
31
    /**
32
     * @param  $timerId
33
     * @return mixed
34
     */
35
    public function deleteTimer($timerId = null)
36
    {
37
        return $this->timerAction('delete', 'daily/delete/' . $timerId);
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43
    public function getOnlineData()
44
    {
45
        $data = [];
46
        $data['projects'] = $this->timerAction('get_projects', 'projects');
47
        $data['tasks'] = $this->timerAction('get_tags', 'tasks');
48
49
        return $data;
50
    }
51
52
    /**
53
     * @param  $data
54
     * @return mixed
55
     */
56
    public function getProjects($data)
57
    {
58
        return $this->getItems('projects', $data);
59
    }
60
61
    public function getRecentTimers()
62
    {
63
        $timers = [];
64
65
        foreach ($this->timerAction('get_recent_timers', 'daily')['day_entries'] as $dayEntry) {
66
            $timers[] = [
67
                'id' => $dayEntry['id'],
68
                'description' => $dayEntry['notes'],
69
                'project_name' => $dayEntry['project'],
70
                'tags' => $dayEntry['task'],
71
                'duration' => $dayEntry['hours'] * 60 * 60,
72
            ];
73
        }
74
75
        return array_reverse($timers);
76
    }
77
78
    /**
79
     * @param  $data
80
     * @return mixed
81
     */
82
    public function getTags($data)
83
    {
84
        return $this->getItems('tasks', $data);
85
    }
86
87
    /**
88
     * @param  $description
89
     * @param  $projectId
90
     * @param  $taskId
91
     * @return mixed
92
     */
93
    public function startTimer($description, $projectId, $taskId)
94
    {
95
        $harvestId = null;
96
97
        $item = [
98
            'notes'      => $description,
99
            'project_id' => $projectId,
100
            'task_id'    => $taskId,
101
        ];
102
103
        $data = $this->timerAction('start', 'daily/add/', ['json' => $item]);
104
105
        if (isset($data['id']) === true) {
106
            $harvestId = $data['id'];
107
        }
108
109
        return $harvestId;
110
    }
111
112
    /**
113
     * @param  $timerId
114
     * @return mixed
115
     */
116
    public function stopTimer($timerId = null)
117
    {
118
        if ($this->isTimerRunning($timerId) === false) {
119
            return false;
120
        }
121
122
        return $this->timerAction('stop', 'daily/timer/' . $timerId);
123
    }
124
125
    /**
126
     * @param  $needle
127
     * @param  array     $haystack
128
     * @return mixed
129
     */
130
    private function getItems($needle, array $haystack = [])
131
    {
132
        $items = [];
133
134
        if (isset($haystack[$needle]) === false) {
135
            return [];
136
        }
137
138
        foreach ($haystack[$needle] as $item) {
139
            $items[] = [
140
                'name' => $item[key($item)]['name'],
141
                'id'   => $item[key($item)]['id'],
142
            ];
143
        }
144
145
        return $items;
146
    }
147
148
    /**
149
     * @param  $timerId
150
     * @return boolean
151
     */
152
    private function isTimerRunning($timerId)
153
    {
154
        $data = $this->timerAction('timer_running', 'daily/show/' . $timerId);
155
156
        return isset($data['timer_started_at']);
157
    }
158
159
    /**
160
     * @param  string  $action
161
     * @param  string  $apiUri
162
     * @return mixed
163
     */
164
    private function timerAction($action, $apiUri, array $options = [])
165
    {
166
        $returnDataFor = [
167
            'start',
168
            'timer_running',
169
            'get_projects',
170
            'get_tags',
171
            'get_recent_timers',
172
        ];
173
        $methods = [
174
            'start'             => 'post',
175
            'stop'              => 'get',
176
            'delete'            => 'delete',
177
            'timer_running'     => 'get',
178
            'get_projects'      => 'get',
179
            'get_tags'          => 'get',
180
            'get_recent_timers' => 'get',
181
        ];
182
183
        $method = isset($methods[$action]) ? $methods[$action] : '';
184
185
        return $this->serviceApiCall->send(
186
            $method,
187
            $apiUri,
188
            $options,
189
            in_array($action, $returnDataFor)
190
        );
191
    }
192
}
193