Channel::setName()   A
last analyzed

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
use Pageon\SlackWebhookMonolog\General\SerializeToString;
8
9
/**
10
 * @author Jelmer Prins <[email protected]>
11
 *
12
 * @since 0.1.0
13
 */
14
class Channel extends SerializeToString implements ChannelInterface
15
{
16
    private $name;
17
18
    /**
19
     * Channel constructor.
20
     *
21
     * @param string $name
22
     */
23 22
    public function __construct($name)
24
    {
25 22
        $this->setName($name);
26 12
    }
27
28
    /**
29
     * @param string $name
30
     *
31
     * @return self
32
     */
33 22
    private function setName($name)
34
    {
35
        // names should be lowercase so we just enforce this without further validation
36 22
        $name = trim(mb_strtolower($name, 'UTF8'));
37
38 22
        if (!preg_match('_^[#@][\w-]{1,21}$_', $name)) {
39 10
            throw new InvalidChannelException(
40
                'Channel names must be all lowercase.
41
                 The name should start with "#" for a channel or "@" for an account
42 10
                 They cannot be longer than 21 characters and can only contain letters, numbers, hyphens, and underscores.',
43
                400
44 10
            );
45
        }
46
47 12
        $this->name = $name;
48 12
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 10
    public function getChannel()
54
    {
55 10
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 9
    public function __toString()
62
    {
63 9
        return $this->getChannel();
64
    }
65
}
66