Completed
Push — master ( f2cd16...c9da8a )
by jelmer
02:26
created

Channel::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack;
4
5
use Pageon\SlackWebhookMonolog\Slack\Exceptions\InvalidChannelException;
6
use Pageon\SlackWebhookMonolog\Slack\Interfaces\ChannelInterface;
7
8
/**
9
 * @author Jelmer Prins <[email protected]>
10
 *
11
 * @since 0.1.0
12
 */
13
class Channel implements ChannelInterface
14
{
15
    private $name;
16
17
    /**
18
     * Channel constructor.
19
     *
20
     * @param string $name
21
     */
22 17
    public function __construct($name)
23
    {
24 17
        $this->setName($name);
25 7
    }
26
27
    /**
28
     * @param string $name
29
     *
30
     * @return self
31
     */
32 17
    private function setName($name)
33
    {
34
        // names should be lowercase so we just enforce this without further validation
35 17
        $name = trim(mb_strtolower($name, 'UTF8'));
36
37 17
        if (!preg_match('_^[#@][\w-]{1,21}$_', $name)) {
38 10
            throw new InvalidChannelException(
39
                'Channel names must be all lowercase.
40
                 The name should start with "#" for a channel or "@" for an account
41 10
                 They cannot be longer than 21 characters and can only contain letters, numbers, hyphens, and underscores.',
42
                400
43 10
            );
44
        }
45
46 7
        $this->name = $name;
47 7
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 5
    public function getChannel()
53
    {
54 5
        return $this->name;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 4
    public function __toString()
61
    {
62 4
        return $this->getChannel();
63
    }
64
}
65