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

Portfolio::_getDir()   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 0
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\CrudTrait;
7
use Helix\Asana\Base\AbstractEntity\PostMutatorTrait;
8
use Helix\Asana\Base\AbstractEntity\WorkspaceTrait;
9
use Helix\Asana\CustomField\FieldSetting;
10
use Helix\Asana\CustomField\FieldSettingsTrait;
11
use IteratorAggregate;
12
use Traversable;
13
14
/**
15
 * A portfolio.
16
 *
17
 * This is only available to business/enterprise accounts.
18
 *
19
 * So...
20
 *
21
 * I haven't tested this.
22
 *
23
 * If you have a biz account you want to let me play on, please contact me.
24
 *
25
 * @see https://developers.asana.com/docs/asana-portfolios
26
 * @see https://developers.asana.com/docs/portfolio
27
 *
28
 * @method string   getColor        () @depends after-create
29
 * @method string   getCreatedAt    () RFC3339x
30
 * @method User     getCreatedBy    ()
31
 * @method User[]   getMembers      ()
32
 * @method string   getName         ()
33
 * @method User     getOwner        ()
34
 *
35
 * @method bool     hasMembers      ()
36
 *
37
 * @method $this    setColor        (string $color)
38
 * @method $this    setMembers      (User[] $members)
39
 * @method $this    setName         (string $name)
40
 * @method $this    setOwner        (User $owner)
41
 *
42
 * @method User[]   selectMembers   (callable $filter) `fn( User $user ): bool`
43
 */
44
class Portfolio extends AbstractEntity implements IteratorAggregate {
45
46
    use CrudTrait;
47
    use FieldSettingsTrait;
48
    use PostMutatorTrait;
49
    use WorkspaceTrait;
50
51
    const DIR = 'portfolios';
52
    const TYPE = 'portfolio';
53
54
    protected const MAP = [
55
        'created_by' => User::class,
56
        'custom_field_settings' => [FieldSetting::class],
57
        'owner' => User::class,
58
        'members' => [User::class],
59
        'workspace' => Workspace::class
60
    ];
61
62
    /**
63
     * @depends after-create
64
     * @param Project $item
65
     * @return $this
66
     */
67
    public function addItem (Project $item) {
68
        $this->api->post("{$this}/addItem", ['item' => $item->getGid()]);
69
        return $this;
70
    }
71
72
    /**
73
     * @param User $user
74
     * @return $this
75
     */
76
    public function addMember (User $user) {
77
        return $this->addMembers([$user]);
78
    }
79
80
    /**
81
     * @param User[] $users
82
     * @return $this
83
     */
84
    public function addMembers (array $users) {
85
        return $this->_addWithPost("{$this}/addMembers", [
86
            'members' => array_column($users, 'gid')
87
        ], 'members', $users);
88
    }
89
90
    /**
91
     * @depends after-create
92
     * @return Project[]
93
     */
94
    public function getItems () {
95
        return iterator_to_array($this);
96
    }
97
98
    /**
99
     * @return Traversable|Project[]
100
     */
101
    public function getIterator () {
102
        return $this->api->loadEach($this, Project::class, "{$this}/items");
103
    }
104
105
    /**
106
     * @depends after-create
107
     * @param Project $item
108
     * @return $this
109
     */
110
    public function removeItem (Project $item) {
111
        $this->api->post("{$this}/removeItem", ['item' => $item->getGid()]);
112
        return $this;
113
    }
114
115
    /**
116
     * @param User $user
117
     * @return $this
118
     */
119
    public function removeMember (User $user) {
120
        return $this->removeMembers([$user]);
121
    }
122
123
    /**
124
     * @param User[] $users
125
     * @return $this
126
     */
127
    public function removeMembers (array $users) {
128
        return $this->_removeWithPost("{$this}/removeMembers", [
129
            'members' => array_column($users, 'gid')
130
        ], 'members', $users);
131
    }
132
133
    /**
134
     * @param callable $filter `fn( Project $project ): bool`
135
     * @return Project[]
136
     */
137
    public function selectItems (callable $filter) {
138
        return $this->_select($this, $filter);
139
    }
140
}