Completed
Push — master ( f54dae...4fe7c3 )
by jelmer
02:12
created

Channel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 11
c 4
b 0
f 2
lcom 1
cbo 1
dl 0
loc 94
ccs 36
cts 36
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setName() 0 37 6
A getChannel() 0 4 1
A __toString() 0 4 1
A isValidName() 0 12 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 17
        $name = trim($name);
35
36 17
        if (strlen($name) === 0) {
37 1
            throw new InvalidChannelException(
38
                'The name should start with "#" for a channel or "@" for an account.
39 1
                    The name that was provided did not start with either of those.',
40
                400
41 1
            );
42
        }
43
44
        // names should be lowercase so we just enforce this without further validation
45 16
        $name = mb_strtolower($name, 'UTF8');
46
47 16
        switch ($name[0]) {
48 16
            case '#':
49 9
                if ($this->isValidName($name, '_^#[\w-]{1,21}$_')) {
50 5
                    $this->name = $name;
51 5
                }
52
53 5
                return;
54 7
            case '@':
55
                // check if the username matches the requirements from slack
56 6
                if ($this->isValidName($name, '_^@[\w-.]{1,21}$_')) {
57 2
                    $this->name = $name;
58 2
                }
59
60 2
                return;
61 1
            default:
62 1
                throw new InvalidChannelException(
63
                    'The name should start with "#" for a channel or "@" for an account.
64 1
                    The name that was provided did not start with either of those.',
65
                    400
66 1
                );
67 1
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 5
    public function getChannel()
74
    {
75 5
        return $this->name;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 4
    public function __toString()
82
    {
83 4
        return $this->getChannel();
84
    }
85
86
    /**
87
     * Check if the name is valid against a regular expression.
88
     *
89
     * @param string $name
90
     * @param string $validationRegex
91
     *
92
     * @return bool
93
     */
94 15
    private function isValidName($name, $validationRegex)
95
    {
96 15
        if (!preg_match($validationRegex, $name)) {
97 8
            throw new InvalidChannelException(
98
                'Channel names must be all lowercase.
99 8
                 They cannot be longer than 21 characters and can only contain letters, numbers, hyphens, and underscores.',
100
                400
101 8
            );
102
        }
103
104 7
        return true;
105
    }
106
}
107