Passed
Push — master ( 0658f3...f968a7 )
by y
02:18
created

User::__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\ImmutableInterface;
7
use Helix\Asana\User\Photo;
8
use Helix\Asana\User\TaskList;
9
10
/**
11
 * A user.
12
 *
13
 * @see https://developers.asana.com/docs/asana-users
14
 * @see https://developers.asana.com/docs/user
15
 *
16
 * @immutable Users cannot be altered via the API.
17
 *
18
 * @method string       getEmail        ()
19
 * @method string       getName         ()
20
 * @method null|Photo   getPhoto        ()
21
 * @method Workspace[]  getWorkspaces   ()
22
 */
23
class User extends AbstractEntity implements ImmutableInterface {
24
25
    const TYPE = 'user';
26
27
    protected const MAP = [
28
        'photo' => Photo::class,
29
        'workspaces' => [Workspace::class]
30
    ];
31
32
    final public function __toString (): string {
33
        return "users/{$this->getGid()}";
34
    }
35
36
    /**
37
     * @param Workspace $workspace
38
     * @return $this
39
     */
40
    public function addToWorkspace (Workspace $workspace) {
41
        return $this->_addWithPost("{$workspace}/addUser", [
42
            'user' => $this->getGid()
43
        ], 'workspaces', [$workspace]);
44
    }
45
46
    /**
47
     * Returns the first known workspace for the user.
48
     * You should only rely on this if the authorized user is in exactly one workspace.
49
     *
50
     * @return Workspace
51
     */
52
    public function getDefaultWorkspace () {
53
        return $this->getWorkspaces()[0];
54
    }
55
56
    /**
57
     * @param null|Workspace $workspace Falls back to the default workspace.
58
     * @return Portfolio[]
59
     */
60
    public function getFavoritePortfolios (Workspace $workspace = null) {
61
        return $this->getFavorites(Portfolio::class, Portfolio::TYPE, $workspace);
62
    }
63
64
    /**
65
     * @param null|Workspace $workspace Falls back to the default workspace.
66
     * @return Project[]
67
     */
68
    public function getFavoriteProjects (Workspace $workspace = null) {
69
        return $this->getFavorites(Project::class, Project::TYPE, $workspace);
70
    }
71
72
    /**
73
     * @param null|Workspace $workspace Falls back to the default workspace.
74
     * @return Tag[]
75
     */
76
    public function getFavoriteTags (Workspace $workspace = null) {
77
        return $this->getFavorites(Tag::class, Tag::TYPE, $workspace);
78
    }
79
80
    /**
81
     * @param null|Workspace $workspace Falls back to the default workspace.
82
     * @return Team[]
83
     */
84
    public function getFavoriteTeams (Workspace $workspace = null) {
85
        return $this->getFavorites(Team::class, Team::TYPE, $workspace);
86
    }
87
88
    /**
89
     * @param null|Workspace $workspace Falls back to the default workspace.
90
     * @return User[]
91
     */
92
    public function getFavoriteUsers (Workspace $workspace = null) {
93
        return $this->getFavorites(self::class, self::TYPE, $workspace);
94
    }
95
96
    /**
97
     * @param string $class
98
     * @param string $resourceType
99
     * @param null|Workspace $workspace Falls back to the default workspace.
100
     * @return array
101
     */
102
    protected function getFavorites (string $class, string $resourceType, Workspace $workspace = null) {
103
        return $this->api->loadAll($this, $class, "{$this}/favorites", [
104
            'resource_type' => $resourceType,
105
            'workspace' => ($workspace ?? $this->api->getDefaultWorkspace())->getGid()
106
        ]);
107
    }
108
109
    /**
110
     * @return string[]
111
     */
112
    public function getPoolKeys () {
113
        $keys = parent::getPoolKeys();
114
115
        // include email as a key if it's loaded
116
        if (isset($this->data['email'])) {
117
            $keys[] = "users/{$this->data['email']}";
118
        }
119
120
        return $keys;
121
    }
122
123
    /**
124
     * @param null|Workspace $workspace
125
     * @return Portfolio[]
126
     */
127
    public function getPortfolios (Workspace $workspace = null) {
128
        return $this->api->loadAll($this, Portfolio::class, "portfolios", [
129
            'workspace' => ($workspace ?? $this->api->getDefaultWorkspace())->getGid(),
130
            'owner' => $this->getGid()
131
        ]);
132
    }
133
134
    /**
135
     * @param null|Workspace $workspace Falls back to the default workspace.
136
     * @return TaskList
137
     */
138
    public function getTaskList (Workspace $workspace = null) {
139
        return $this->api->load($this, TaskList::class, "{$this}/user_task_list", [
140
            'workspace' => ($workspace ?? $this->api->getDefaultWorkspace())->getGid()
141
        ]);
142
    }
143
144
    /**
145
     * Returns all tasks in the given workspace that are assigned to the user.
146
     *
147
     * @param string[] $filter `workspace` falls back to the default.
148
     * @return Task[]
149
     */
150
    public function getTasks (array $filter = []) {
151
        $filter['assignee'] = $this->getGid();
152
        $filter += ['workspace' => $this->api->getDefaultWorkspace()->getGid()];
153
        return $this->api->loadAll($this, Task::class, 'tasks', $filter);
154
    }
155
156
    /**
157
     * The user's teams.
158
     *
159
     * @see  https://developers.asana.com/docs/get-teams-for-a-user
160
     *
161
     * @param null|Workspace $organization Falls back to the default workspace.
162
     * @return Team[]
163
     */
164
    public function getTeams (Workspace $organization = null) {
165
        return $this->api->loadAll($this, Team::class, "{$this}/teams", [
166
            'organization' => ($organization ?? $this->getDefaultWorkspace())->getGid()
167
        ]);
168
    }
169
170
    public function getUrl (): string {
171
        return "https://app.asana.com/0/{$this->getGid()}/list";
172
    }
173
174
    /**
175
     * @param Workspace $workspace
176
     * @return $this
177
     */
178
    public function removeFromWorkspace (Workspace $workspace) {
179
        return $this->_removeWithPost("{$workspace}/removeUser", [
180
            'user' => $this->getGid()
181
        ], 'workspaces', [$workspace]);
182
    }
183
184
}