Passed
Push — master ( 02e6b9...8f2f39 )
by y
01:57
created

Workspace::selectPortfolios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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