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