Completed
Push — master ( 817c3d...f287a4 )
by Vladimir
03:07
created

DriverManager::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use FondBot\Channels\Channel;
8
use FondBot\Contracts\Drivers\Driver;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FondBot\Drivers\Driver.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use FondBot\Channels\Exceptions\InvalidConfiguration;
10
11
class DriverManager
12
{
13
    /** @var Driver[] */
14
    private $drivers;
15
16
    /**
17
     * Add driver.
18
     *
19
     * @param string $alias
20
     * @param Driver $instance
21
     */
22 2
    public function add(string $alias, Driver $instance): void
23
    {
24 2
        $this->drivers[$alias] = $instance;
25 2
    }
26
27
    /**
28
     * Get driver instance.
29
     *
30
     * @param Channel $channel
31
     *
32
     * @param array   $request
33
     * @param array   $headers
34
     * @param array   $parameters
35
     *
36
     * @return Driver
37
     */
38 2
    public function get(Channel $channel, array $request = [], array $headers = [], array $parameters = []): Driver
39
    {
40 2
        $driver = $this->drivers[$channel->getDriver()];
41
42 2
        $this->validateParameters($channel, $driver);
43
44 1
        $driver->fill($parameters, $request, $headers);
45
46 1
        return $driver;
47
    }
48
49
    /**
50
     * Validate channel parameters with driver requirements.
51
     *
52
     * @param Channel $channel
53
     * @param Driver  $driver
54
     */
55
    private function validateParameters(Channel $channel, Driver $driver): void
56
    {
57 2
        collect($driver->getConfig())->each(function (string $parameter) use ($channel) {
58 2
            if (!array_key_exists($parameter, $channel->getParameters())) {
59 1
                throw new InvalidConfiguration('Invalid `'.$channel->getName().'` channel configuration.');
60
            }
61 2
        });
62 1
    }
63
}
64