Passed
Branch main (b6a268)
by Iain
04:11
created

MessageBuilder::addTextSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Notification\Slack;
16
17
use Parthenon\Notification\Exception\Slack\UnclosedSectionException;
18
use Parthenon\Notification\Exception\Slack\UnopenedSectionException;
19
20
final class MessageBuilder
21
{
22
    private $sections = [];
23
24
    private $currentSection = null;
25
26
    public function addMarkdownSection(string $text): self
27
    {
28
        if (null !== $this->currentSection) {
29
            throw new UnclosedSectionException();
30
        }
31
32
        $this->currentSection = [
33
            'type' => 'section',
34
            'text' => [
35
                'type' => 'mrkdwn',
36
                'text' => $text,
37
            ],
38
        ];
39
40
        return $this;
41
    }
42
43
    public function addTextSection(string $text, bool $emoji = true): self
44
    {
45
        if (null !== $this->currentSection) {
46
            throw new UnclosedSectionException();
47
        }
48
49
        $this->currentSection = [
50
            'type' => 'section',
51
            'text' => [
52
                'type' => 'plain_text',
53
                'text' => $text,
54
                'emoji' => $emoji,
55
            ],
56
        ];
57
58
        return $this;
59
    }
60
61
    public function addImage(string $url, string $altText): self
62
    {
63
        if (null === $this->currentSection) {
64
            throw new UnopenedSectionException();
65
        }
66
67
        $this->currentSection['accessory'] = [
68
            'type' => 'image',
69
            'image_url' => $url,
70
            'alt_text' => $altText,
71
        ];
72
73
        return $this;
74
    }
75
76
    public function addButton(string $text, string $url, string $value = 'click', string $actionId = 'button-action', bool $emoji = true): self
77
    {
78
        if (null === $this->currentSection) {
79
            throw new UnopenedSectionException();
80
        }
81
        $this->currentSection['accessory'] = [
82
            'type' => 'button',
83
            'text' => [
84
                'type' => 'plain_text',
85
                'text' => $text,
86
                'emoji' => $emoji,
87
            ],
88
            'value' => $value,
89
            'url' => $url,
90
            'action_id' => $actionId,
91
        ];
92
93
        return $this;
94
    }
95
96
    public function closeSection(): self
97
    {
98
        if (null === $this->currentSection) {
99
            throw new UnopenedSectionException();
100
        }
101
        $this->sections[] = $this->currentSection;
102
        $this->currentSection = null;
103
104
        return $this;
105
    }
106
107
    public function addDivider(): self
108
    {
109
        if (null !== $this->currentSection) {
110
            throw new UnclosedSectionException();
111
        }
112
        $this->sections[] = ['type' => 'divider'];
113
114
        return $this;
115
    }
116
117
    public function build(): array
118
    {
119
        if (null !== $this->currentSection) {
120
            throw new UnclosedSectionException();
121
        }
122
123
        return ['blocks' => $this->sections];
124
    }
125
}
126