Test Failed
Push — master ( bd1e8a...fed951 )
by Alexey
12:42 queued 12s
created

SlackMessageValidator::attachmentDto()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 72
1
<?php
2
3
/*
4
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony.
5
 * https://github.com/wow-apps/symfony-slack-bot
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE
10
 *
11
 * For technical documentation.
12
 * https://wow-apps.github.io/symfony-slack-bot/docs/
13
 *
14
 * Author Alexey Samara <[email protected]>
15
 *
16
 * Copyright 2016 WoW-Apps.
17
 */
18
19
namespace WowApps\SlackBundle\Service;
20
21
use WowApps\SlackBundle\DTO\Attachment;
22
use WowApps\SlackBundle\DTO\AttachmentAction;
23
use WowApps\SlackBundle\DTO\AttachmentActionConfirm;
24
use WowApps\SlackBundle\DTO\SlackMessage;
25
use WowApps\SlackBundle\Exception\SlackbotException;
26
use WowApps\SlackBundle\Tests\Service\SlackEmojiTest;
27
28 1
/**
29
 * Class SlackMessageValidator.
30 1
 *
31 1
 * @author Alexey Samara <[email protected]>
32
 */
33
class SlackMessageValidator
34
{
35
    const ATTACHMENTS_LIMIT = 20;
36
37
    /**
38
     * @param SlackMessage $message
39
     *
40 1
     * @throws SlackbotException
41
     */
42 1
    public static function validateMessage(SlackMessage $message)
43 1
    {
44
        self::slackMessageDto($message);
45
    }
46
47
    /**
48
     * @param SlackMessage $message
49
     *
50
     * @throws SlackbotException
51
     */
52
    private static function slackMessageDto(SlackMessage $message)
53
    {
54
        if (empty($message->getUsername())) {
55
            throw new SlackbotException(SlackbotException::E_EMPTY_USERNAME);
56
        }
57
58
        if (empty($message->getChannel())) {
59
            throw new SlackbotException(SlackbotException::E_EMPTY_CHANNEL);
60
        }
61
62
        self::checkIcon($message);
63
64
        if (empty($message->getText()) && empty($message->getAttachments())) {
65
            throw new SlackbotException(SlackbotException::E_EMPTY_TEXT);
66
        }
67
68
        self::checkAttachments($message);
69
    }
70
71
    /**
72
     * @param SlackMessage $message
73
     *
74
     * @throws SlackbotException
75
     */
76
    private static function checkIcon(SlackMessage $message)
77
    {
78
        if (!empty($message->getIconUrl())) {
79
            if (!preg_match(
80
                '/^(?:http|https|ftp)\:\/\/(.*?)\.(.*?)\/(.*?)\.(?:jpg|jpeg|png)/i',
81
                $message->getIconUrl()
82
            )) {
83
                throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_URL);
84
            }
85
        }
86
87
        if (!empty($message->getIconEmoji())) {
88
            if (!preg_match(SlackEmojiTest::EMOJI_FORMAT_PATTERN, $message->getIconEmoji())) {
89
                throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_EMOJI);
90
            }
91
        }
92
    }
93
94
    private static function checkAttachments(SlackMessage $message)
95
    {
96
        if (count($message->getAttachments()) > self::ATTACHMENTS_LIMIT) {
97
            throw new SlackbotException(
98
                SlackbotException::E_ATTACHMENT_LIMIT_EXCEEDED,
99
                ['max_attachments_allowed' => self::ATTACHMENTS_LIMIT]
100
            );
101
        }
102
103
        if (!empty($message->getAttachments())) {
104
            foreach ($message->getAttachments() as $attachment) {
105
                self::attachmentDto($attachment);
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param Attachment $attachment
112
     *
113
     * @throws SlackbotException
114
     */
115
    private static function attachmentDto(Attachment $attachment)
116
    {
117
        if (!in_array($attachment->getColor(), SlackColor::COLOR_MAP)) {
118
            throw new SlackbotException(SlackbotException::E_INCORRECT_COLOR);
119
        }
120
121
        if (empty($attachment->getAuthorName())
122
            && (!empty($attachment->getAuthorLink()) || !empty($attachment->getAuthorIconUrl()))
123
        ) {
124
            throw new SlackbotException(SlackbotException::E_ATTACHMENT_AUTHOR_EMPTY);
125
        }
126
127
        if (empty($attachment->getTitle()) && !empty($attachment->getTitleLink())) {
128
            throw new SlackbotException(SlackbotException::E_ATTACHMENT_TITLE_EMPTY);
129
        }
130
131
        foreach ($attachment->getActions() as $action) {
132
            self::checkAttachmentAction($action);
133
        }
134
    }
135
136
    /**
137
     * @param AttachmentAction $action
138
     *
139
     * @throws SlackbotException
140
     */
141
    private static function checkAttachmentAction(AttachmentAction $action)
142
    {
143
        if (AttachmentAction::TYPE_BUTTON !== $action->getType()) {
144
            throw new SlackbotException(SlackbotException::E_ACTION_TYPE_NOT_BUTTON);
145
        }
146
147
        if (empty($action->getText())) {
148
            throw new SlackbotException(SlackbotException::E_ACTION_TEXT_EMPTY);
149
        }
150
151
        if ($action->getConfirm()->isActive()) {
152
            self::attachmentActionConfirm($action->getConfirm());
153
        }
154
    }
155
156
    /**
157
     * @param AttachmentActionConfirm $actionConfirm
158
     *
159
     * @throws SlackbotException
160
     */
161
    private static function attachmentActionConfirm(AttachmentActionConfirm $actionConfirm)
162
    {
163
        if (empty($actionConfirm->getTitle())) {
164
            throw new SlackbotException(SlackbotException::E_ACTION_CONFIRM_TITLE_EMPTY);
165
        }
166
167
        if (empty($actionConfirm->getText())) {
168
            throw new SlackbotException(SlackbotException::E_ACTION_CONFIRM_TEXT_EMPTY);
169
        }
170
    }
171
}
172