Workspace::export()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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