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

Bucket   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromAdapter() 0 8 1
A __construct() 0 5 1
A getOperator() 0 3 1
A getUriResolver() 0 3 1
A file() 0 3 1
A withUriResolver() 0 6 1
A getName() 0 3 1
A withName() 0 6 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\UriResolverInterface;
18
use Spiral\Storage\Bucket\ReadableTrait;
19
use Spiral\Storage\Bucket\UriResolvableInterface;
20
use Spiral\Storage\Bucket\WritableTrait;
21
22
class Bucket implements BucketInterface
23
{
24
    use ReadableTrait;
25
    use WritableTrait;
26
27
    /**
28
     * @var FilesystemOperator
29
     */
30
    protected $fs;
31
32
    /**
33
     * @var UriResolverInterface|null
34
     */
35
    protected $resolver;
36
37
    /**
38
     * @var string|null
39
     */
40
    protected $name;
41
42
    /**
43
     * @param FilesystemOperator $fs
44
     * @param string|null $name
45
     * @param UriResolverInterface|null $resolver
46
     */
47
    public function __construct(FilesystemOperator $fs, string $name = null, UriResolverInterface $resolver = null)
48
    {
49
        $this->fs = $fs;
50
        $this->resolver = $resolver;
51
        $this->name = $name;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function getName(): ?string
58
    {
59
        return $this->name;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function withName(?string $name): BucketInterface
66
    {
67
        $self = clone $this;
68
        $self->name = $name;
69
70
        return $self;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function getUriResolver(): ?UriResolverInterface
77
    {
78
        return $this->resolver;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function withUriResolver(?UriResolverInterface $resolver): UriResolvableInterface
85
    {
86
        $self = clone $this;
87
        $self->resolver = $resolver;
88
89
        return $self;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function file(string $pathname): FileInterface
96
    {
97
        return new File($this, $pathname, $this->resolver);
98
    }
99
100
    /**
101
     * @param FilesystemAdapter $adapter
102
     * @param string|null $name
103
     * @param UriResolverInterface|null $resolver
104
     * @return static
105
     */
106
    public static function fromAdapter(
107
        FilesystemAdapter $adapter,
108
        string $name = null,
109
        UriResolverInterface $resolver = null
110
    ): self {
111
        $fs = new Filesystem($adapter);
112
113
        return new self($fs, $name, $resolver);
114
    }
115
116
    /**
117
     * @return FilesystemOperator
118
     */
119
    protected function getOperator(): FilesystemOperator
120
    {
121
        return $this->fs;
122
    }
123
}
124