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