Passed
Push — master ( fbd180...23ab98 )
by y
02:18
created

Portfolio::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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