Passed
Push — master ( 02e6b9...8f2f39 )
by y
01:57
created

CustomField::setNotificationsEnabled()   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
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
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\CustomField\EnumOption;
9
use Helix\Asana\Task\FieldEntries;
10
use Helix\Asana\Workspace\WorkspaceTrait;
11
12
/**
13
 * A custom field.
14
 *
15
 * See {@link FieldEntries} for getting {@link Task} values.
16
 *
17
 * @see https://developers.asana.com/docs/asana-custom-fields
18
 * @see https://developers.asana.com/docs/custom-field
19
 *
20
 * @method string       getDescription      ()
21
 * @method EnumOption[] getEnumOptions      ()
22
 * @method string       getName             ()
23
 * @method int          getPrecision        ()
24
 * @method string       getResourceSubtype  ()
25
 *
26
 * @method $this        setDescription      (string $text)
27
 * @method $this        setName             (string $name)
28
 * @method $this        setPrecision        (int $precision)
29
 * @method $this        setResourceSubtype  (string $type) @depends create-only
30
 */
31
class CustomField extends AbstractEntity {
32
33
    use CrudTrait;
34
    use PostMutatorTrait;
35
    use WorkspaceTrait;
36
37
    const TYPE = 'custom_field';
38
    const TYPE_ENUM = 'enum';
39
    const TYPE_NUMBER = 'number';
40
    const TYPE_TEXT = 'text';
41
42
    protected const MAP = [
43
        'enum_options' => [EnumOption::class],
44
    ];
45
46
    /**
47
     * `custom_fields/{gid}`
48
     *
49
     * @return string
50
     */
51
    final public function __toString (): string {
52
        return "custom_fields/{$this->getGid()}";
53
    }
54
55
    /**
56
     * `custom_fields`
57
     *
58
     * @return string
59
     */
60
    final protected function _getDir (): string {
61
        return 'custom_fields';
62
    }
63
64
    protected function _setData (array $data): void {
65
        // strip out field entry values if present.
66
        $data = array_intersect_key($data, array_flip([
67
            'gid',
68
            'description',
69
            'enum_options',
70
            'name',
71
            'precision',
72
            'resource_subtype'
73
        ]));
74
        parent::_setData($data);
75
    }
76
77
    /**
78
     * `POST` or stage a new enum option.
79
     *
80
     * @see https://developers.asana.com/docs/create-an-enum-option
81
     *
82
     * @param string $name
83
     * @return EnumOption
84
     */
85
    public function addEnumOption (string $name) {
86
        /** @var EnumOption $option */
87
        $option = $this->api->factory($this, EnumOption::class);
88
        $option->setName($name);
89
        if ($this->hasGid()) {
90
            $option->create();
91
        }
92
        $this->_addWithPost("{$this}/enum_options", [
93
            'name' => $name
94
        ], 'enum_options', [$option]);
95
        return $option;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    final public function hasNotificationsEnabled (): bool {
102
        return $this->_is('has_notifications_enabled');
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    final public function isEnum (): bool {
109
        return $this->getResourceSubtype() === self::TYPE_ENUM;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    final public function isGlobalToWorkspace (): bool {
116
        return $this->_is('is_global_to_workspace');
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    final public function isNumber (): bool {
123
        return $this->getResourceSubtype() === self::TYPE_NUMBER;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    final public function isText (): bool {
130
        return $this->getResourceSubtype() === self::TYPE_TEXT;
131
    }
132
133
    /**
134
     * @param bool $flag
135
     * @return $this
136
     */
137
    final public function setGlobalToWorkspace (bool $flag) {
138
        return $this->_set('is_global_to_workspace', $flag);
139
    }
140
141
    /**
142
     * @param bool $flag
143
     * @return $this
144
     */
145
    final public function setNotificationsEnabled (bool $flag) {
146
        return $this->_set('has_notifications_enabled', $flag);
147
    }
148
149
    /**
150
     * @param callable $cmp `fn( EnumOption $a, EnumOption $b ): int`
151
     * @return $this
152
     */
153
    public function sortEnumOptions (callable $cmp) {
154
        if ($options = $this->getEnumOptions()) {
155
            $prev = $options[0]; // first option on remote
156
            usort($options, $cmp);
157
            if ($this->hasGid()) {
158
                foreach ($options as $option) {
159
                    if ($option !== $prev) {
160
                        $this->api->put($option, [
161
                            'insert_after' => $prev->getGid()
162
                        ]);
163
                    }
164
                    $prev = $option;
165
                }
166
            }
167
            $this->data['enum_options'] = $options;
168
            // no diff    
169
        }
170
        return $this;
171
    }
172
173
}