Passed
Push — master ( 5521dc...e99b67 )
by Vladimir
01:54
created

ChannelManager::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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