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

SlackMessageValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 48
ccs 6
cts 16
cp 0.375
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateMessage() 0 6 2
A validateIconEmoji() 0 6 3
A setDefaultsForEmptyFields() 0 16 4
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