Passed
Pull Request — master (#407)
by Kirill
07:03
created

WritableTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 9 2
A move() 0 9 2
A delete() 0 7 1
A write() 0 7 1
A create() 0 7 1
A setVisibility() 0 10 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\Storage;
13
14
use JetBrains\PhpStorm\ExpectedValues;
15
use Spiral\Storage\FileInterface;
16
use Spiral\Storage\BucketInterface;
17
use Spiral\Storage\Visibility;
18
19
/**
20
 * @mixin WritableInterface
21
 */
22
trait WritableTrait
23
{
24
    /**
25
     * {@see StorageInterface::bucket()}
26
     */
27
    abstract public function bucket(string $name = null): BucketInterface;
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function create($id, array $config = []): FileInterface
33
    {
34
        [$name, $pathname] = $this->parseUri($id);
35
36
        $bucket = $this->bucket($name);
37
38
        return $bucket->create($pathname, $config);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function write($id, $content, array $config = []): FileInterface
45
    {
46
        [$name, $pathname] = $this->parseUri($id);
47
48
        $bucket = $this->bucket($name);
49
50
        return $bucket->write($pathname, $content, $config);
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function setVisibility(
57
        $id,
58
        #[ExpectedValues(valuesFromClass: Visibility::class)]
59
        string $visibility
60
    ): FileInterface {
61
        [$name, $pathname] = $this->parseUri($id);
62
63
        $bucket = $this->bucket($name);
64
65
        return $bucket->setVisibility($pathname, $visibility);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function copy($source, $destination, array $config = []): FileInterface
72
    {
73
        [$sourceName, $sourcePathname] = $this->parseUri($source);
74
        [$destName, $destPathname] = $this->parseUri($destination, false);
75
76
        $sourceStorage = $this->bucket($sourceName);
77
        $destStorage = $destName ? $this->bucket($destName) : null;
78
79
        return $sourceStorage->copy($sourcePathname, $destPathname, $destStorage, $config);
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function move($source, $destination, array $config = []): FileInterface
86
    {
87
        [$sourceName, $sourcePathname] = $this->parseUri($source);
88
        [$destName, $destPathname] = $this->parseUri($destination, false);
89
90
        $sourceStorage = $this->bucket($sourceName);
91
        $destStorage = $destName ? $this->bucket($destName) : null;
92
93
        return $sourceStorage->move($sourcePathname, $destPathname, $destStorage, $config);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function delete($id, bool $clean = false): void
100
    {
101
        [$name, $pathname] = $this->parseUri($id);
102
103
        $bucket = $this->bucket($name);
104
105
        $bucket->delete($pathname, $clean);
106
    }
107
108
    /**
109
     * {@see Storage::parseUri()}
110
     */
111
    abstract protected function parseUri($uri, bool $withScheme = true): array;
112
}
113