Completed
Push — master ( 96700d...96e717 )
by jelmer
02:14
created

EmojiIcon::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

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