Passed
Push — master ( b66c10...181037 )
by y
02:15
created

CustomField::sortEnumOptions()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.6111
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\EnumOption;
9
use Helix\Asana\Task\CustomValues;
10
11
/**
12
 * A custom field.
13
 *
14
 * Use {@see CustomValues} to map {@see Task} `custom_fields` as an array-object representation of values.
15
 *
16
 * @see https://developers.asana.com/docs/asana-custom-fields
17
 * @see https://developers.asana.com/docs/custom-field
18
 *
19
 * @method string       getDescription      ()
20
 * @method $this        setDescription      (string $text)
21
 * @method EnumOption[] getEnumOptions      ()
22
 * @method string       getName             ()
23
 * @method $this        setName             (string $name)
24
 * @method int          getPrecision        ()
25
 * @method $this        setPrecision        (int $precision)
26
 * @method string       getResourceSubtype  ()
27
 * @method $this        setResourceSubtype  (string $type) @depends create-only
28
 */
29
class CustomField extends AbstractEntity {
30
31
    use CrudTrait;
32
    use WorkspaceTrait;
33
34
    const TYPE = 'custom_field';
35
    const TYPE_ENUM = 'enum';
36
    const TYPE_NUMBER = 'number';
37
    const TYPE_TEXT = 'text';
38
39
    protected static $map = [
40
        'enum_options' => [EnumOption::class],
41
    ];
42
43
    final public function __toString (): string {
44
        return "custom_fields/{$this->getGid()}";
45
    }
46
47
    final protected function _getDir (): string {
48
        return 'custom_fields';
49
    }
50
51
    /**
52
     * `POST` or stage a new enum option.
53
     *
54
     * @see https://developers.asana.com/docs/create-an-enum-option
55
     *
56
     * @param string $name
57
     * @return EnumOption
58
     */
59
    public function addEnumOption (string $name) {
60
        /** @var EnumOption $option */
61
        $option = $this->factory(EnumOption::class);
62
        $option->setName($name);
1 ignored issue
show
Unused Code introduced by
The call to Helix\Asana\CustomField\EnumOption::setName() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $option->/** @scrutinizer ignore-call */ 
63
                 setName($name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        if ($this->hasGid()) {
64
            $option->create();
65
        }
66
        $this->_addWithPost("{$this}/enum_options", [
67
            'name' => $name
68
        ], 'enum_options', [$option]);
69
        return $option;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function hasNotificationsEnabled (): bool {
76
        return $this->_is('has_notifications_enabled');
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isEnum (): bool {
83
        return $this->getResourceSubtype() === self::TYPE_ENUM;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function isGlobalToWorkspace (): bool {
90
        return $this->_is('is_global_to_workspace');
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    final public function isNumber (): bool {
97
        return $this->getResourceSubtype() === self::TYPE_NUMBER;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    final public function isText (): bool {
104
        return $this->getResourceSubtype() === self::TYPE_TEXT;
105
    }
106
107
    /**
108
     * @param bool $flag
109
     * @return $this
110
     */
111
    public function setGlobalToWorkspace (bool $flag) {
112
        return $this->_set('is_global_to_workspace', $flag);
113
    }
114
115
    /**
116
     * @param bool $flag
117
     * @return $this
118
     */
119
    public function setNotificationsEnabled (bool $flag) {
120
        return $this->_set('has_notifications_enabled', $flag);
121
    }
122
123
    /**
124
     * @param callable $cmp `fn( EnumOption $a, EnumOption $b ): int`
125
     * @return $this
126
     */
127
    public function sortEnumOptions (callable $cmp) {
128
        if ($options = $this->getEnumOptions()) {
129
            $prev = $options[0]; // first option on remote
130
            usort($options, $cmp);
131
            if ($this->hasGid()) {
132
                foreach ($options as $option) {
133
                    if ($option !== $prev) {
134
                        $this->api->put($option, [
135
                            'insert_after' => $prev->getGid()
136
                        ]);
137
                    }
138
                    $prev = $option;
139
                }
140
            }
141
            $this->data['enum_options'] = $options;
142
            // no diff    
143
        }
144
        return $this;
145
    }
146
147
}