Completed
Push — master ( 7db396...ea8905 )
by Alexey
02:58
created

SlackMessageTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 0
loc 120
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A inlineMultilines() 0 4 1
A formatBold() 0 4 1
A formatItalic() 0 4 1
A formatStrikeThought() 0 4 1
A formatCode() 0 8 1
A newLine() 0 4 1
A formatLink() 0 4 1
A escapeCharacters() 0 8 1
A formatListMarker() 0 12 3
A formatListNumeric() 0 14 3
1
<?php
2
/**
3
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3
4
 * https://github.com/wow-apps/symfony-slack-bot
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\SlackBundle\Traits;
13
14
/**
15
 * Trait SlackMessageTrait
16
 *
17
 * @author Alexey Samara <[email protected]>
18
 * @package WowApps\SlackBundle
19
 */
20
trait SlackMessageTrait
21
{
22
    /**
23
     * @param array $lines
24
     * @return string
25
     */
26 2
    public function inlineMultilines(array $lines): string
27
    {
28 2
        return implode("\n", $lines);
29
    }
30
31
    /**
32
     * @param string $string
33
     * @return string
34
     */
35 1
    public function formatBold(string $string): string
36
    {
37 1
        return sprintf("*%s*", $string);
38
    }
39
40
    /**
41
     * @param string $string
42
     * @return string
43
     */
44 1
    public function formatItalic(string $string): string
45
    {
46 1
        return sprintf("_%s_", $string);
47
    }
48
49
    /**
50
     * @param string $string
51
     * @return string
52
     */
53 1
    public function formatStrikeThought(string $string): string
54
    {
55 1
        return sprintf("~%s~", $string);
56
    }
57
58
    /**
59
     * @param array $list
60
     * @return string
61
     * @throws \InvalidArgumentException
62
     */
63 1
    public function formatListMarker(array $list): string
64
    {
65 1
        if (empty($list)) {
66 1
            throw new \InvalidArgumentException('The list must contain at least one value');
67
        }
68
69 1
        foreach ($list as $key => $value) {
70 1
            $list[$key] = sprintf('• %s', $value);
71
        }
72
73 1
        return sprintf("\n%s\n", implode("\n", $list));
74
    }
75
76
    /**
77
     * @param array $list
78
     * @return string
79
     * @throws \InvalidArgumentException
80
     */
81 1
    public function formatListNumeric(array $list): string
82
    {
83 1
        if (empty($list)) {
84 1
            throw new \InvalidArgumentException('The list must contain at least one value');
85
        }
86
87 1
        $num = 0;
88 1
        $output = "\n";
89 1
        foreach ($list as $value) {
90 1
            $output .= sprintf("%s. %s\n", ++$num, $value);
91
        }
92
93 1
        return $output;
94
    }
95
96
    /**
97
     * @param array $lines
98
     * @return string
99
     */
100 1
    public function formatCode(array $lines): string
101
    {
102 1
        $output = "\n```";
103 1
        $output .= $this->inlineMultilines($lines);
104 1
        $output .= "```\n";
105
106 1
        return $output;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 1
    public function newLine(): string
113
    {
114 1
        return "\n";
115
    }
116
117
    /**
118
     * @param string $title
119
     * @param string $url
120
     * @return string
121
     */
122 1
    public function formatLink(string $title, string $url): string
123
    {
124 1
        return sprintf("<%s|%s>", $url, $title);
125
    }
126
127
    /**
128
     * @param string $string
129
     * @return string
130
     */
131 10
    public function escapeCharacters(string $string): string
132
    {
133 10
        $string = str_replace('&', '&amp;', $string);
134 10
        $string = str_replace('<', '&lt;', $string);
135 10
        $string = str_replace('>', '&gt;', $string);
136
137 10
        return $string;
138
    }
139
}
140