Passed
Pull Request — master (#75)
by Chris
12:05
created

MultiChannelSelect   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 120
ccs 28
cts 28
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setInitialChannels() 0 9 2
A toArray() 0 23 4
A addInitialChannel() 0 10 2
A clearInitialChannels() 0 5 1
A getInitialChannels() 0 3 1
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Object\Option;
6
7
class MultiChannelSelect extends MultiDynamicSelect
8
{
9
    /**
10
     * Block type.
11
     *
12
     * @var string
13
     */
14
    protected $type = 'multi_channels_select';
15
16
    /**
17
     * Initially selected channels.
18
     *
19
     * @var string[]
20
     */
21
    protected $initial_channels;
22
23
    /**
24
     * Internal attribute to property map.
25
     *
26
     * @var array
27
     */
28
    protected static $availableAttributes = [
29
        'placeholder'        => 'placeholder',
30
        'action_id'          => 'action_id',
31
        'initial_channels'   => 'initial_channels',
32
        'confirm'            => 'confirm',
33
        'max_selected_items' => 'max_selected_items',
34
    ];
35
36
    /**
37
     * Get the initially selected channels.
38
     *
39
     * @return string[]
40
     */
41 4
    public function getInitialChannels()
42
    {
43 4
        return $this->initial_channels;
44
    }
45
46
    /**
47
     * Set the initially selected channels.
48
     *
49
     * @param string[] $initialChannels
50
     *
51
     * @return $this
52
     *
53
     * @throws InvalidArgumentException
54
     */
55 4
    public function setInitialChannels(array $initialChannels)
56
    {
57 4
        $this->clearInitialChannels();
58
59 4
        foreach ($initialChannels as $initialChannel) {
60 4
            $this->addInitialChannel($initialChannel);
61
        }
62
63 3
        return $this;
64
    }
65
66
    /**
67
     * Clear the initially selected channels.
68
     *
69
     * @return $this
70
     */
71 4
    public function clearInitialChannels()
72
    {
73 4
        $this->initial_channels = [];
74
75 4
        return $this;
76
    }
77
78
    /**
79
     * Add an initially selected channel.
80
     *
81
     * @param string $initialChannels
82
     *
83
     * @return $this
84
     *
85
     * @throws InvalidArgumentException
86
     */
87 4
    public function addInitialChannel($initialChannel)
88
    {
89 4
        if (is_string($initialChannel)) {
90 3
            $this->initial_channels[] = $initialChannel;
91
92 3
            return $this;
93
        }
94
95
96 1
        throw new InvalidArgumentException('The initial channel ID must be a string');
97
    }
98
99
    /**
100
     * Convert the block to its array representation.
101
     *
102
     * @return array
103
     */
104 1
    public function toArray()
105
    {
106 1
        $data = [
107 1
            'type'        => $this->getType(),
108 1
            'placeholder' => $this->getPlaceholder()->toArray(),
109 1
            'action_id'   => $this->getActionId(),
110
        ];
111
112 1
        $initialChannels = $this->getInitialChannels();
113
114 1
        if (count($initialChannels)) {
115 1
            $data['initial_channels'] = $initialChannels;
116
        }
117
118 1
        if ($this->getConfirm()) {
119 1
            $data['confirm'] = $this->getConfirm()->toArray();
120
        }
121
122 1
        if ($this->getMaxSelectedItems()) {
123 1
            $data['max_selected_items'] = $this->getMaxSelectedItems();
124
        }
125
126 1
        return $data;
127
    }
128
}
129