Completed
Push — master ( 1f74ae...d5972d )
by Mario
03:04
created

TimeEntryService   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 20
c 7
b 0
f 2
lcom 1
cbo 15
dl 0
loc 196
ccs 66
cts 66
cp 1
rs 9.1666

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createTimeEntry() 0 10 1
A startTimeEntry() 0 10 1
A stopTimeEntry() 0 16 3
A getTimeEntry() 0 16 3
A getRunningTimeEntry() 0 6 1
A updateTimeEntry() 0 17 3
A deleteTimeEntry() 0 16 3
A getTimeEntriesStartedInDateRange() 0 22 2
B bulkUpdateTimeEntriesTags() 0 27 2
A delegateHydrateAndReturnResponse() 0 10 1
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)
0 ignored issues
show
Duplication introduced by
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...
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)
0 ignored issues
show
Duplication introduced by
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...
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 1
    public function getTimeEntriesStartedInDateRange(\DateTime $startDate, \DateTime $endDate)
0 ignored issues
show
Duplication introduced by
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...
150
    {
151 1
        $request = new GetTimeEntriesStartedInDateRange(
152
            array(
153 1
                'startDate' => $startDate,
154 1
                'endDate'   => $endDate,
155
            )
156
        );
157
158 1
        $response = $this->delegate($request);
159
160 1
        $entries = array();
161 1
        foreach ($response->body as $entry) {
162 1
            $entries[] = $this->hydrator->hydrate($entry, new TimeEntry());
163
        }
164
165 1
        return new TimeEntries(
166
            array(
167 1
                'timeEntries' => $entries,
168
            )
169
        );
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175 1
    public function bulkUpdateTimeEntriesTags(array $timeEntries, array $tags, $tagAction = \Marek\Toggable\API\Toggl\Values\TagAction::ADD)
176
    {
177
        $data = array(
178 1
            'tags' => $tags,
179 1
            'tag_action' => $tagAction
180
        );
181
182 1
        $request = new BulkUpdateTimeEntriesTags(
183
            array(
184 1
                'timeEntryIds'   => $timeEntries,
185 1
                'data'           => $data,
186
            )
187
        );
188
189 1
        $response = $this->delegate($request);
190
191 1
        $entries = array();
192 1
        foreach ($response->body['data'] as $entry) {
193 1
            $entries[] = $this->hydrator->hydrate($entry, new TimeEntry());
194
        }
195
196 1
        return new TimeEntries(
197
            array(
198 1
                'timeEntries' => $entries,
199
            )
200
        );
201
    }
202
203
    /**
204
     * Response helper method
205
     *
206
     * @param \Marek\Toggable\API\Http\Request\RequestInterface $request
207
     *
208
     * @return \Marek\Toggable\API\Http\Response\TimeEntry\TimeEntry
209
     */
210 6
    protected function delegateHydrateAndReturnResponse(RequestInterface $request)
211
    {
212 6
        $response = $this->delegate($request);
213
214 6
        return new TimeEntryResponse(
215
            array(
216 6
                'timeEntry' => $this->hydrateDataFromArrayToObject($response, new TimeEntry()),
217
            )
218
        );
219
    }
220
}
221