Passed
Push — master ( 922d24...7b732c )
by Vladimir
06:12
created

DriverManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A get() 0 12 3
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 AbstractDriver[] */
16
    private $drivers = [];
17
18
    /**
19
     * Add driver.
20
     *
21
     * @param AbstractDriver $driver
22
     */
23 2
    public function add(AbstractDriver $driver): void
24
    {
25 2
        $this->drivers[$driver->getShortName()] = $driver;
26 2
    }
27
28
    /**
29
     * Get driver for channel.
30
     *
31
     * @param Channel $channel
32
     * @param Request $request
33
     *
34
     * @return AbstractDriver
35
     *
36
     * @throws InvalidConfiguration
37
     * @throws DriverNotFound
38
     */
39 2
    public function get(Channel $channel, Request $request): AbstractDriver
40
    {
41 2
        $driver = Arr::get($this->drivers, $channel->getDriver());
42
43 2
        if ($driver === null || !$driver instanceof AbstractDriver) {
44 1
            throw new DriverNotFound('Driver `'.$channel->getDriver().'` not found.');
45
        }
46
47 1
        $driver->initialize($channel->getParameters(), $request);
48
49 1
        return $driver;
50
    }
51
}
52