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

Harvest::getProjects()   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 1
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
    /**
62
     * @param  $data
63
     * @return mixed
64
     */
65
    public function getTags($data)
66
    {
67
        return $this->getItems('tasks', $data);
68
    }
69
70
    /**
71
     * @param  $description
72
     * @param  $projectId
73
     * @param  $taskId
74
     * @return mixed
75
     */
76
    public function startTimer($description, $projectId, $taskId)
77
    {
78
        $harvestId = null;
79
80
        $item = [
81
            'notes'      => $description,
82
            'project_id' => $projectId,
83
            'task_id'    => $taskId,
84
        ];
85
86
        $data = $this->timerAction('start', 'daily/add/', ['json' => $item]);
87
88
        if (isset($data['id']) === true) {
89
            $harvestId = $data['id'];
90
        }
91
92
        return $harvestId;
93
    }
94
95
    /**
96
     * @param  $timerId
97
     * @return mixed
98
     */
99
    public function stopTimer($timerId = null)
100
    {
101
        if ($this->isTimerRunning($timerId) === false) {
102
            return false;
103
        }
104
105
        return $this->timerAction('stop', 'daily/timer/' . $timerId);
106
    }
107
108
    /**
109
     * @param  $needle
110
     * @param  array     $haystack
111
     * @return mixed
112
     */
113
    private function getItems($needle, array $haystack = [])
114
    {
115
        $items = [];
116
117
        if (isset($haystack[$needle]) === false) {
118
            return [];
119
        }
120
121
        foreach ($haystack[$needle] as $item) {
122
            $items[] = [
123
                'name' => $item[key($item)]['name'],
124
                'id'   => $item[key($item)]['id'],
125
            ];
126
        }
127
128
        return $items;
129
    }
130
131
    /**
132
     * @param  $timerId
133
     * @return boolean
134
     */
135
    private function isTimerRunning($timerId)
136
    {
137
        $data = $this->timerAction('timer_running', 'daily/show/' . $timerId);
138
139
        return isset($data['timer_started_at']);
140
    }
141
142
    /**
143
     * @param  string  $action
144
     * @param  string  $apiUri
145
     * @return mixed
146
     */
147
    private function timerAction($action, $apiUri, array $options = [])
148
    {
149
        $returnDataFor = [
150
            'start',
151
            'timer_running',
152
            'get_projects',
153
            'get_tags',
154
        ];
155
        $methods = [
156
            'start'         => 'post',
157
            'stop'          => 'get',
158
            'delete'        => 'delete',
159
            'timer_running' => 'get',
160
            'get_projects'  => 'get',
161
            'get_tags'      => 'get',
162
            '',
163
        ];
164
165
        $method = isset($methods[$action]) ? $methods[$action] : '';
166
167
        return $this->serviceApiCall->send(
168
            $method,
169
            $apiUri,
170
            $options,
171
            in_array($action, $returnDataFor)
172
        );
173
    }
174
}
175