Passed
Pull Request — master (#407)
by Kirill
10:25 queued 04:56
created

StorageBootloader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A boot() 0 36 3
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\Storage;
13
14
use Spiral\Boot\Bootloader\Bootloader;
15
use Spiral\Config\ConfiguratorInterface;
16
use Spiral\Core\Container;
17
use Spiral\Storage\Storage;
18
use Spiral\Storage\StorageInterface;
19
use Spiral\Storage\Bucket;
20
use Spiral\Storage\BucketInterface;
21
use Spiral\Distribution\DistributionInterface as CdnInterface;
22
23
class StorageBootloader extends Bootloader
24
{
25
    /**
26
     * @param Container $app
27
     * @param ConfiguratorInterface $config
28
     */
29
    public function boot(Container $app, ConfiguratorInterface $config): void
30
    {
31
        $config->setDefaults(StorageConfig::CONFIG, [
32
            'default' => Storage::DEFAULT_STORAGE,
33
            'servers' => [],
34
            'buckets' => [],
35
        ]);
36
37
        $app->bindInjector(StorageConfig::class, ConfiguratorInterface::class);
38
39
        $app->bindSingleton(StorageInterface::class, static function (StorageConfig $config, CdnInterface $cdn) {
40
            $manager = new Storage($config->getDefaultBucket());
41
42
            $distributions = $config->getDistributions();
43
44
            foreach ($config->getAdapters() as $name => $adapter) {
45
                $resolver = isset($distributions[$name])
46
                    ? $cdn->resolver($distributions[$name])
47
                    : null;
48
49
                $manager->add($name, Bucket::fromAdapter($adapter, $name, $resolver));
50
            }
51
52
            return $manager;
53
        });
54
55
        $app->bindSingleton(Storage::class, static function (StorageInterface $manager) {
56
            return $manager;
57
        });
58
59
        $app->bindSingleton(BucketInterface::class, static function (StorageInterface $manager) {
60
            return $manager->bucket();
61
        });
62
63
        $app->bindSingleton(Bucket::class, static function (BucketInterface $storage) {
64
            return $storage;
65
        });
66
    }
67
}
68