1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Service\Client; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Http\Request\Client\UpdateClient; |
6
|
|
|
use Marek\Toggable\API\Http\Response\Client\Client as ClientResponse; |
7
|
|
|
use Marek\Toggable\API\Http\Response\Client\Clients as ClientsResponse; |
8
|
|
|
use Marek\Toggable\API\Http\Response\Project\Projects as ProjectsResponse; |
9
|
|
|
use Marek\Toggable\API\Toggl\Values\Project\Project as ProjectValue; |
10
|
|
|
use Marek\Toggable\API\Toggl\Values\Client\Client as ClientValue; |
11
|
|
|
use Marek\Toggable\API\Http\Request\Client\CreateClient as CreateClientRequest; |
12
|
|
|
use Marek\Toggable\API\Http\Request\Client\GetClientDetails as GetClientDetailsRequest; |
13
|
|
|
use Marek\Toggable\API\Http\Request\Client\GetClientProjects as GetClientProjectsRequest; |
14
|
|
|
use Marek\Toggable\API\Http\Request\Client\GetClients as GetClientsRequest; |
15
|
|
|
use Marek\Toggable\API\Http\Request\Client\DeleteClient as DeleteClientRequest; |
16
|
|
|
use Marek\Toggable\Service\AbstractService; |
17
|
|
|
use Marek\Toggable\API\Toggl\Values\Activity; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ClientService |
21
|
|
|
* @package Marek\Toggable\Service\Client |
22
|
|
|
*/ |
23
|
|
|
class ClientService extends AbstractService implements \Marek\Toggable\API\Toggl\ClientServiceInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
1 |
|
public function createClient(ClientValue $client) |
29
|
|
|
{ |
30
|
1 |
|
$request = new CreateClientRequest( |
31
|
|
|
array( |
32
|
1 |
|
'data' => $this->extractDataFromObject($client), |
33
|
|
|
) |
34
|
1 |
|
); |
35
|
|
|
|
36
|
1 |
|
$response = $this->delegate($request); |
37
|
|
|
|
38
|
1 |
|
return new ClientResponse( |
39
|
|
|
array( |
40
|
1 |
|
'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()), |
41
|
|
|
) |
42
|
1 |
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function getClientDetails($clientId) |
49
|
|
|
{ |
50
|
2 |
|
$request = new GetClientDetailsRequest( |
51
|
|
|
array( |
52
|
2 |
|
'clientId' => $this->validate($clientId), |
53
|
|
|
) |
54
|
1 |
|
); |
55
|
|
|
|
56
|
1 |
|
$response = $this->delegate($request); |
57
|
|
|
|
58
|
1 |
|
return new ClientResponse( |
59
|
|
|
array( |
60
|
1 |
|
'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()) |
61
|
1 |
|
) |
62
|
1 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function getClients() |
69
|
|
|
{ |
70
|
1 |
|
$request = new GetClientsRequest(); |
71
|
|
|
|
72
|
1 |
|
$response = $this->delegate($request); |
73
|
|
|
|
74
|
1 |
|
$clients = array(); |
75
|
1 |
|
foreach($response->body as $client) { |
76
|
1 |
|
$clients[] = $this->hydrator->hydrate($client, new ClientValue); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return new ClientsResponse( |
80
|
|
|
array( |
81
|
1 |
|
'clients' => $clients, |
82
|
|
|
) |
83
|
1 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function getClientProjects($clientId, $active = Activity::ACTIVE) |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
$request = new GetClientProjectsRequest( |
92
|
|
|
array( |
93
|
1 |
|
'clientId' => $this->validate($clientId), |
94
|
1 |
|
'active' => $active, |
95
|
|
|
) |
96
|
1 |
|
); |
97
|
|
|
|
98
|
1 |
|
$response = $this->delegate($request); |
99
|
|
|
|
100
|
1 |
|
$projects = array(); |
101
|
1 |
|
foreach ($response->body as $project) { |
102
|
1 |
|
$projects[] = $this->hydrator->hydrate($project, new ProjectValue()); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
return new ProjectsResponse(array('projects' => $projects)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
2 |
|
public function updateClient($clientId, ClientValue $client) |
|
|
|
|
112
|
|
|
{ |
113
|
2 |
|
$request = new UpdateClient( |
114
|
|
|
array( |
115
|
2 |
|
'clientId' => $this->validate($clientId), |
116
|
1 |
|
'data' => $this->extractDataFromObject($client), |
117
|
|
|
) |
118
|
1 |
|
); |
119
|
|
|
|
120
|
1 |
|
$response = $this->delegate($request); |
121
|
|
|
|
122
|
1 |
|
return new ClientResponse( |
123
|
|
|
array( |
124
|
1 |
|
'client' => $this->hydrateDataFromArrayToObject($response, new ClientValue()), |
125
|
|
|
) |
126
|
1 |
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function deleteClient($clientId) |
133
|
|
|
{ |
134
|
2 |
|
$request = new DeleteClientRequest( |
135
|
|
|
array( |
136
|
2 |
|
'clientId' => $this->validate($clientId), |
137
|
|
|
) |
138
|
1 |
|
); |
139
|
|
|
|
140
|
1 |
|
return $this->delegate($request); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.