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

Storage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A file() 0 3 1
A getUriResolver() 0 3 1
A withUriResolver() 0 6 1
A getOperator() 0 3 1
A fromAdapter() 0 5 1
A __construct() 0 4 1
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;
13
14
use League\Flysystem\Filesystem;
15
use League\Flysystem\FilesystemAdapter;
16
use League\Flysystem\FilesystemOperator;
17
use Spiral\Distribution\ResolverInterface;
18
use Spiral\Storage\File\EntryInterface;
19
use Spiral\Storage\Storage\ReadableTrait;
20
use Spiral\Storage\Storage\UriResolvableInterface;
21
use Spiral\Storage\Storage\WritableTrait;
22
23
/**
24
 * @internal Storage is an internal library class, please do not use it in your code.
25
 * @psalm-internal Spiral\Storage
26
 */
27
final class Storage implements StorageInterface
28
{
29
    use ReadableTrait;
30
    use WritableTrait;
31
32
    /**
33
     * @var FilesystemOperator
34
     */
35
    private $fs;
36
37
    /**
38
     * @var ResolverInterface|null
39
     */
40
    private $resolver;
41
42
    /**
43
     * @param FilesystemOperator $fs
44
     * @param ResolverInterface|null $resolver
45
     */
46
    public function __construct(FilesystemOperator $fs, ResolverInterface $resolver = null)
47
    {
48
        $this->fs = $fs;
49
        $this->resolver = $resolver;
50
    }
51
52
    /**
53
     * @param FilesystemAdapter $adapter
54
     * @param ResolverInterface|null $resolver
55
     * @return static
56
     */
57
    public static function fromAdapter(FilesystemAdapter $adapter, ResolverInterface $resolver = null): self
58
    {
59
        $fs = new Filesystem($adapter);
60
61
        return new self($fs, $resolver);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getUriResolver(): ?ResolverInterface
68
    {
69
        return $this->resolver;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function withUriResolver(?ResolverInterface $resolver): UriResolvableInterface
76
    {
77
        $self = clone $this;
78
        $self->resolver = $resolver;
79
80
        return $self;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function file(string $pathname): FileInterface
87
    {
88
        return new File($this, $pathname, $this->resolver);
89
    }
90
91
    /**
92
     * @return FilesystemOperator
93
     */
94
    protected function getOperator(): FilesystemOperator
95
    {
96
        return $this->fs;
97
    }
98
}
99