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