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

SlackColor::getHex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Exception\SlackbotException;
22
23
/**
24
 * Class SlackColor.
25
 *
26
 * @author Alexey Samara <[email protected]>
27
 */
28
class SlackColor
29
{
30
    const COLOR_DEFAULT = 'default';
31
    const COLOR_DANGER = 'danger';
32
    const COLOR_SUCCESS = 'success';
33
    const COLOR_WARNING = 'warning';
34
    const COLOR_INFO = 'info';
35
36
    const COLOR_MAP = [
37
        self::COLOR_DEFAULT,
38
        self::COLOR_DANGER,
39
        self::COLOR_SUCCESS,
40
        self::COLOR_WARNING,
41
        self::COLOR_INFO,
42
    ];
43
44
    /** @var array */
45
    private $colorHexCodes = [
46
        self::COLOR_DEFAULT => '',
47
        self::COLOR_DANGER => '',
48
        self::COLOR_SUCCESS => '',
49
        self::COLOR_WARNING => '',
50
        self::COLOR_INFO => '',
51
    ];
52
53
    /**
54
     * SlackColor constructor.
55
     *
56
     * @param array $config
57
     */
58
    public function __construct(array $config)
59
    {
60
        foreach (self::COLOR_MAP as $colorName) {
61
            $this->colorHexCodes[$colorName] = !empty($config['colors'][$colorName])
62
                ? $config['colors'][$colorName] : '';
63
        }
64
    }
65
66
    /**
67
     * @param string $color
68
     *
69
     * @return string
70
     */
71
    public function getHex(string $color): string
72
    {
73
        if (!in_array($color, self::COLOR_MAP)) {
74
            throw new SlackbotException(SlackbotException::E_INCORRECT_COLOR);
75
        }
76
77
        return $this->colorHexCodes[$color];
78
    }
79
}
80