Completed
Push — master ( 10533f...9c60cb )
by Vladimir
02:25
created

ChannelManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A all() 0 4 1
A get() 0 14 2
A getDefaultDriver() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels;
6
7
use FondBot\Drivers\Driver;
8
use Illuminate\Support\Manager;
9
use FondBot\Channels\Exceptions\ChannelNotFound;
10
11
/**
12
 * Class ChannelManager.
13
 *
14
 * @method Driver createDriver($driver)
15
 */
16
class ChannelManager extends Manager
17
{
18
    /** @var array */
19
    private $channels = [];
20
21
    /**
22
     * Register channels.
23
     *
24
     * @param array $channels
25
     */
26 2
    public function register(array $channels): void
27
    {
28 2
        $this->channels = $channels;
29 2
    }
30
31
    /**
32
     * Get all channels.
33
     *
34
     * @return array
35
     */
36 1
    public function all(): array
37
    {
38 1
        return $this->channels;
39
    }
40
41
    /**
42
     * Get channel by name.
43
     *
44
     * @param string $name
45
     *
46
     * @return Channel
47
     * @throws ChannelNotFound
48
     */
49 2
    public function get(string $name): Channel
50
    {
51 2
        if (!array_has($this->channels, $name)) {
52 1
            throw new ChannelNotFound('Channel `'.$name.'` not found.');
53
        }
54
55 1
        $parameters = $this->channels[$name];
56
57
        // Create driver and initialize it with channel parameters
58 1
        $driver = $this->createDriver($parameters['driver']);
59 1
        $driver->initialize(collect($parameters)->except('driver'));
60
61 1
        return new Channel($name, $driver);
62
    }
63
64
    /**
65
     * Get the default driver name.
66
     *
67
     * @return string
68
     */
69 1
    public function getDefaultDriver()
70
    {
71 1
        return null;
72
    }
73
}
74