ChannelManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels;
6
7
use Illuminate\Support\Manager;
8
use Illuminate\Support\Collection;
9
use FondBot\Channels\Exceptions\ChannelNotFound;
10
11
/**
12
 * Class ChannelManager.
13
 *
14
 * @method Driver driver($driver = null)
15
 * @method Driver createDriver($driver)
16
 */
17
class ChannelManager extends Manager
18
{
19
    /** @var Collection */
20
    private $channels;
21
22
    public function __construct($app)
23 82
    {
24
        parent::__construct($app);
25 82
26
        $this->channels = collect([]);
27 82
    }
28 82
29
    /**
30
     * Register channels.
31
     *
32
     * @param array $channels
33
     */
34
    public function register(array $channels): void
35 82
    {
36
        $this->channels = collect($channels);
37 82
    }
38 82
39
    /**
40
     * Get all channels.
41
     *
42
     * @return Collection
43
     */
44
    public function all(): Collection
45 1
    {
46
        return $this->channels;
47 1
    }
48
49
    /**
50
     * Get channels by driver.
51
     *
52
     * @param string $driver
53
     *
54
     * @return Collection
55
     */
56
    public function getByDriver(string $driver): Collection
57 1
    {
58
        return $this->channels->filter(function (array $channel) use ($driver) {
59
            return $channel['driver'] === $driver;
60 1
        });
61 1
    }
62
63
    /**
64
     * Create channel.
65
     *
66
     * @param string $name
67
     *
68
     * @return Channel
69
     * @throws ChannelNotFound
70
     */
71
    public function create(string $name): Channel
72 2
    {
73
        if (!array_has($this->channels, $name)) {
74 2
            throw new ChannelNotFound('Channel `'.$name.'` not found.');
75 1
        }
76
77
        $parameters = $this->channels[$name];
78 1
79
        // Create driver and initialize it with channel parameters
80
        $driver = $this->createDriver($parameters['driver']);
81 1
        $driver->initialize(collect($parameters)->except('driver'));
82 1
83
        return new Channel($name, $driver, $parameters['webhook-secret'] ?? null);
84 1
    }
85
86
    /**
87
     * Get the default driver name.
88
     *
89
     * @return string
90
     */
91
    public function getDefaultDriver(): ?string
92 1
    {
93
        return null;
94 1
    }
95
}
96