Completed
Push — master ( afce1d...f8221d )
by Vladimir
09:37
created

DriverManager::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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