Completed
Push — master ( 3b0b6a...263c36 )
by Guillaume
02:53
created

src/Harvest.php (3 issues)

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