Passed
Pull Request — master (#407)
by Kirill
06:33
created

AwsVisibilityConverter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 8
A getConverter() 0 9 2
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\Config\DTO\FileSystemInfo\Aws;
13
14
use League\Flysystem\Visibility;
15
use Spiral\Storage\Exception\ConfigException;
16
use Spiral\Storage\Config\DTO\FileSystemInfo\ClassBasedInterface;
17
use Spiral\Storage\Config\DTO\FileSystemInfo\OptionsBasedInterface;
18
use Spiral\Storage\Config\DTO\Traits\OptionsTrait;
19
use Spiral\Storage\Exception\StorageException;
20
use Spiral\Storage\Config\DTO\Traits\ClassBasedTrait;
21
22
class AwsVisibilityConverter implements ClassBasedInterface, OptionsBasedInterface
23
{
24
    use ClassBasedTrait;
25
    use OptionsTrait;
26
27
    public const VISIBILITY_KEY = 'visibility';
28
29
    private $converter = null;
30
31
    /**
32
     * @param array $info
33
     *
34
     * @throws StorageException
35
     */
36
    public function __construct(array $info)
37
    {
38
        if (
39
            !array_key_exists(static::CLASS_KEY, $info)
40
            || !is_string($info[static::CLASS_KEY])
41
        ) {
42
            throw new ConfigException('Aws visibility converter must be described with class');
43
        }
44
45
        if (
46
            !array_key_exists(static::OPTIONS_KEY, $info)
47
            || empty($info[static::OPTIONS_KEY])
48
            || !is_array($info[static::OPTIONS_KEY])
49
        ) {
50
            throw new ConfigException('Aws visibility converter must be described with options list');
51
        }
52
53
        $this->setClass($info[static::CLASS_KEY], $info[static::CLASS_KEY]);
54
55
        $this->options = $info[static::OPTIONS_KEY];
56
57
        if (!$this->hasOption(static::VISIBILITY_KEY)) {
58
            throw new ConfigException(
59
                \sprintf('`%s` option should be defined for Aws visibility converter', static::VISIBILITY_KEY)
60
            );
61
        }
62
63
        $allowedVisibilityOptionValues = [Visibility::PUBLIC, Visibility::PRIVATE];
64
        if (!in_array($this->getOption(static::VISIBILITY_KEY), $allowedVisibilityOptionValues, true)) {
65
            throw new ConfigException(
66
                \sprintf(
67
                    '`%s` should be defined with one of values: %s',
68
                    static::VISIBILITY_KEY,
69
                    implode(',', $allowedVisibilityOptionValues)
70
                )
71
            );
72
        }
73
    }
74
75
    /**
76
     * Get aws visibility converter of provided class
77
     *
78
     * @return mixed|null
79
     */
80
    public function getConverter()
81
    {
82
        $class = $this->getClass();
83
84
        if (!$this->converter instanceof $class) {
85
            $this->converter = new $class($this->getOption(static::VISIBILITY_KEY));
86
        }
87
88
        return $this->converter;
89
    }
90
}
91