1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Service\WorkspaceUsers; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Http\Request\WorkspaceUsers\DeleteWorkspaceUser; |
6
|
|
|
use Marek\Toggable\API\Http\Request\WorkspaceUsers\GetWorkspaceUsers; |
7
|
|
|
use Marek\Toggable\API\Http\Request\WorkspaceUsers\InviteUserToWorkspace; |
8
|
|
|
use Marek\Toggable\API\Http\Request\WorkspaceUsers\UpdateWorkspaceUser; |
9
|
|
|
use Marek\Toggable\API\Http\Response\WorkspaceUsers\Invitation; |
10
|
|
|
use Marek\Toggable\API\Http\Response\WorkspaceUsers\WorkspaceUser; |
11
|
|
|
use Marek\Toggable\API\Http\Response\WorkspaceUsers\WorkspaceUsers; |
12
|
|
|
use Marek\Toggable\API\Toggl\Values\Notification; |
13
|
|
|
use Marek\Toggable\API\Toggl\Values\Workspace\User; |
14
|
|
|
use Marek\Toggable\Service\AbstractService; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class WorkspaceUsersService |
18
|
|
|
* @package Marek\Toggable\Service\WorkspaceUsers |
19
|
|
|
*/ |
20
|
|
|
class WorkspaceUsersService extends AbstractService implements \Marek\Toggable\API\Toggl\WorkspaceUsersServiceInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritDoc |
24
|
|
|
*/ |
25
|
2 |
|
public function inviteUsers($workspaceId, array $emails) |
26
|
|
|
{ |
27
|
2 |
|
$request = new InviteUserToWorkspace( |
28
|
|
|
array( |
29
|
2 |
|
'workspaceId' => $this->validate($workspaceId), |
30
|
1 |
|
'data' => $emails, |
31
|
|
|
) |
32
|
1 |
|
); |
33
|
|
|
|
34
|
1 |
|
$response = $this->delegate($request); |
35
|
|
|
|
36
|
1 |
|
$workspaceUsers = array(); |
37
|
1 |
|
foreach($response->body['data'] as $workspaceUser) { |
38
|
1 |
|
$workspaceUsers[] = $this->hydrator->hydrate($workspaceUser, new User()); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
$notifications = array(); |
42
|
1 |
|
foreach($response->body['notifications'] as $notification) { |
43
|
1 |
|
$notifications[] = new Notification( |
44
|
|
|
array( |
45
|
1 |
|
'message' => $notification, |
46
|
|
|
) |
47
|
1 |
|
); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
return new Invitation( |
51
|
|
|
array( |
52
|
1 |
|
'workspaceUsers' => $workspaceUsers, |
53
|
1 |
|
'notifications' => $notifications, |
54
|
|
|
) |
55
|
1 |
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
2 |
|
public function updateWorkspaceUser($workspaceUserId, \Marek\Toggable\API\Toggl\Values\Workspace\User $user) |
62
|
|
|
{ |
63
|
2 |
|
$request = new UpdateWorkspaceUser( |
64
|
|
|
array( |
65
|
2 |
|
'workspaceUserId' => $this->validate($workspaceUserId), |
66
|
1 |
|
'data' => $this->extractDataFromObject($user), |
67
|
|
|
) |
68
|
1 |
|
); |
69
|
|
|
|
70
|
1 |
|
$response = $this->delegate($request); |
71
|
|
|
|
72
|
1 |
|
return new WorkspaceUser( |
73
|
|
|
array( |
74
|
1 |
|
'workspaceUser' => $this->hydrateDataFromArrayToObject($response, new User()) |
75
|
1 |
|
) |
76
|
1 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
2 |
|
public function deleteWorkspaceUser($workspaceUserId) |
83
|
|
|
{ |
84
|
2 |
|
$request = new DeleteWorkspaceUser( |
85
|
|
|
array( |
86
|
2 |
|
'workspaceUserId' => $this->validate($workspaceUserId), |
87
|
|
|
) |
88
|
1 |
|
); |
89
|
|
|
|
90
|
1 |
|
return $this->delegate($request); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
2 |
|
public function getWorkspaceUsers($workspaceId) |
|
|
|
|
97
|
|
|
{ |
98
|
2 |
|
$request = new GetWorkspaceUsers( |
99
|
|
|
array( |
100
|
2 |
|
'workspaceId' => $this->validate($workspaceId), |
101
|
|
|
) |
102
|
1 |
|
); |
103
|
|
|
|
104
|
1 |
|
$response = $this->delegate($request); |
105
|
|
|
|
106
|
1 |
|
$workspaceUsers = array(); |
107
|
1 |
|
foreach($response->body as $workspaceUser) { |
108
|
1 |
|
$workspaceUsers[] = $this->hydrator->hydrate($workspaceUser, new User()); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
return new WorkspaceUsers( |
112
|
|
|
array( |
|
|
|
|
113
|
|
|
'workspaceUsers' => $workspaceUsers, |
114
|
|
|
) |
115
|
1 |
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.