Completed
Push — master ( e7ffb0...9d31f0 )
by Alexey
03:03
created

SlackMessageValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 39
c 0
b 0
f 0
ccs 3
cts 12
cp 0.25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateMessage() 0 6 2
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
 * Class SlackMessageValidator
19
 *
20
 * @author Alexey Samara <[email protected]>
21
 * @package WowApps\SlackBundle
22
 */
23
class SlackMessageValidator
24
{
25
    /**
26
     * Validate fields
27
     *
28
     * @param SlackMessage $slackMessage
29
     * @return void
30
     */
31 1
    public function validateMessage(SlackMessage $slackMessage)
32
    {
33 1
        if (!$slackMessage->getText()) {
34 1
            throw new SlackbotException(SlackbotException::E_EMPTY_MESSAGE);
35
        }
36
    }
37
38
    /**
39
     * Setting default values for empty fields
40
     *
41
     * @param SlackMessage $slackMessage
42
     * @param array $config
43
     * @return SlackMessage
44
     */
45
    public function setDefaultsForEmptyFields(SlackMessage $slackMessage, array $config): SlackMessage
46
    {
47
        if (!$slackMessage->getIcon()) {
48
            $slackMessage->setIcon($config['default_icon']);
49
        }
50
51
        if (!$slackMessage->getRecipient()) {
52
            $slackMessage->setRecipient($config['default_channel']);
53
        }
54
55
        if (!$slackMessage->getSender()) {
56
            $slackMessage->setSender('SlackBot');
57
        }
58
59
        return $slackMessage;
60
    }
61
}
62