Passed
Push — master ( 676b4d...97599d )
by Vladimir
09:05
created

DriverManager::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use FondBot\Helpers\Arr;
8
use FondBot\Http\Request;
9
use FondBot\Channels\Channel;
10
use FondBot\Drivers\Exceptions\DriverNotFound;
11
use FondBot\Drivers\Exceptions\InvalidConfiguration;
12
13
class DriverManager
14
{
15
    /** @var Driver[] */
16
    private $drivers = [];
17
18
    /**
19
     * Add driver.
20
     *
21
     * @param Driver $driver
22
     */
23 2
    public function add(Driver $driver): void
24
    {
25 2
        $this->drivers[$driver->getShortName()] = $driver;
26 2
    }
27
28
    /**
29
     * Get all registered drivers.
30
     *
31
     * @return array
32
     */
33
    public function all(): array
34
    {
35
        return $this->drivers;
36
    }
37
38
    /**
39
     * Get driver for channel.
40
     *
41
     * @param Channel $channel
42
     * @param Request $request
43
     *
44
     * @return Driver
45
     *
46
     * @throws InvalidConfiguration
47
     * @throws DriverNotFound
48
     */
49 2
    public function get(Channel $channel, Request $request): Driver
50
    {
51 2
        $driver = Arr::get($this->drivers, $channel->getDriver());
52
53 2
        if ($driver === null || !$driver instanceof Driver) {
54 1
            throw new DriverNotFound('Driver `'.$channel->getDriver().'` not found.');
55
        }
56
57 1
        $driver->initialize($channel->getParameters(), $request);
58
59 1
        return $driver;
60
    }
61
}
62