Passed
Pull Request — master (#407)
by Kirill
11:08 queued 03:58
created

StorageBootloader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A storageRegistrar() 0 4 1
A uriResolverRegistrar() 0 4 1
A boot() 0 12 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader;
13
14
use Spiral\Boot\Bootloader\Bootloader;
15
use Spiral\Config\ConfiguratorInterface;
16
use Spiral\Core\Container;
17
use Spiral\Storage\Config\StorageConfig;
18
use Spiral\Storage\Parser\UriParser;
19
use Spiral\Storage\Parser\UriParserInterface;
20
use Spiral\Storage\Storage;
21
use Spiral\Storage\StorageInterface;
22
use Spiral\Storage\UriResolver;
23
use Spiral\Storage\UriResolverInterface;
24
25
class StorageBootloader extends Bootloader
26
{
27
    /**
28
     * @var array<class-string, class-string>
29
     */
30
    protected const SINGLETONS = [
31
        UriParser::class          => UriParser::class,
32
        UriParserInterface::class => UriParser::class,
33
    ];
34
35
    /**
36
     * @param Container $app
37
     */
38
    public function boot(Container $app): void
39
    {
40
        $app->bindInjector(StorageConfig::class, ConfiguratorInterface::class);
41
42
        $app->bindSingleton(UriResolverInterface::class, $this->uriResolverRegistrar());
43
        $app->bindSingleton(UriResolver::class, static function (UriResolverInterface $resolver) {
44
            return $resolver;
45
        });
46
47
        $app->bindSingleton(StorageInterface::class, $this->storageRegistrar());
48
        $app->bindSingleton(Storage::class, static function (StorageInterface $storage) {
49
            return $storage;
50
        });
51
    }
52
53
    /**
54
     * @return \Closure
55
     */
56
    private function storageRegistrar(): \Closure
57
    {
58
        return static function (StorageConfig $config, UriParserInterface $parser) {
59
            return new Storage($config, $parser);
60
        };
61
    }
62
63
    /**
64
     * @return \Closure
65
     */
66
    private function uriResolverRegistrar(): \Closure
67
    {
68
        return static function (StorageConfig $config, UriParserInterface $parser) {
69
            return new UriResolver($config, $parser);
70
        };
71
    }
72
}
73