Passed
Pull Request — master (#407)
by Kirill
05:29
created

AdapterFactory::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Storage\Builder;
13
14
use League\Flysystem\FilesystemAdapter;
15
use Spiral\Storage\Builder\Adapter as AdapterBuilder;
16
use Spiral\Storage\Config\DTO\FileSystemInfo;
17
use Spiral\Storage\Exception\StorageException;
18
19
class AdapterFactory
20
{
21
    /**
22
     * Build filesystem adapter by provided filesystem info
23
     *
24
     * @param FileSystemInfo\FileSystemInfoInterface $info
25
     *
26
     * @return FilesystemAdapter
27
     *
28
     * @throws StorageException
29
     */
30
    public static function build(FileSystemInfo\FileSystemInfoInterface $info): FilesystemAdapter
31
    {
32
        $builder = static::detectAdapterBuilder($info);
33
34
        if ($info->isAdvancedUsage()) {
35
            return $builder->buildAdvanced();
36
        }
37
38
        return $builder->buildSimple();
39
    }
40
41
    /**
42
     * Detect required builder by filesystem info
43
     *
44
     * @param FileSystemInfo\FileSystemInfoInterface $info
45
     *
46
     * @return AdapterBuilder\AdapterBuilderInterface
47
     *
48
     * @throws StorageException
49
     */
50
    private static function detectAdapterBuilder(
51
        FileSystemInfo\FileSystemInfoInterface $info
52
    ): AdapterBuilder\AdapterBuilderInterface {
53
        switch (get_class($info)) {
54
            case FileSystemInfo\LocalInfo::class:
55
                return new AdapterBuilder\LocalBuilder($info);
56
            case FileSystemInfo\Aws\AwsS3Info::class:
57
                return new AdapterBuilder\AwsS3Builder($info);
58
            default:
59
                throw new StorageException(
60
                    \sprintf('Adapter can\'t be built by filesystem info `%s`', $info->getName())
61
                );
62
        }
63
    }
64
}
65