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

ReadableTrait::getVisibility()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
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 League\Flysystem\FilesystemException;
16
use League\Flysystem\FilesystemOperator;
17
use Spiral\Storage\Exception\FileOperationException;
18
use Spiral\Storage\Visibility;
19
20
/**
21
 * @mixin ReadableInterface
22
 */
23
trait ReadableTrait
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function exists(string $pathname): bool
29
    {
30
        $fs = $this->getOperator();
31
32
        try {
33
            return $fs->fileExists($pathname);
34
        } catch (FilesystemException $e) {
35
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
36
        }
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function getContents(string $pathname): string
43
    {
44
        $fs = $this->getOperator();
45
46
        try {
47
            return $fs->read($pathname);
48
        } catch (FilesystemException $e) {
49
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
50
        }
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function getStream(string $pathname)
57
    {
58
        $fs = $this->getOperator();
59
60
        try {
61
            return $fs->readStream($pathname);
62
        } catch (FilesystemException $e) {
63
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
64
        }
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getLastModified(string $pathname): int
71
    {
72
        $fs = $this->getOperator();
73
74
        try {
75
            return $fs->lastModified($pathname);
76
        } catch (FilesystemException $e) {
77
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
78
        }
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getSize(string $pathname): int
85
    {
86
        $fs = $this->getOperator();
87
88
        try {
89
            return $fs->fileSize($pathname);
90
        } catch (FilesystemException $e) {
91
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
92
        }
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function getMimeType(string $pathname): string
99
    {
100
        $fs = $this->getOperator();
101
102
        try {
103
            return $fs->mimeType($pathname);
104
        } catch (FilesystemException $e) {
105
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
106
        }
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    #[ExpectedValues(valuesFromClass: Visibility::class)]
113
    public function getVisibility(string $pathname): string
114
    {
115
        $fs = $this->getOperator();
116
117
        try {
118
            return $this->fromFlysystemVisibility(
119
                $fs->visibility($pathname)
120
            );
121
        } catch (FilesystemException $e) {
122
            throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e);
123
        }
124
    }
125
    /**
126
     * @return FilesystemOperator
127
     */
128
    abstract protected function getOperator(): FilesystemOperator;
129
130
    /**
131
     * @param string $visibility
132
     * @return string
133
     */
134
    #[ExpectedValues(valuesFromClass: Visibility::class)]
135
    private function fromFlysystemVisibility(
136
        #[ExpectedValues(valuesFromClass: \League\Flysystem\Visibility::class)]
137
        string $visibility
138
    ): string {
139
        return $visibility === \League\Flysystem\Visibility::PUBLIC
140
            ? Visibility::VISIBILITY_PUBLIC
141
            : Visibility::VISIBILITY_PRIVATE;
142
    }
143
}
144