Completed
Pull Request — master (#50)
by Vladimir
04:58 queued 02:00
created

DriverManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A get() 0 10 3
A all() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use FondBot\Helpers\Arr;
8
use FondBot\Drivers\Exceptions\DriverNotFound;
9
use FondBot\Drivers\Exceptions\InvalidConfiguration;
10
11
class DriverManager
12
{
13
    /** @var Driver[] */
14
    private $drivers = [];
15
16
    /**
17
     * Add driver.
18
     *
19
     * @param Driver $driver
20
     */
21 3
    public function add(Driver $driver): void
22
    {
23 3
        $this->drivers[$driver->getShortName()] = $driver;
24 3
    }
25
26
    /**
27
     * Get all registered drivers.
28
     *
29
     * @return array
30
     */
31 1
    public function all(): array
32
    {
33 1
        return $this->drivers;
34
    }
35
36
    /**
37
     * Get driver for channel.
38
     *
39
     * @param string $name
40
     *
41
     * @return Driver
42
     *
43
     * @throws InvalidConfiguration
44
     * @throws DriverNotFound
45
     */
46 2
    public function get(string $name): Driver
47
    {
48 2
        $driver = Arr::get($this->drivers, $name);
49
50 2
        if ($driver === null || !$driver instanceof Driver) {
51 1
            throw new DriverNotFound('Driver `'.$name.'` not found.');
52
        }
53
54 1
        return $driver;
55
    }
56
}
57