Completed
Push — master ( d20c63...0b7335 )
by Guillaume
02:17
created

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