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