AbstractStaticSelect   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 139
ccs 35
cts 35
cp 1
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionGroups() 0 3 1
A setOptionGroups() 0 10 2
A setOptions() 0 5 1
A clearOptionGroups() 0 5 1
A getOptionGroupsAsArrays() 0 9 2
A clearAllOptions() 0 6 1
A addOptionGroup() 0 14 3
A addOption() 0 6 1
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\CompositionObject\OptionGroup;
6
use Maknz\Slack\PlaceholderTrait;
7
8
abstract class AbstractStaticSelect extends Options
9
{
10
    use PlaceholderTrait;
11
12
    /**
13
     * Select option groups.
14
     *
15
     * @var \Maknz\Slack\CompositionObject\OptionGroup[]
16
     */
17
    protected $option_groups = [];
18
19
    /**
20
     * Set options available within the block.
21
     *
22
     * @param array $options
23
     *
24
     * @return $this
25
     *
26
     * @throws \InvalidArgumentException
27
     */
28 10
    public function setOptions(array $options)
29
    {
30 10
        $this->clearOptionGroups();
31
32 10
        return parent::setOptions($options);
33
    }
34
35
    /**
36
     * Add an option to the block.
37
     *
38
     * @param mixed $option
39
     *
40
     * @return $this
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44 13
    public function addOption($option)
45
    {
46 13
        parent::addOption($option);
47 12
        $this->clearOptionGroups();
48
49 12
        return $this;
50
    }
51
52
    /**
53
     * Get the option groups.
54
     *
55
     * @return \Maknz\Slack\CompositionObject\OptionGroup[]
56
     */
57 17
    public function getOptionGroups()
58
    {
59 17
        return $this->option_groups;
60
    }
61
62
    /**
63
     * Get the option groups in array format.
64
     *
65
     * @return array
66
     */
67 5
    public function getOptionGroupsAsArrays()
68
    {
69 5
        $groups = [];
70
71 5
        foreach ($this->getOptionGroups() as $group) {
72 4
            $groups[] = $group->toArray();
73
        }
74
75 5
        return $groups;
76
    }
77
78
    /**
79
     * Set the option groups.
80
     *
81
     * @param array $groups
82
     *
83
     * @return $this
84
     *
85
     * @throws \InvalidArgumentException
86
     */
87 7
    public function setOptionGroups(array $groups)
88
    {
89 7
        $this->clearOptions();
90 7
        $this->clearOptionGroups();
91
92 7
        foreach ($groups as $group) {
93 7
            $this->addOptionGroup($group);
94
        }
95
96 6
        return $this;
97
    }
98
99
    /**
100
     * Clear option groups in the block.
101
     *
102
     * @return $this
103
     */
104 19
    public function clearOptionGroups()
105
    {
106 19
        $this->option_groups = [];
107
108 19
        return $this;
109
    }
110
111
    /**
112
     * Clear options and option groups.
113
     *
114
     * @return $this
115
     */
116 1
    public function clearAllOptions()
117
    {
118 1
        $this->clearOptions();
119 1
        $this->clearOptionGroups();
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * Add an option group to the block.
126
     *
127
     * @param mixed $group
128
     *
129
     * @return $this
130
     *
131
     * @throws \InvalidArgumentException
132
     */
133 10
    public function addOptionGroup($group)
134
    {
135 10
        if (is_array($group)) {
136 3
            $group = new OptionGroup($group);
137
        }
138
139 10
        if ($group instanceof OptionGroup) {
140 9
            $this->clearOptions();
141 9
            $this->option_groups[] = $group;
142
143 9
            return $this;
144
        }
145
146 1
        throw new InvalidArgumentException('The option group must be an instance of '.OptionGroup::class.' or a keyed array');
147
    }
148
}
149