Completed
Push — master ( 61e940...6fab34 )
by jelmer
02:26
created

Colour   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 82
ccs 29
cts 29
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setColour() 0 19 3
A getColour() 0 4 1
A __toString() 0 4 1
A jsonSerialize() 0 4 1
A getDefaultColours() 0 8 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use JsonSerializable;
6
use Pageon\SlackWebhookMonolog\Slack\Exceptions\InvalidAttachmentColourException;
7
8
/**
9
 * @author Jelmer Prins <[email protected]>
10
 *
11
 * An optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0).
12
 * This value is used to color the border along the left side of the message attachment.
13
 *
14
 * @since 0.3.2
15
 */
16
final class Colour implements JsonSerializable
17
{
18
    const COLOUR_GOOD = 'good';
19
    const COLOUR_WARNING = 'warning';
20
    const COLOUR_DANGER = 'danger';
21
22
    /**
23
     * @var string
24
     */
25
    private $colour;
26
27
    /**
28
     * @param string $colour
29
     */
30 5
    public function __construct($colour)
31
    {
32 5
        $this->setColour($colour);
33 4
    }
34
35
    /**
36
     * Set the colour if it is a valid colour.
37
     *
38
     * @param $colour
39
     */
40 5
    private function setColour($colour)
41
    {
42 5
        if (!in_array($colour, $this->getDefaultColours()) &&
43 2
            !preg_match(
44 2
                '_^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$_',
45
                $colour
46 2
            )
47 5
        ) {
48 1
            throw new InvalidAttachmentColourException(
49 1
                sprintf(
50 1
                    'The colour "%s" is not a valid colour. Possible options are "%s" or a valid hex colour',
51 1
                    $colour,
52 1
                    implode('", "', $this->getDefaultColours())
53 1
                )
54 1
            );
55
        }
56
57 4
        $this->colour = $colour;
58 4
    }
59
60
    /**
61
     * @return string
62
     */
63 4
    public function getColour()
64
    {
65 4
        return $this->colour;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 3
    public function __toString()
72
    {
73 3
        return $this->getColour();
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79 1
    public function jsonSerialize()
80
    {
81 1
        return (string) $this;
82
    }
83
84
    /**
85
     * Get the possible default colours.
86
     *
87
     * @return array
88
     */
89 5
    private function getDefaultColours()
90
    {
91
        return [
92 5
            self::COLOUR_GOOD,
93 5
            self::COLOUR_WARNING,
94 5
            self::COLOUR_DANGER,
95 5
        ];
96
    }
97
}
98