Passed
Push — master ( 98541d...d359a0 )
by
unknown
08:18
created

DriverManager::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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