EnumOption::moveAbove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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