Passed
Push — master ( 3db212...288d3b )
by Aleksei
20:14 queued 09:04
created

DistributionBootloader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 4
eloc 25
c 0
b 0
f 0
dl 0
loc 49
ccs 32
cts 33
cp 0.9697
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerResolver() 0 10 1
A init() 0 9 1
A registerManager() 0 23 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Distribution\Bootloader;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Config\ConfiguratorInterface;
10
use Spiral\Core\BinderInterface;
11
use Spiral\Distribution\Config\DistributionConfig;
12
use Spiral\Distribution\DistributionInterface;
13
use Spiral\Distribution\Manager;
14
use Spiral\Distribution\MutableDistributionInterface;
15
use Spiral\Distribution\Resolver\UriResolver;
16
use Spiral\Distribution\UriResolverInterface;
17
18
class DistributionBootloader extends Bootloader
19
{
20 359
    public function init(ConfiguratorInterface $config, BinderInterface $binder): void
21
    {
22 359
        $config->setDefaults(DistributionConfig::CONFIG, [
23 359
            'default' => Manager::DEFAULT_RESOLVER,
24 359
            'resolvers' => [],
25 359
        ]);
26
27 359
        $this->registerManager($binder);
28 359
        $this->registerResolver($binder);
29
    }
30
31 359
    private function registerResolver(BinderInterface $binder): void
32
    {
33 359
        $binder->bindSingleton(
34 359
            UriResolverInterface::class,
35 359
            static fn (DistributionInterface $dist): UriResolverInterface => $dist->resolver()
36 359
        );
37
38 359
        $binder->bindSingleton(
39 359
            UriResolver::class,
40 359
            static fn (ContainerInterface $app) => $app->get(UriResolverInterface::class)
41 359
        );
42
    }
43
44 359
    private function registerManager(BinderInterface $binder): void
45
    {
46 359
        $binder->bindSingleton(
47 359
            DistributionInterface::class,
48 359
            static function (DistributionConfig $config): DistributionInterface {
49 3
                $manager = new Manager($config->getDefaultDriver());
50
51 3
                foreach ($config->getResolvers() as $name => $resolver) {
52
                    $manager->add($name, $resolver);
53
                }
54
55 3
                return $manager;
56 359
            },
57 359
        );
58
59 359
        $binder->bindSingleton(
60 359
            MutableDistributionInterface::class,
61 359
            static fn (ContainerInterface $app) => $app->get(DistributionInterface::class)
62 359
        );
63
64 359
        $binder->bindSingleton(
65 359
            Manager::class,
66 359
            static fn (ContainerInterface $app) => $app->get(DistributionInterface::class)
67 359
        );
68
    }
69
}
70