Completed
Pull Request — master (#38)
by
unknown
03:25
created

Block::getBlockId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack;
3
4
use InvalidArgumentException;
5
6
abstract class Block extends Payload
7
{
8
    /**
9
     * Block type.
10
     *
11
     * @var string
12
     */
13
    protected $type;
14
15
    /**
16
     * Block identifier.
17
     *
18
     * @var string
19
     */
20
    protected $block_id;
21
22
    /**
23
     * Get the block type.
24
     *
25
     * @return string
26
     */
27 14
    public function getType()
28
    {
29 14
        return $this->type;
30
    }
31
32
    /**
33
     * Get the block identifier.
34
     *
35
     * @return string
36
     */
37 9
    public function getBlockId()
38
    {
39 9
        return $this->block_id;
40
    }
41
42
    /**
43
     * Set the block identifier.
44
     *
45
     * @param string $blockId
46
     *
47
     * @return Block
48
     */
49 3
    public function setBlockId($blockId)
50
    {
51 3
        $this->block_id = $blockId;
52
53 3
        return $this;
54
    }
55
56
    /**
57
     * Create a Block element from a keyed array of attributes.
58
     *
59
     * @param array $attributes
60
     *
61
     * @return Block
62
     *
63
     * @throws \InvalidArgumentException
64
     */
65 5
    public static function factory(array $attributes)
66
    {
67 5
        if (!isset($attributes['type'])) {
68 1
            throw new InvalidArgumentException('Cannot create Block without a type attribute');
69
        }
70
71
        $validBlocks = [
72 4
            'actions',
73
            'context',
74
            'divider',
75
            'file',
76
            'image',
77
            'input',
78
            'section',
79
        ];
80
81 4
        if (!in_array($attributes['type'], $validBlocks)) {
82 1
            throw new InvalidArgumentException('Block type must be one of: ' . implode(', ', $validBlocks) . '.');
83
        }
84
85 3
        $className = __NAMESPACE__ . '\\Block\\' . ucfirst($attributes['type']);
86 3
        return new $className($attributes);
87
    }
88
}
89