1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Service\TimeEntry; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Marek\Toggable\API\Http\Request\RequestInterface; |
7
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\BulkUpdateTimeEntriesTags; |
8
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\CreateTimeEntry; |
9
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\DeleteTimeEntry; |
10
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\GetRunningTimeEntry; |
11
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\GetTimeEntriesStartedInDateRange; |
12
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\GetTimeEntry; |
13
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\StartTimeEntry; |
14
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\StopTimeEntry; |
15
|
|
|
use Marek\Toggable\API\Http\Request\TimeEntry\UpdateTimeEntry; |
16
|
|
|
use Marek\Toggable\API\Http\Response\TimeEntry\TimeEntries; |
17
|
|
|
use Marek\Toggable\API\Http\Response\TimeEntry\TimeEntry as TimeEntryResponse; |
18
|
|
|
use Marek\Toggable\API\Toggl\Values\TimeEntry\TimeEntry; |
19
|
|
|
use Marek\Toggable\Service\AbstractService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class TimeEntryService |
23
|
|
|
* @package Marek\Toggable\Service\TimeEntry |
24
|
|
|
*/ |
25
|
|
|
class TimeEntryService extends AbstractService implements \Marek\Toggable\API\Toggl\TimeEntryServiceInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
1 |
|
public function createTimeEntry(\Marek\Toggable\API\Toggl\Values\TimeEntry\TimeEntry $timeEntry) |
31
|
|
|
{ |
32
|
1 |
|
$request = new CreateTimeEntry( |
33
|
|
|
array( |
34
|
1 |
|
'data' => $this->extractDataFromObject($timeEntry), |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
|
38
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
1 |
|
public function startTimeEntry(\Marek\Toggable\API\Toggl\Values\TimeEntry\TimeEntry $timeEntry) |
45
|
|
|
{ |
46
|
1 |
|
$request = new StartTimeEntry( |
47
|
|
|
array( |
48
|
1 |
|
'data' => $this->extractDataFromObject($timeEntry), |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
|
52
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
2 |
|
public function stopTimeEntry($timeEntryId) |
|
|
|
|
59
|
|
|
{ |
60
|
2 |
|
if (empty($timeEntryId) || !is_int($timeEntryId)) { |
61
|
1 |
|
throw new InvalidArgumentException( |
62
|
1 |
|
sprintf('$timeEntryId argument not provided in %s', get_class($this)) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$request = new StopTimeEntry( |
67
|
|
|
array( |
68
|
1 |
|
'timeEntryId' => $timeEntryId, |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
|
72
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
2 |
|
public function getTimeEntry($timeEntryId) |
79
|
|
|
{ |
80
|
2 |
|
if (empty($timeEntryId) || !is_int($timeEntryId)) { |
81
|
1 |
|
throw new InvalidArgumentException( |
82
|
1 |
|
sprintf('$timeEntryId argument not provided in %s', get_class($this)) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$request = new GetTimeEntry( |
87
|
|
|
array( |
88
|
1 |
|
'timeEntryId' => $timeEntryId, |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
1 |
|
public function getRunningTimeEntry() |
99
|
|
|
{ |
100
|
1 |
|
$request = new GetRunningTimeEntry(); |
101
|
|
|
|
102
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
2 |
|
public function updateTimeEntry($timeEntryId, \Marek\Toggable\API\Toggl\Values\TimeEntry\TimeEntry $timeEntry) |
109
|
|
|
{ |
110
|
2 |
|
if (empty($timeEntryId) || !is_int($timeEntryId)) { |
111
|
1 |
|
throw new InvalidArgumentException( |
112
|
1 |
|
sprintf('$timeEntryId argument not provided in %s', get_class($this)) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$request = new UpdateTimeEntry( |
117
|
|
|
array( |
118
|
1 |
|
'timeEntryId' => $timeEntryId, |
119
|
1 |
|
'data' => $this->extractDataFromObject($timeEntry), |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
|
123
|
1 |
|
return $this->delegateHydrateAndReturnResponse($request); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
2 |
|
public function deleteTimeEntry($timeEntryId) |
|
|
|
|
130
|
|
|
{ |
131
|
2 |
|
if (empty($timeEntryId) || !is_int($timeEntryId)) { |
132
|
1 |
|
throw new InvalidArgumentException( |
133
|
1 |
|
sprintf('$timeEntryId argument not provided in %s', get_class($this)) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$request = new DeleteTimeEntry( |
138
|
|
|
array( |
139
|
1 |
|
'timeEntryId' => $timeEntryId, |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
|
143
|
1 |
|
return $this->delegate($request); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritDoc |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function getTimeEntriesStartedInDateRange(\DateTime $startDate, \DateTime $endDate) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$request = new GetTimeEntriesStartedInDateRange( |
152
|
|
|
array( |
153
|
|
|
'startDate' => $startDate, |
154
|
|
|
'endDate' => $endDate, |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$response = $this->delegate($request); |
159
|
|
|
|
160
|
|
|
$entries = array(); |
161
|
|
|
foreach ($response->body as $entry) { |
162
|
|
|
$entries[] = $this->hydrator->hydrate($entry, new TimeEntry()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return new TimeEntries( |
166
|
|
|
array( |
167
|
|
|
'timeEntries' => $entries, |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function bulkUpdateTimeEntriesTags(array $timeEntries, array $tags, $tagAction = \Marek\Toggable\API\Toggl\Values\TagAction::ADD) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$request = new BulkUpdateTimeEntriesTags( |
178
|
|
|
array( |
179
|
|
|
'timeEntries' => $timeEntries, |
180
|
|
|
'tags' => $tags, |
181
|
|
|
'tagAction' => $tagAction, |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$response = $this->delegate($request); |
186
|
|
|
|
187
|
|
|
$entries = array(); |
188
|
|
|
foreach ($response->body['data'] as $entry) { |
189
|
|
|
$entries[] = $this->hydrator->hydrate($entry, new TimeEntry()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return new TimeEntries( |
193
|
|
|
array( |
194
|
|
|
'timeEntries' => $entries, |
195
|
|
|
) |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Response helper method |
201
|
|
|
* |
202
|
|
|
* @param \Marek\Toggable\API\Http\Request\RequestInterface $request |
203
|
|
|
* |
204
|
|
|
* @return \Marek\Toggable\API\Http\Response\TimeEntry\TimeEntry |
205
|
|
|
*/ |
206
|
6 |
|
protected function delegateHydrateAndReturnResponse(RequestInterface $request) |
207
|
|
|
{ |
208
|
6 |
|
$response = $this->delegate($request); |
209
|
|
|
|
210
|
6 |
|
return new TimeEntryResponse( |
211
|
|
|
array( |
212
|
6 |
|
'timeEntry' => $this->hydrateDataFromArrayToObject($response, new TimeEntry()), |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.