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

EnumOption::insertBefore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A EnumOption::moveFirst() 0 6 2
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            ()
16
 * @method $this    setColor            (string $color)
17
 * @method bool     isEnabled           ()
18
 * @method $this    setEnabled          (bool $enabled)
19
 * @method string   getName             ()
20
 * @method $this    setName             (string $name)
21
 */
22
class EnumOption extends AbstractEntity {
23
24
    use CreateTrait;
25
    use UpdateTrait;
26
27
    const TYPE = 'enum_option';
28
29
    /**
30
     * Remote doesn't have a `custom_field` property, so it's required here.
31
     *
32
     * @var CustomField
33
     */
34
    protected $customField;
35
36
    public function __construct (CustomField $customField, array $data = []) {
37
        parent::__construct($customField, $data);
38
        $this->customField = $customField;
39
    }
40
41
    final public function __toString (): string {
42
        return "enum_options/{$this->getGid()}";
43
    }
44
45
    final protected function _getDir (): string {
46
        return "{$this->customField}/enum_options";
47
    }
48
49
    /**
50
     * @return CustomField
51
     */
52
    public function getCustomField () {
53
        return $this->customField;
54
    }
55
56
    /**
57
     * @see https://developers.asana.com/docs/reorder-a-custom-fields-enum
58
     * @param EnumOption $option
59
     * @return $this
60
     */
61
    public function moveAfter (EnumOption $option) {
62
        $this->api->post("{$this->_getDir()}/insert", [
63
            'after_enum_option' => $option->getGid(),
64
            'enum_option' => $this->getGid()
65
        ]);
66
        $this->customField->reload('enum_options');
67
        return $this;
68
    }
69
70
    /**
71
     * @see https://developers.asana.com/docs/reorder-a-custom-fields-enum
72
     * @param EnumOption $option
73
     * @return $this
74
     */
75
    public function moveBefore (EnumOption $option) {
76
        $this->api->post("{$this->_getDir()}/insert", [
77
            'before_enum_option' => $option->getGid(),
78
            'enum_option' => $this->getGid()
79
        ]);
80
        $this->customField->reload('enum_options');
81
        return $this;
82
    }
83
84
    /**
85
     * @return $this
86
     */
87
    public function moveFirst () {
88
        $first = $this->customField->getEnumOptions()[0];
89
        if ($first !== $this) {
90
            $this->moveBefore($first);
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * @return $this
97
     */
98
    public function moveLast () {
99
        $options = $this->customField->getEnumOptions();
100
        $last = $options[count($options) - 1];
101
        if ($last !== $this) {
102
            $this->moveAfter($last);
103
        }
104
        return $this;
105
    }
106
}