Passed
Push — master ( ae08b1...91bc2f )
by Alexey
04:51
created

SlackMessageValidator::validateIconEmoji()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.1406
1
<?php
2
/**
3
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3
4
 * https://github.com/wow-apps/symfony-slack-bot
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\SlackBundle\Service;
13
14
use WowApps\SlackBundle\DTO\SlackMessage;
15
use WowApps\SlackBundle\Exception\SlackbotException;
16
17
/**
18
 * @author Alexey Samara <[email protected]>
19
 * @package WowApps\SlackBundle
20
 */
21
class SlackMessageValidator
22
{
23
    /**
24
     * @param SlackMessage $slackMessage
25
     * @return void
26
     * @throws SlackbotException
27
     */
28 1
    public function validateMessage(SlackMessage $slackMessage)
29
    {
30 1
        if (!$slackMessage->getText()) {
31 1
            throw new SlackbotException(SlackbotException::E_EMPTY_MESSAGE);
32
        }
33
    }
34
35
    /**
36
     * @param SlackMessage $slackMessage
37
     * @return void
38
     * @throws SlackbotException
39
     */
40 1
    public function validateIconEmoji(SlackMessage $slackMessage)
41
    {
42 1
        if (!empty($slackMessage->getIconEmoji()) && !preg_match('/^\:(.*?)\:$/i', $slackMessage->getIconEmoji())) {
43 1
            throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_EMOJI);
44
        }
45
    }
46
47
    /**
48
     * @param SlackMessage $slackMessage
49
     * @param array $config
50
     * @return SlackMessage
51
     */
52
    public function setDefaultsForEmptyFields(SlackMessage $slackMessage, array $config): SlackMessage
53
    {
54
        if (!$slackMessage->getIcon()) {
0 ignored issues
show
Deprecated Code introduced by
The method WowApps\SlackBundle\DTO\SlackMessage::getIcon() has been deprecated.

This method has been deprecated.

Loading history...
55
            $slackMessage->setIcon($config['default_icon']);
0 ignored issues
show
Deprecated Code introduced by
The method WowApps\SlackBundle\DTO\SlackMessage::setIcon() has been deprecated.

This method has been deprecated.

Loading history...
56
        }
57
58
        if (!$slackMessage->getRecipient()) {
59
            $slackMessage->setRecipient($config['default_channel']);
60
        }
61
62
        if (!$slackMessage->getSender()) {
63
            $slackMessage->setSender('SlackBot');
64
        }
65
66
        return $slackMessage;
67
    }
68
}
69