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

CustomField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 123
rs 10
c 3
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A sortEnumOptions() 0 18 5
A addEnumOption() 0 11 2
A isText() 0 2 1
A isGlobalToWorkspace() 0 2 1
A _setData() 0 11 1
A hasNotificationsEnabled() 0 2 1
A setNotificationsEnabled() 0 2 1
A isEnum() 0 2 1
A setGlobalToWorkspace() 0 2 1
A isNumber() 0 2 1
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\EnumOption;
10
use Helix\Asana\Task\FieldEntries;
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 DIR = 'custom_fields';
38
    const TYPE = 'custom_field';
39
    const TYPE_ENUM = 'enum';
40
    const TYPE_NUMBER = 'number';
41
    const TYPE_TEXT = 'text';
42
43
    protected const MAP = [
44
        'enum_options' => [EnumOption::class],
45
    ];
46
47
    protected function _setData (array $data): void {
48
        // strip out field entry values if present.
49
        $data = array_intersect_key($data, array_flip([
50
            'gid',
51
            'description',
52
            'enum_options',
53
            'name',
54
            'precision',
55
            'resource_subtype'
56
        ]));
57
        parent::_setData($data);
58
    }
59
60
    /**
61
     * `POST` or stage a new enum option.
62
     *
63
     * @see https://developers.asana.com/docs/create-an-enum-option
64
     *
65
     * @param string $name
66
     * @return EnumOption
67
     */
68
    public function addEnumOption (string $name) {
69
        /** @var EnumOption $option */
70
        $option = $this->api->factory($this, EnumOption::class);
71
        $option->setName($name);
72
        if ($this->hasGid()) {
73
            $option->create();
74
        }
75
        $this->_addWithPost("{$this}/enum_options", [
76
            'name' => $name
77
        ], 'enum_options', [$option]);
78
        return $option;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    final public function hasNotificationsEnabled (): bool {
85
        return $this->_is('has_notifications_enabled');
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    final public function isEnum (): bool {
92
        return $this->getResourceSubtype() === self::TYPE_ENUM;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    final public function isGlobalToWorkspace (): bool {
99
        return $this->_is('is_global_to_workspace');
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    final public function isNumber (): bool {
106
        return $this->getResourceSubtype() === self::TYPE_NUMBER;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    final public function isText (): bool {
113
        return $this->getResourceSubtype() === self::TYPE_TEXT;
114
    }
115
116
    /**
117
     * @param bool $flag
118
     * @return $this
119
     */
120
    final public function setGlobalToWorkspace (bool $flag) {
121
        return $this->_set('is_global_to_workspace', $flag);
122
    }
123
124
    /**
125
     * @param bool $flag
126
     * @return $this
127
     */
128
    final public function setNotificationsEnabled (bool $flag) {
129
        return $this->_set('has_notifications_enabled', $flag);
130
    }
131
132
    /**
133
     * @param callable $cmp `fn( EnumOption $a, EnumOption $b ): int`
134
     * @return $this
135
     */
136
    public function sortEnumOptions (callable $cmp) {
137
        if ($options = $this->getEnumOptions()) {
138
            $prev = $options[0]; // first option on remote
139
            usort($options, $cmp);
140
            if ($this->hasGid()) {
141
                foreach ($options as $option) {
142
                    if ($option !== $prev) {
143
                        $this->api->put($option, [
144
                            'insert_after' => $prev->getGid()
145
                        ]);
146
                    }
147
                    $prev = $option;
148
                }
149
            }
150
            $this->data['enum_options'] = $options;
151
            // no diff    
152
        }
153
        return $this;
154
    }
155
156
}