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