Test Failed
Pull Request — master (#20)
by Alexey
16:57
created

SlackMessageValidator::checkAttachmentAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 - 2020 WoW-Apps.
17
 */
18
19
namespace WowApps\SlackBundle\Service;
20
21
use WowApps\SlackBundle\Entity\Attachment;
22
use WowApps\SlackBundle\Entity\AttachmentAction;
23
use WowApps\SlackBundle\Entity\AttachmentActionConfirm;
24
use WowApps\SlackBundle\Entity\SlackMessage;
25
use WowApps\SlackBundle\Exception\SlackbotException;
26
27
/**
28
 * Class SlackMessageValidator.
29
 *
30
 * @author Alexey Samara <[email protected]>
31
 */
32
class SlackMessageValidator
33
{
34
    const ATTACHMENTS_LIMIT = 20;
35
36
    /**
37
     * @param SlackMessage $message
38
     *
39
     * @throws SlackbotException
40
     */
41 14
    public static function validateMessage(SlackMessage $message)
42
    {
43 14
        self::slackMessageDto($message);
44 1
    }
45
46
    /**
47
     * @param SlackMessage $message
48
     *
49
     * @throws SlackbotException
50
     */
51 14
    private static function slackMessageDto(SlackMessage $message)
52
    {
53 14
        if (empty($message->getUsername())) {
54 1
            throw new SlackbotException(SlackbotException::E_EMPTY_USERNAME);
55
        }
56
57 13
        self::checkIcon($message);
58
59 11
        if (empty($message->getText()) && empty($message->getAttachments())) {
60 1
            throw new SlackbotException(SlackbotException::E_EMPTY_TEXT);
61
        }
62
63 10
        self::checkAttachments($message);
64 1
    }
65
66
    /**
67
     * @param SlackMessage $message
68
     *
69
     * @throws SlackbotException
70
     */
71 13
    private static function checkIcon(SlackMessage $message)
72
    {
73 13 View Code Duplication
        if (!empty($message->getIconUrl())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 13
            if (!preg_match(
75 13
                '/^(?:http|https|ftp)\:\/\/(.*?)\.(.*?)\/(.*?)\.(?:jpg|jpeg|png)/i',
76 13
                $message->getIconUrl()
77
            )) {
78 1
                throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_URL);
79
            }
80
        }
81
82 12 View Code Duplication
        if (!empty($message->getIconEmoji())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83 12
            if (!preg_match('/^\:[a-zA-Z\-\_\d]+\:$/i', $message->getIconEmoji())) {
84 1
                throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_EMOJI);
85
            }
86
        }
87 11
    }
88
89 10
    private static function checkAttachments(SlackMessage $message)
90
    {
91 10
        if (count($message->getAttachments()) > self::ATTACHMENTS_LIMIT) {
92 1
            throw new SlackbotException(
93 1
                SlackbotException::E_ATTACHMENT_LIMIT_EXCEEDED,
94 1
                ['max_attachments_allowed' => self::ATTACHMENTS_LIMIT]
95
            );
96
        }
97
98 9
        if (!empty($message->getAttachments())) {
99 9
            foreach ($message->getAttachments() as $attachment) {
100 9
                self::attachmentDto($attachment);
101
            }
102
        }
103 1
    }
104
105
    /**
106
     * @param Attachment $attachment
107
     *
108
     * @throws SlackbotException
109
     */
110 9
    private static function attachmentDto(Attachment $attachment)
111
    {
112 9
        if (!in_array($attachment->getColor(), SlackColor::COLOR_MAP)) {
113 1
            throw new SlackbotException(SlackbotException::E_INCORRECT_COLOR);
114
        }
115
116 8
        if (empty($attachment->getAuthorName())
117 8
            && (!empty($attachment->getAuthorLink()) || !empty($attachment->getAuthorIconUrl()))
118
        ) {
119 2
            throw new SlackbotException(SlackbotException::E_ATTACHMENT_AUTHOR_EMPTY);
120
        }
121
122 6
        if (empty($attachment->getTitle()) && !empty($attachment->getTitleLink())) {
123 1
            throw new SlackbotException(SlackbotException::E_ATTACHMENT_TITLE_EMPTY);
124
        }
125
126 5
        foreach ($attachment->getActions() as $action) {
127 5
            self::checkAttachmentAction($action);
128
        }
129 1
    }
130
131
    /**
132
     * @param AttachmentAction $action
133
     *
134
     * @throws SlackbotException
135
     */
136 5
    private static function checkAttachmentAction(AttachmentAction $action)
137
    {
138 5
        if (AttachmentAction::TYPE_BUTTON !== $action->getType()) {
139 1
            throw new SlackbotException(SlackbotException::E_ACTION_TYPE_NOT_BUTTON);
140
        }
141
142 4
        if (empty($action->getText())) {
143 1
            throw new SlackbotException(SlackbotException::E_ACTION_TEXT_EMPTY);
144
        }
145
146 3
        if ($action->getConfirm()->isActive()) {
147 3
            self::attachmentActionConfirm($action->getConfirm());
148
        }
149 1
    }
150
151
    /**
152
     * @param AttachmentActionConfirm $actionConfirm
153
     *
154
     * @throws SlackbotException
155
     */
156 3
    private static function attachmentActionConfirm(AttachmentActionConfirm $actionConfirm)
157
    {
158 3
        if (empty($actionConfirm->getTitle())) {
159 1
            throw new SlackbotException(SlackbotException::E_ACTION_CONFIRM_TITLE_EMPTY);
160
        }
161
162 2
        if (empty($actionConfirm->getText())) {
163 1
            throw new SlackbotException(SlackbotException::E_ACTION_CONFIRM_TEXT_EMPTY);
164
        }
165 1
    }
166
}
167