Passed
Push — master ( 866c3b...50a671 )
by y
08:13
created

Workspace::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helix\Asana;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\UpdateTrait;
7
use Helix\Asana\Webhook\ProjectWebhook;
8
use Helix\Asana\Webhook\TaskWebhook;
9
use Helix\Asana\Workspace\OrganizationExport;
10
11
/**
12
 * A workspace.
13
 *
14
 * Workspaces cannot be created or deleted through the API, but they can be updated.
15
 *
16
 * @see https://developers.asana.com/docs/asana-workspaces
17
 * @see https://developers.asana.com/docs/workspace
18
 *
19
 * @method string[] getEmailDomains     ()
20
 * @method string[] selectEmailDomains  () `fn( string $emailDomain ): bool`
21
 * @method string   getName             ()
22
 * @method $this    setName             (string $name)
23
 */
24
class Workspace extends AbstractEntity {
25
26
    use UpdateTrait;
27
28
    const TYPE = 'workspace';
29
30
    final public function __toString (): string {
31
        return "workspaces/{$this->getGid()}";
32
    }
33
34
    /**
35
     * Initiates and returns an asynchronous export for the organization.
36
     *
37
     * @return OrganizationExport
38
     */
39
    public function export () {
40
        /** @var OrganizationExport $export */
41
        $export = $this->factory(OrganizationExport::class);
42
        return $export->create($this);
43
    }
44
45
    /**
46
     * Finds entities via the typeahead endpoint.
47
     *
48
     * This endpoint is fuzzy, so you should prefer using the selection methods for precision.
49
     *
50
     * The typeahead endpoint isn't very good for finding users, so that method doesn't exist in this class.
51
     *
52
     * @see https://developers.asana.com/docs/get-objects-via-typeahead
53
     *
54
     * @param string $class
55
     * @param string $resourceType
56
     * @param string $text
57
     * @param int $limit 1-100
58
     * @return array|AbstractEntity[]
59
     */
60
    protected function find (string $class, string $resourceType, string $text, int $limit = 20) {
61
        return $this->loadAll($class, "{$this}/typeahead", [
62
            'resource_type' => $resourceType,
63
            'query' => $text,
64
            'count' => $limit
65
        ]);
66
    }
67
68
    /**
69
     * Finds custom fields via the typeahead endpoint.
70
     *
71
     * @param string $text
72
     * @param int $limit 1-100
73
     * @return CustomField[]
74
     */
75
    public function findCustomFields (string $text, int $limit = 20) {
76
        return $this->find(CustomField::class, CustomField::TYPE, $text, $limit);
77
    }
78
79
    /**
80
     * Finds portfolios via the typeahead endpoint.
81
     *
82
     * @param string $text
83
     * @param int $limit 1-100
84
     * @return Portfolio[]
85
     */
86
    public function findPortfolios (string $text, int $limit = 20) {
87
        return $this->find(Portfolio::class, Portfolio::TYPE, $text, $limit);
88
    }
89
90
    /**
91
     * Finds projects via the typeahead endpoint.
92
     *
93
     * @param string $text
94
     * @param int $limit 1-100
95
     * @return Project[]
96
     */
97
    public function findProjects (string $text, int $limit = 20) {
98
        return $this->find(Project::class, Project::TYPE, $text, $limit);
99
    }
100
101
    /**
102
     * Finds tags via the typeahead endpoint.
103
     *
104
     * @param string $text
105
     * @param int $limit 1-100
106
     * @return Tag[]
107
     */
108
    public function findTags (string $text, int $limit = 20) {
109
        return $this->find(Tag::class, Tag::TYPE, $text, $limit);
110
    }
111
112
    /**
113
     * Finds tasks via the typeahead endpoint.
114
     *
115
     * @param string $text
116
     * @param int $limit 1-100
117
     * @return Task[]
118
     */
119
    public function findTasks (string $text, int $limit = 20) {
120
        return $this->find(Task::class, Task::TYPE, $text, $limit);
121
    }
122
123
    /**
124
     * @return CustomField[]
125
     */
126
    public function getCustomFields () {
127
        return $this->loadAll(CustomField::class, "{$this}/custom_fields");
128
    }
129
130
    /**
131
     * Returns the authenticated user's portfolios in this workspace.
132
     *
133
     * @see https://developers.asana.com/docs/get-multiple-portfolios
134
     *
135
     * @return Portfolio[]
136
     */
137
    public function getPortfolios () {
138
        return $this->loadAll(Portfolio::class, "portfolios", [
139
            'workspace' => $this->getGid(),
140
            'owner' => $this->api->getMe()->getGid() // the only allowed value, but still required.
141
        ]);
142
    }
143
144
    /**
145
     * The workspace's projects.
146
     *
147
     * @see https://developers.asana.com/docs/get-multiple-projects
148
     *
149
     * @param array $filter
150
     * @return Project[]
151
     */
152
    public function getProjects (array $filter = ['archived' => false]) {
153
        $filter['workspace'] = $this->getGid();
154
        return $this->loadAll(Project::class, 'projects', $filter);
155
    }
156
157
    /**
158
     * @return Tag[]
159
     */
160
    public function getTags () {
161
        return $this->loadAll(Tag::class, 'tags', ['workspace' => $this->getGid()]);
162
    }
163
164
    /**
165
     * @return Team[]
166
     */
167
    public function getTeams () {
168
        return $this->loadAll(Team::class, "organizations/{$this->getGid()}/teams");
169
    }
170
171
    /**
172
     * @param string $email
173
     * @return null|User
174
     */
175
    public function getUserByEmail (string $email) {
176
        return $this->api->getCache()->get("users/{$email}", $this, function() use ($email) {
177
            foreach ($this->getUsers() as $user) {
178
                if ($user->getEmail() === $email) {
179
                    return $user;
180
                }
181
            }
182
            return null;
183
        });
184
    }
185
186
    /**
187
     * @return User[]
188
     */
189
    public function getUsers () {
190
        return $this->loadAll(User::class, "{$this}/users");
191
    }
192
193
    /**
194
     * Returns all webhooks belonging to the workspace.
195
     *
196
     * @return ProjectWebhook[]|TaskWebhook[]
197
     */
198
    public function getWebhooks () {
199
        /** @var array $all */
200
        $all = $this->api->get('webhooks', ['workspace' => $this->getGid()], ['expand' => 'this']);
201
        return array_map(function(array $each) {
202
            return $this->api->getCache()->get($each['gid'], $this, function() use ($each) {
203
                static $classes = [
204
                    Project::TYPE => ProjectWebhook::class,
205
                    Task::TYPE => TaskWebhook::class
206
                ];
207
                return $this->factory($classes[$each['resource_type']], $each);
208
            });
209
        }, $all);
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function isOrganization (): bool {
216
        return $this->_is('is_organization');
217
    }
218
219
    /**
220
     * Returns a new custom field for the workspace.
221
     *
222
     * @return CustomField
223
     */
224
    public function newCustomField () {
225
        /** @var CustomField $field */
226
        $field = $this->factory(CustomField::class);
227
        return $field->setWorkspace($this);
228
    }
229
230
    /**
231
     * Returns a new portfolio for the workspace.
232
     *
233
     * @return Portfolio
234
     */
235
    public function newPortfolio () {
236
        /** @var Portfolio $portfolio */
237
        $portfolio = $this->factory(Portfolio::class);
238
        return $portfolio->setWorkspace($this);
239
    }
240
241
    /**
242
     * Returns a new project for the workspace.
243
     *
244
     * @return Project
245
     */
246
    public function newProject () {
247
        /** @var Project $project */
248
        $project = $this->factory(Project::class);
249
        return $project->setWorkspace($this);
250
    }
251
252
    /**
253
     * Returns a new tag for the workspace.
254
     *
255
     * @return Tag
256
     */
257
    public function newTag () {
258
        /** @var Tag $tag */
259
        $tag = $this->factory(Tag::class);
260
        return $tag->setWorkspace($this);
261
    }
262
263
    /**
264
     * Returns a new task for the workspace.
265
     *
266
     * @return Task
267
     */
268
    public function newTask () {
269
        /** @var Task $task */
270
        $task = $this->factory(Task::class);
271
        return $task->setWorkspace($this);
272
    }
273
274
    /**
275
     * @param callable $filter `fn( CustomField $field ): bool`
276
     * @return CustomField[]
277
     */
278
    public function selectCustomFields (callable $filter) {
279
        return $this->_select($this->getCustomFields(), $filter);
280
    }
281
282
    /**
283
     * @param callable $filter `fn( Portfolio $portfolio ): bool`
284
     * @return Portfolio[]
285
     */
286
    public function selectPortfolios (callable $filter) {
287
        return $this->_select($this->getPortfolios(), $filter);
288
    }
289
290
    /**
291
     * @param callable $filter `fn( Project $project ): bool`
292
     * @return Project[]
293
     */
294
    public function selectProjects (callable $filter) {
295
        return $this->_select($this->getProjects([]), $filter);
296
    }
297
298
    /**
299
     * @param callable $filter `fn( Tag $tag ): bool`
300
     * @return Tag[]
301
     */
302
    public function selectTags (callable $filter) {
303
        return $this->_select($this->getTags(), $filter);
304
    }
305
306
    /**
307
     * @param callable $filter `fn( Team $team ): bool`
308
     * @return Team[]
309
     */
310
    public function selectTeams (callable $filter) {
311
        return $this->_select($this->getTeams(), $filter);
312
    }
313
314
    /**
315
     * @param callable $filter
316
     * @return User[]
317
     */
318
    public function selectUsers (callable $filter) {
319
        return $this->_select($this->getUsers(), $filter);
320
    }
321
}