Passed
Push — master ( 2428ce...06a092 )
by y
02:04
created

Workspace::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 2
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
10
/**
11
 * A workspace.
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 $fn, array $apiX = Project::GET_ACTIVE) `fn( Project $project ): bool`
25
 * @method Tag[]            selectTags          (callable $filter) `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
        $export->_set('organization', $this);
47
        return $export->create();
48
    }
49
50
    /**
51
     * Finds entities via the typeahead endpoint.
52
     *
53
     * This endpoint is fuzzy, so you should prefer using the selection methods for precision.
54
     *
55
     * The typeahead endpoint isn't very good for finding users, so that method doesn't exist in this class.
56
     *
57
     * @see https://developers.asana.com/docs/get-objects-via-typeahead
58
     *
59
     * @param string $class
60
     * @param string $text
61
     * @param int $limit 1-100
62
     * @return array|AbstractEntity[]
63
     */
64
    protected function find (string $class, string $text, int $limit = 20) {
65
        return $this->api->loadAll($this, $class, "{$this}/typeahead", [
66
            'resource_type' => $class::TYPE,
67
            'query' => $text,
68
            'count' => $limit
69
        ]);
70
    }
71
72
    /**
73
     * Finds custom fields via the typeahead endpoint.
74
     *
75
     * @param string $text
76
     * @param int $limit 1-100
77
     * @return CustomField[]
78
     */
79
    public function findCustomFields (string $text, int $limit = 20) {
80
        return $this->find(CustomField::class, $text, $limit);
81
    }
82
83
    /**
84
     * Finds portfolios via the typeahead endpoint.
85
     *
86
     * @param string $text
87
     * @param int $limit 1-100
88
     * @return Portfolio[]
89
     */
90
    public function findPortfolios (string $text, int $limit = 20) {
91
        return $this->find(Portfolio::class, $text, $limit);
92
    }
93
94
    /**
95
     * Finds projects via the typeahead endpoint.
96
     *
97
     * @param string $text
98
     * @param int $limit 1-100
99
     * @return Project[]
100
     */
101
    public function findProjects (string $text, int $limit = 20) {
102
        return $this->find(Project::class, $text, $limit);
103
    }
104
105
    /**
106
     * Finds tags via the typeahead endpoint.
107
     *
108
     * @param string $text
109
     * @param int $limit 1-100
110
     * @return Tag[]
111
     */
112
    public function findTags (string $text, int $limit = 20) {
113
        return $this->find(Tag::class, $text, $limit);
114
    }
115
116
    /**
117
     * Finds tasks via the typeahead endpoint.
118
     *
119
     * @param string $text
120
     * @param int $limit 1-100
121
     * @return Task[]
122
     */
123
    public function findTasks (string $text, int $limit = 20) {
124
        return $this->find(Task::class, $text, $limit);
125
    }
126
127
    /**
128
     * @return CustomField[]
129
     */
130
    public function getCustomFields () {
131
        return $this->api->loadAll($this, CustomField::class, "{$this}/custom_fields");
132
    }
133
134
    /**
135
     * The API user's portfolios in this workspace.
136
     *
137
     * @return Portfolio[]
138
     */
139
    public function getPortfolios () {
140
        return $this->api->loadAll($this, Portfolio::class, "portfolios", [
141
            'workspace' => $this->getGid(),
142
            'owner' => $this->api->getMe()->getGid() // the only allowed value, but still required.
143
        ]);
144
    }
145
146
    /**
147
     * The workspace's projects.
148
     *
149
     * @param array $filter
150
     * @return Project[]
151
     */
152
    public function getProjects (array $filter = Project::GET_ACTIVE) {
153
        $filter['workspace'] = $this->getGid();
154
        return $this->api->loadAll($this, Project::class, 'projects', $filter);
155
    }
156
157
    /**
158
     * @return Tag[]
159
     */
160
    public function getTags () {
161
        return $this->api->loadAll($this, Tag::class, 'tags', ['workspace' => $this->getGid()]);
162
    }
163
164
    /**
165
     * @return Team[]
166
     */
167
    public function getTeams () {
168
        return $this->api->loadAll($this, 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->getPool()->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->api->loadAll($this, User::class, "{$this}/users");
191
    }
192
193
    /**
194
     * @return ProjectWebhook[]|TaskWebhook[]
195
     */
196
    public function getWebhooks () {
197
        return array_map(function(array $each) {
198
            return $this->api->getPool()->get($each['gid'], $this, function() use ($each) {
199
                return $this->api->factory($this, [
200
                    Project::TYPE => ProjectWebhook::class,
201
                    Task::TYPE => TaskWebhook::class
202
                ][$each['resource_type']], $each);
203
            });
204
        }, $this->api->get('webhooks', [
205
            'workspace' => $this->getGid(),
206
            'opt_expand' => 'this'
207
        ]));
208
    }
209
210
    /**
211
     * @return bool
212
     */
213
    final public function isOrganization (): bool {
214
        return $this->_is('is_organization');
215
    }
216
217
    /**
218
     * Factory.
219
     *
220
     * @return CustomField
221
     */
222
    public function newCustomField () {
223
        /** @var CustomField $field */
224
        $field = $this->api->factory($this, CustomField::class);
225
        return $field->setWorkspace($this);
226
    }
227
228
    /**
229
     * Factory.
230
     *
231
     * @return Portfolio
232
     */
233
    public function newPortfolio () {
234
        /** @var Portfolio $portfolio */
235
        $portfolio = $this->api->factory($this, Portfolio::class);
236
        return $portfolio->setWorkspace($this);
237
    }
238
239
    /**
240
     * Factory.
241
     *
242
     * @return Project
243
     */
244
    public function newProject () {
245
        /** @var Project $project */
246
        $project = $this->api->factory($this, Project::class);
247
        return $project->setWorkspace($this);
248
    }
249
250
    /**
251
     * Factory.
252
     *
253
     * @return Tag
254
     */
255
    public function newTag () {
256
        /** @var Tag $tag */
257
        $tag = $this->api->factory($this, Tag::class);
258
        return $tag->setWorkspace($this);
259
    }
260
261
    /**
262
     * Factory.
263
     *
264
     * @return Task
265
     */
266
    public function newTask () {
267
        /** @var Task $task */
268
        $task = $this->api->factory($this, Task::class);
269
        return $task->setWorkspace($this);
270
    }
271
272
}