Passed
Push — master ( 023346...878e6c )
by y
02:05
created

Workspace::findTasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
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/#tocS_Workspace
18
 *
19
 * @method string[] getEmailDomains ()
20
 * @method string   getName         ()
21
 * @method $this    setName         (string $name)
22
 */
23
class Workspace extends AbstractEntity {
24
25
    use UpdateTrait;
26
27
    const TYPE = 'workspace';
28
29
    final public function __toString (): string {
30
        return "workspaces/{$this->getGid()}";
31
    }
32
33
    /**
34
     * Initiates and returns an asynchronous export for the organization.
35
     *
36
     * @return OrganizationExport
37
     */
38
    public function export () {
39
        assert($this->isOrganization());
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
     * @see https://developers.asana.com/docs/#get-objects-via-typeahead
49
     *
50
     * @param string $class
51
     * @param string $resourceType
52
     * @param string $text
53
     * @param int $limit 1-100
54
     * @return array|AbstractEntity[]
55
     */
56
    protected function find (string $class, string $resourceType, string $text, int $limit = 20) {
57
        return $this->loadAll($class, "{$this}/typeahead", [
58
            'resource_type' => $resourceType,
59
            'query' => $text,
60
            'count' => $limit
61
        ]);
62
    }
63
64
    /**
65
     * Finds custom fields via the typeahead endpoint.
66
     *
67
     * @param string $text
68
     * @param int $limit 1-100
69
     * @return CustomField[]
70
     */
71
    public function findCustomFields (string $text, int $limit = 20) {
72
        return $this->find(CustomField::class, CustomField::TYPE, $text, $limit);
73
    }
74
75
    /**
76
     * Finds portfolios via the typeahead endpoint.
77
     *
78
     * @param string $text
79
     * @param int $limit 1-100
80
     * @return Portfolio[]
81
     */
82
    public function findPortfolios (string $text, int $limit = 20) {
83
        return $this->find(Portfolio::class, Portfolio::TYPE, $text, $limit);
84
    }
85
86
    /**
87
     * Finds projects via the typeahead endpoint.
88
     *
89
     * @param string $text
90
     * @param int $limit 1-100
91
     * @return Project[]
92
     */
93
    public function findProjects (string $text, int $limit = 20) {
94
        return $this->find(Project::class, Project::TYPE, $text, $limit);
95
    }
96
97
    /**
98
     * Finds tags via the typeahead endpoint.
99
     *
100
     * @param string $text
101
     * @param int $limit 1-100
102
     * @return Tag[]
103
     */
104
    public function findTags (string $text, int $limit = 20) {
105
        return $this->find(Tag::class, Tag::TYPE, $text, $limit);
106
    }
107
108
    /**
109
     * Finds tasks via the typeahead endpoint.
110
     *
111
     * @param string $text
112
     * @param int $limit 1-100
113
     * @return Task[]
114
     */
115
    public function findTasks (string $text, int $limit = 20) {
116
        return $this->find(Task::class, Task::TYPE, $text, $limit);
117
    }
118
119
    /**
120
     * Finds users via the typeahead endpoint.
121
     *
122
     * @param string $text
123
     * @param int $limit 1-100
124
     * @return User[]
125
     */
126
    public function findUsers (string $text, int $limit = 20) {
127
        return $this->find(User::class, User::TYPE, $text, $limit);
128
    }
129
130
    /**
131
     * @return Project[]
132
     */
133
    public function getProjects () {
134
        return $this->loadAll(Project::class, 'projects', ['workspace' => $this->getGid()]);
135
    }
136
137
    /**
138
     * @return Tag[]
139
     */
140
    public function getTags () {
141
        return $this->loadAll(Tag::class, 'tags', ['workspace' => $this->getGid()]);
142
    }
143
144
    /**
145
     * @return Team[]
146
     */
147
    public function getTeams () {
148
        return $this->loadAll(Team::class, "organizations/{$this->getGid()}/teams");
149
    }
150
151
    /**
152
     * @param string $email
153
     * @return null|User
154
     */
155
    public function getUserByEmail (string $email) {
156
        return $this->api->getCache()->get($email, $this, function() use ($email) {
157
            foreach ($this->getUsers() as $user) {
158
                if ($user->getEmail() === $email) {
159
                    return $user;
160
                }
161
            }
162
            return null;
163
        });
164
    }
165
166
    /**
167
     * @return User[]
168
     */
169
    public function getUsers () {
170
        return $this->loadAll(User::class, "{$this}/users");
171
    }
172
173
    /**
174
     * Returns all webhooks belonging to the workspace.
175
     *
176
     * @return ProjectWebhook[]|TaskWebhook[]
177
     */
178
    public function getWebhooks () {
179
        $all = $this->api->get('webhooks', ['workspace' => $this->getGid()], ['expand' => 'this']);
180
        return array_map(function(array $each) {
181
            return $this->api->getCache()->get($each['gid'], $this, function() use ($each) {
182
                static $classes = [
183
                    Project::TYPE => ProjectWebhook::class,
184
                    Task::TYPE => TaskWebhook::class
185
                ];
186
                return $this->factory($classes[$each['resource_type']], $each);
187
            });
188
        }, $all);
1 ignored issue
show
Bug introduced by
It seems like $all can also be of type null; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
        }, /** @scrutinizer ignore-type */ $all);
Loading history...
189
    }
190
191
    /**
192
     * @return bool
193
     */
194
    public function isOrganization (): bool {
195
        return $this->_is('is_organization');
196
    }
197
198
    /**
199
     * Returns a new custom field for the workspace.
200
     *
201
     * @return CustomField
202
     */
203
    public function newCustomField () {
204
        /** @var CustomField $field */
205
        $field = $this->factory(CustomField::class);
206
        return $field->setWorkspace($this);
207
    }
208
209
    /**
210
     * Returns a new portfolio for the workspace.
211
     *
212
     * @return Portfolio
213
     */
214
    public function newPortfolio () {
215
        /** @var Portfolio $portfolio */
216
        $portfolio = $this->factory(Portfolio::class);
217
        return $portfolio->setWorkspace($this);
218
    }
219
220
    /**
221
     * Returns a new project for the workspace.
222
     *
223
     * @return Project
224
     */
225
    public function newProject () {
226
        /** @var Project $project */
227
        $project = $this->factory(Project::class);
228
        return $project->setWorkspace($this);
229
    }
230
231
    /**
232
     * Returns a new tag for the workspace.
233
     *
234
     * @return Tag
235
     */
236
    public function newTag () {
237
        /** @var Tag $tag */
238
        $tag = $this->factory(Tag::class);
239
        return $tag->setWorkspace($this);
240
    }
241
242
    /**
243
     * Returns a new task for the workspace.
244
     *
245
     * @return Task
246
     */
247
    public function newTask () {
248
        /** @var Task $task */
249
        $task = $this->factory(Task::class);
250
        return $task->setWorkspace($this);
251
    }
252
}