EmojiIcon::getIcon()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack;
4
5
use Pageon\SlackWebhookMonolog\Slack\Exceptions\InvalidEmojiException;
6
use Pageon\SlackWebhookMonolog\Slack\Interfaces\IconInterface;
7
use Pageon\SlackWebhookMonolog\General\SerializeToString;
8
9
/**
10
 * @author Jelmer Prins <[email protected]>
11
 *
12
 * @since 0.1.0
13
 */
14
class EmojiIcon extends SerializeToString implements IconInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $emoji;
20
21
    /**
22
     * UrlIcon constructor.
23
     *
24
     * @param string $emoji
25
     */
26 8
    public function __construct($emoji)
27
    {
28 8
        $this->setEmoji($emoji);
29 7
    }
30
31
    /**
32
     * This will set the emoji if it is valid.
33
     *
34
     * @param string $emoji
35
     *
36
     * @throws InvalidEmojiException Thrown when the emoji is not valid
37
     *
38
     * @return $this
39
     */
40 8
    private function setEmoji($emoji)
41
    {
42
        // remove the whitespace
43 8
        $emoji = trim($emoji);
44
45 8
        $emojiValidationRegex = '_^:[\w-+]+:$_iuS';
46 8
        if (!preg_match($emojiValidationRegex, $emoji)) {
47 1
            throw new InvalidEmojiException(
48 1
                sprintf(
49
                    'The emoji: "%s" is not a valid emoji.
50 1
                     An emoji should always be a string starting and ending with ":".',
51
                    $emoji
52 1
                ),
53
                400
54 1
            );
55
        }
56 7
        $this->emoji = $emoji;
57
58 7
        return $this;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 5
    public function getIcon()
65
    {
66 5
        return $this->emoji;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function __toString()
73
    {
74 4
        return $this->getIcon();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function getType()
81
    {
82 1
        return 'emoji';
83
    }
84
}
85