BlockElement   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 96
ccs 18
cts 18
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 23 5
A getType() 0 3 1
A isValidFor() 0 6 1
1
<?php
2
namespace Maknz\Slack;
3
4
use InvalidArgumentException;
5
6
abstract class BlockElement extends Payload
7
{
8
    /**
9
     * Element type.
10
     *
11
     * @var string
12
     */
13
    protected $type;
14
15
    /**
16
     * List of blocks each element is valid for.
17
     *
18
     * @var array
19
     */
20
    protected static $validFor = [
21
        'button'                     => ['Button',                   ['section', 'actions']],
22
        'checkboxes'                 => ['Checkboxes',               ['section', 'actions', 'input']],
23
        'datepicker'                 => ['DatePicker',               ['section', 'actions', 'input']],
24
        'timepicker'                 => ['Timepicker',               ['section', 'actions', 'input']],
25
        'image'                      => ['Image',                    ['section', 'context']],
26
        'multi_static_select'        => ['MultiStaticSelect',        ['section', 'input']],
27
        'multi_external_select'      => ['MultiExternalSelect',      ['section', 'input']],
28
        'multi_users_select'         => ['MultiUsersSelect',         ['section', 'input']],
29
        'multi_conversations_select' => ['MultiConversationsSelect', ['section', 'input']],
30
        'multi_channels_select'      => ['MultiChannelsSelect',      ['section', 'input']],
31
        'overflow'                   => ['Overflow',                 ['section', 'actions']],
32
        'plain_text_input'           => ['TextInput',                ['input']],
33
        'radio_buttons'              => ['RadioButtons',             ['section', 'actions', 'input']],
34
        'static_select'              => ['StaticSelect',             ['section', 'actions', 'input']],
35
        'external_select'            => ['ExternalSelect',           ['section', 'actions', 'input']],
36
        'users_select'               => ['UsersSelect',              ['section', 'actions', 'input']],
37
        'conversations_select'       => ['ConversationsSelect',      ['section', 'actions', 'input']],
38
        'channels_select'            => ['ChannelsSelect',           ['section', 'actions', 'input']],
39
40
        // Context Block allows a Text object to be used directly, so need to map types here
41
        'plain_text' => ['Text', ['context']],
42
        'mrkdwn'     => ['Text', ['context']],
43
    ];
44
45
    /**
46
     * Create a Block element from a keyed array of attributes.
47
     *
48
     * @param mixed $attributes
49
     *
50
     * @return BlockElement
51
     *
52
     * @throws \InvalidArgumentException
53
     */
54 37
    public static function factory($attributes)
55
    {
56 37
        if ($attributes instanceof static) {
57 1
            return $attributes;
58
        }
59
60 37
        if ( ! is_array($attributes)) {
61 1
            throw new InvalidArgumentException('The attributes must be a '.static::class.' or keyed array');
62
        }
63
64 36
        if ( ! isset($attributes['type'])) {
65 1
            throw new InvalidArgumentException('Cannot create BlockElement without a type attribute');
66
        }
67
68 35
        $validElements = array_keys(static::$validFor);
69
70 35
        if ( ! in_array($attributes['type'], $validElements)) {
71 1
            throw new InvalidArgumentException('Invalid Block type "'.$attributes['type'].'". Must be one of: '.implode(', ', $validElements).'.');
72
        }
73
74 34
        $className = __NAMESPACE__.'\\BlockElement\\'.static::$validFor[$attributes['type']][0];
75
76 34
        return new $className($attributes);
77
    }
78
79
    /**
80
     * Check if an element is valid for a Block.
81
     *
82
     * @param Block $block
83
     *
84
     * @return bool
85
     */
86 15
    public function isValidFor(Block $block)
87
    {
88 15
        $blockType = $block->getType();
89 15
        $validBlocks = static::$validFor[$this->getType()][1];
90
91 15
        return in_array($blockType, $validBlocks);
92
    }
93
94
    /**
95
     * Get the block type.
96
     *
97
     * @return string
98
     */
99 47
    public function getType()
100
    {
101 47
        return $this->type;
102
    }
103
}
104