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

DriverManager::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 6
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\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