Code Duplication    Length = 65-71 lines in 2 locations

src/Slack/EmojiIcon.php 1 location

@@ 13-83 (lines=71) @@
10
 *
11
 * @since 0.1.0
12
 */
13
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
    public function __construct($emoji)
26
    {
27
        $this->setEmoji($emoji);
28
    }
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
    private function setEmoji($emoji)
40
    {
41
        // remove the whitespace
42
        $emoji = trim($emoji);
43
44
        $emojiValidationRegex = '_^:[\w-+]+:$_iuS';
45
        if (!preg_match($emojiValidationRegex, $emoji)) {
46
            throw new InvalidEmojiException(
47
                sprintf(
48
                    'The emoji: "%s" is not a valid emoji.
49
                     An emoji should always be a string starting and ending with ":".',
50
                    $emoji
51
                ),
52
                400
53
            );
54
        }
55
        $this->emoji = $emoji;
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getIcon()
64
    {
65
        return $this->emoji;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function __toString()
72
    {
73
        return $this->getIcon();
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function getType()
80
    {
81
        return 'emoji';
82
    }
83
}
84

src/Slack/UrlIcon.php 1 location

@@ 13-77 (lines=65) @@
10
 *
11
 * @since 0.1.0
12
 */
13
class UrlIcon extends SerializeToString implements IconInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $iconUrl;
19
20
    /**
21
     * UrlIcon constructor.
22
     *
23
     * @param string $iconUrl
24
     */
25
    public function __construct($iconUrl)
26
    {
27
        $this->setIcon($iconUrl);
28
    }
29
30
    /**
31
     * This will set the icon if the url is valid.
32
     *
33
     * @param string $iconUrl
34
     *
35
     * @throws InvalidUrlException Thrown when the url is not valid
36
     *
37
     * @return $this
38
     */
39
    private function setIcon($iconUrl)
40
    {
41
        // remove the whitespace
42
        $iconUrl = trim($iconUrl);
43
44
        // @see https://gist.github.com/dperini/729294 and https://mathiasbynens.be/demo/url-regex
45
        $urlValidationRegex = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS';
46
        if (!preg_match($urlValidationRegex, $iconUrl)) {
47
            throw new InvalidUrlException(sprintf('The url: "%s" is not a valid url.', $iconUrl), 400);
48
        }
49
        $this->iconUrl = $iconUrl;
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getIcon()
58
    {
59
        return $this->iconUrl;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function __toString()
66
    {
67
        return $this->getIcon();
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getType()
74
    {
75
        return 'url';
76
    }
77
}
78