1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana; |
4
|
|
|
|
5
|
|
|
use Helix\Asana\Base\AbstractEntity; |
6
|
|
|
use Helix\Asana\User\Photo; |
7
|
|
|
use Helix\Asana\User\TaskList; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A user. |
11
|
|
|
* |
12
|
|
|
* @see https://developers.asana.com/docs/asana-users |
13
|
|
|
* @see https://developers.asana.com/docs/user |
14
|
|
|
* |
15
|
|
|
* @immutable Users cannot be altered via the API. |
16
|
|
|
* |
17
|
|
|
* @method string getEmail () |
18
|
|
|
* @method string getName () |
19
|
|
|
* @method null|Photo getPhoto () |
20
|
|
|
* @method Workspace[] getWorkspaces () |
21
|
|
|
*/ |
22
|
|
|
class User extends AbstractEntity { |
23
|
|
|
|
24
|
|
|
const TYPE = 'user'; |
25
|
|
|
|
26
|
|
|
protected static $map = [ |
27
|
|
|
'photo' => Photo::class, |
28
|
|
|
'workspaces' => [Workspace::class] |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
final public function __toString (): string { |
32
|
|
|
return "users/{$this->getGid()}"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Workspace $workspace |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function addToWorkspace (Workspace $workspace) { |
40
|
|
|
return $this->_addWithPost("{$workspace}/addUser", [ |
41
|
|
|
'user' => $this->getGid() |
42
|
|
|
], 'workspaces', [$workspace]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
public function getCacheKeys () { |
49
|
|
|
$keys = parent::getCacheKeys(); |
50
|
|
|
|
51
|
|
|
// only include email as a key if it's loaded |
52
|
|
|
if (isset($this->data['email'])) { |
53
|
|
|
$keys[] = "users/{$this->data['email']}"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $keys; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the first known workspace for the user. |
61
|
|
|
* You should only rely on this if the authorized user is in exactly one workspace. |
62
|
|
|
* |
63
|
|
|
* @return Workspace |
64
|
|
|
*/ |
65
|
|
|
public function getDefaultWorkspace () { |
66
|
|
|
return $this->getWorkspaces()[0]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
71
|
|
|
* @return Portfolio[] |
72
|
|
|
*/ |
73
|
|
|
public function getFavoritePortfolios (Workspace $workspace = null) { |
74
|
|
|
return $this->getFavorites(Portfolio::class, Portfolio::TYPE, $workspace); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
79
|
|
|
* @return Project[] |
80
|
|
|
*/ |
81
|
|
|
public function getFavoriteProjects (Workspace $workspace = null) { |
82
|
|
|
return $this->getFavorites(Project::class, Project::TYPE, $workspace); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
87
|
|
|
* @return Tag[] |
88
|
|
|
*/ |
89
|
|
|
public function getFavoriteTags (Workspace $workspace = null) { |
90
|
|
|
return $this->getFavorites(Tag::class, Tag::TYPE, $workspace); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
95
|
|
|
* @return Team[] |
96
|
|
|
*/ |
97
|
|
|
public function getFavoriteTeams (Workspace $workspace = null) { |
98
|
|
|
return $this->getFavorites(Team::class, Team::TYPE, $workspace); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
103
|
|
|
* @return User[] |
104
|
|
|
*/ |
105
|
|
|
public function getFavoriteUsers (Workspace $workspace = null) { |
106
|
|
|
return $this->getFavorites(self::class, self::TYPE, $workspace); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $class |
111
|
|
|
* @param string $resourceType |
112
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
protected function getFavorites (string $class, string $resourceType, Workspace $workspace = null) { |
116
|
|
|
return $this->loadAll($class, "{$this}/favorites", [ |
117
|
|
|
'resource_type' => $resourceType, |
118
|
|
|
'workspace' => ($workspace ?? $this->getDefaultWorkspace())->getGid() |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns all incomplete tasks in the given workspace that are assigned to the user. |
124
|
|
|
* |
125
|
|
|
* @see https://developers.asana.com/docs/get-multiple-tasks |
126
|
|
|
* |
127
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
128
|
|
|
* @return Task[] |
129
|
|
|
*/ |
130
|
|
|
public function getIncompleteTasks (Workspace $workspace = null) { |
131
|
|
|
$filter = ['completed_since' => 'now']; |
132
|
|
|
if ($workspace) { |
133
|
|
|
$filter['workspace'] = $workspace->getGid(); |
134
|
|
|
} |
135
|
|
|
return $this->getTasks($filter); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param null|Workspace $workspace |
140
|
|
|
* @return Portfolio[] |
141
|
|
|
*/ |
142
|
|
|
public function getPortfolios (Workspace $workspace = null) { |
143
|
|
|
return $this->loadAll(Portfolio::class, "portfolios", [ |
144
|
|
|
'workspace' => ($workspace ?? $this->getDefaultWorkspace())->getGid(), |
145
|
|
|
'owner' => $this->getGid() |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param null|Workspace $workspace Falls back to the default workspace. |
151
|
|
|
* @return TaskList |
152
|
|
|
*/ |
153
|
|
|
public function getTaskList (Workspace $workspace = null) { |
154
|
|
|
return $this->load(TaskList::class, "{$this}/user_task_list", [ |
155
|
|
|
'workspace' => ($workspace ?? $this->getDefaultWorkspace())->getGid() |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns all tasks in the given workspace that are assigned to the user. |
161
|
|
|
* |
162
|
|
|
* @see https://developers.asana.com/docs/get-multiple-tasks |
163
|
|
|
* |
164
|
|
|
* @param string[] $filter `workspace` falls back to the default. |
165
|
|
|
* @return Task[] |
166
|
|
|
*/ |
167
|
|
|
public function getTasks (array $filter = []) { |
168
|
|
|
$filter['assignee'] = $this->getGid(); |
169
|
|
|
$filter += ['workspace' => $this->getDefaultWorkspace()->getGid()]; |
170
|
|
|
return $this->loadAll(Task::class, 'tasks', $filter); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* The user's teams. |
175
|
|
|
* |
176
|
|
|
* @see https://developers.asana.com/docs/get-teams-for-a-user |
177
|
|
|
* |
178
|
|
|
* @param null|Workspace $organization Falls back to the default workspace. |
179
|
|
|
* @return Team[] |
180
|
|
|
*/ |
181
|
|
|
public function getTeams (Workspace $organization = null) { |
182
|
|
|
return $this->loadAll(Team::class, "{$this}/teams", [ |
183
|
|
|
'organization' => ($organization ?? $this->getDefaultWorkspace())->getGid() |
184
|
|
|
]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getUrl (): string { |
188
|
|
|
return "https://app.asana.com/0/{$this->getGid()}/list"; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param Workspace $workspace |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function removeFromWorkspace (Workspace $workspace) { |
196
|
|
|
return $this->_removeWithPost("{$workspace}/removeUser", [ |
197
|
|
|
'user' => $this->getGid() |
198
|
|
|
], 'workspaces', [$workspace]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param callable $filter `fn( Task $task ): bool` |
203
|
|
|
* @param array $apiFilter Given to the API to reduce network load. |
204
|
|
|
* @return Task[] |
205
|
|
|
*/ |
206
|
|
|
public function selectTasks (callable $filter, array $apiFilter) { |
207
|
|
|
return $this->_select($this->getTasks($apiFilter), $filter); |
208
|
|
|
} |
209
|
|
|
} |