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

EnumOption::moveFirst()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Helix\Asana\CustomField;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CreateTrait;
7
use Helix\Asana\Base\AbstractEntity\UpdateTrait;
8
use Helix\Asana\CustomField;
9
10
/**
11
 * A custom field enum option.
12
 *
13
 * @see https://developers.asana.com/docs/enum-option
14
 *
15
 * @method string   getColor    () @depends after-create
16
 * @method bool     isEnabled   ()
17
 * @method string   getName     ()
18
 *
19
 * @method $this    setColor    (string $color)
20
 * @method $this    setEnabled  (bool $enabled)
21
 * @method $this    setName     (string $name)
22
 */
23
class EnumOption extends AbstractEntity {
24
25
    use CreateTrait;
26
    use UpdateTrait;
27
28
    const TYPE = 'enum_option';
29
30
    /**
31
     * @var CustomField
32
     */
33
    protected $customField;
34
35
    /**
36
     * @param CustomField $customField
37
     * @param array $data
38
     */
39
    public function __construct (CustomField $customField, array $data = []) {
40
        $this->customField = $customField;
41
        parent::__construct($customField, $data);
42
    }
43
44
    /**
45
     * `enum_options/{gid}`
46
     *
47
     * @return string
48
     */
49
    final public function __toString (): string {
50
        return "enum_options/{$this->getGid()}";
51
    }
52
53
    /**
54
     * `custom_fields/{gid}/enum_options`
55
     *
56
     * @return string
57
     */
58
    final protected function _getDir (): string {
59
        return "{$this->customField}/enum_options";
60
    }
61
62
    /**
63
     * @return CustomField
64
     */
65
    public function getCustomField () {
66
        return $this->customField;
67
    }
68
69
    /**
70
     * Move above another option.
71
     *
72
     * @see https://developers.asana.com/docs/reorder-a-custom-fields-enum
73
     *
74
     * @depends after-create
75
     * @param EnumOption $option
76
     * @return $this
77
     */
78
    public function moveAbove (EnumOption $option) {
79
        $this->api->post("{$this->_getDir()}/insert", [
80
            'before_enum_option' => $option->getGid(),
81
            'enum_option' => $this->getGid()
82
        ]);
83
        $this->customField->_reload('enum_options'); // safe. the options are pooled.
84
        return $this;
85
    }
86
87
    /**
88
     * Move below another option.
89
     *
90
     * @see https://developers.asana.com/docs/reorder-a-custom-fields-enum
91
     *
92
     * @depends after-create
93
     * @param EnumOption $option
94
     * @return $this
95
     */
96
    public function moveBelow (EnumOption $option) {
97
        $this->api->post("{$this->_getDir()}/insert", [
98
            'after_enum_option' => $option->getGid(),
99
            'enum_option' => $this->getGid()
100
        ]);
101
        $this->customField->_reload('enum_options'); // safe. the options are pooled.
102
        return $this;
103
    }
104
105
    /**
106
     * Make the option first.
107
     *
108
     * @depends after-create
109
     * @return $this
110
     */
111
    public function moveFirst () {
112
        $first = $this->customField->getEnumOptions()[0];
113
        if ($first !== $this) {
114
            $this->moveAbove($first);
115
        }
116
        return $this;
117
    }
118
119
    /**
120
     * Make the option last.
121
     *
122
     * @depends after-create
123
     * @return $this
124
     */
125
    public function moveLast () {
126
        $options = $this->customField->getEnumOptions();
127
        $last = $options[count($options) - 1];
128
        if ($last !== $this) {
129
            $this->moveBelow($last);
130
        }
131
        return $this;
132
    }
133
}