Passed
Branch 1.0 (690a53)
by Vladimir
07:08
created

DriverServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 41
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use FondBot\Helpers\Arr;
8
use TheCodingMachine\Discovery\Discovery;
9
use League\Container\ServiceProvider\AbstractServiceProvider;
10
11
class DriverServiceProvider extends AbstractServiceProvider
12
{
13
    protected $provides = [
14
        DriverManager::class,
15
    ];
16
17
    private $discovery;
18
19 1
    public function __construct(Discovery $discovery = null)
20
    {
21 1
        $this->discovery = $discovery ?? Discovery::getInstance();
22 1
    }
23
24
    /**
25
     * Use the register method to register items with the container via the
26
     * protected $this->container property or the `getContainer` method
27
     * from the ContainerAwareTrait.
28
     *
29
     * @return void
30
     */
31
    public function register(): void
32
    {
33 1
        $this->getContainer()->share(DriverManager::class, function () {
34 1
            $manager = new DriverManager($this->getContainer());
35
36
            // Here we will discover all drivers installed
37
            // And add all found drivers to the manager
38 1
            $assets = $this->discovery->getAssetType(Driver::class);
39
40 1
            foreach ($assets->getAssets() as $asset) {
41 1
                $manager->add(
42 1
                    $this->getContainer()->get($asset->getValue()),
43 1
                    $asset->getMetadata()['name'],
44 1
                    Arr::get($asset->getMetadata(), 'parameters', [])
45
                );
46
            }
47
48 1
            return $manager;
49 1
        });
50 1
    }
51
}
52