Flysystem::getMediaType()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\FileWaiter\Adapter;
6
7
use League\Flysystem\FileNotFoundException;
8
use League\Flysystem\FilesystemInterface;
9
use Psr\Http\Message\StreamFactoryInterface;
10
use Psr\Http\Message\StreamInterface;
11
use Stadly\FileWaiter\Adapter;
12
use Stadly\FileWaiter\Exception\FileCouldNotBeFound;
13
use Stadly\FileWaiter\Exception\StreamCouldNotBeOpened;
14
use Stadly\Http\Header\Value\Date;
15
use Stadly\Http\Header\Value\EntityTag\EntityTag;
16
use Stadly\Http\Header\Value\MediaType\MediaType;
17
18
/**
19
 * Adapter for handling files stored in the abstract file system Flysystem: http://flysystem.thephpleague.com
20
 */
21
final class Flysystem implements Adapter
22
{
23
    /**
24
     * @var FilesystemInterface File system where the file is stored.
25
     */
26
    private $fileSystem;
27
28
    /**
29
     * @var string Path to the file in the file system.
30
     */
31
    private $filePath;
32
33
    /**
34
     * @var StreamFactoryInterface Factory for creating streams.
35
     */
36
    private $streamFactory;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param FilesystemInterface $fileSystem File system where the file is stored.
42
     * @param string $filePath Path to the file in the file system.
43
     * @param StreamFactoryInterface $streamFactory Factory for creating streams.
44
     * @throws FileCouldNotBeFound If the file could not be found.
45
     */
46 3
    public function __construct(
47
        FilesystemInterface $fileSystem,
48
        string $filePath,
49
        StreamFactoryInterface $streamFactory
50
    ) {
51 3
        if (!$fileSystem->has($filePath)) {
52 1
            throw new FileCouldNotBeFound($filePath);
53
        }
54
55
        try {
56 2
            $metadata = $fileSystem->getMetadata($filePath);
57
        // @codeCoverageIgnoreStart
58
        } catch (FileNotFoundException $exception) {
59
            throw new FileCouldNotBeFound($filePath, $exception);
60
        }
61
        // @codeCoverageIgnoreEnd
62
63 2
        if ($metadata === false || $metadata['type'] !== 'file') {
64 1
            throw new FileCouldNotBeFound($filePath);
65
        }
66
67 1
        $this->fileSystem = $fileSystem;
68 1
        $this->filePath = $filePath;
69 1
        $this->streamFactory = $streamFactory;
70 1
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 2
    public function getFileStream(): StreamInterface
76
    {
77
        try {
78 2
            $fileStream = $this->fileSystem->readStream($this->filePath);
79 1
        } catch (FileNotFoundException $exception) {
80 1
            throw new StreamCouldNotBeOpened($this->filePath, $exception);
81
        }
82
83 1
        if ($fileStream === false) {
84
            // @codeCoverageIgnoreStart
85
            throw new StreamCouldNotBeOpened($this->filePath);
86
            // @codeCoverageIgnoreEnd
87
        }
88
89 1
        return $this->streamFactory->createStreamFromResource($fileStream);
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 3
    public function getFileName(): string
96
    {
97 3
        return basename($this->filePath);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 3
    public function getFileSize(): ?int
104
    {
105
        try {
106 3
            $fileSize = $this->fileSystem->getSize($this->filePath);
107 1
        } catch (FileNotFoundException $exception) {
108 1
            return null;
109
        }
110
111 2
        if ($fileSize === false) {
112 1
            return null;
113
        }
114
115 1
        return $fileSize;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121 3
    public function getMediaType(): ?MediaType
122
    {
123
        try {
124 3
            $mediaTypeString = $this->fileSystem->getMimetype($this->filePath);
125 1
        } catch (FileNotFoundException $exception) {
126 1
            return null;
127
        }
128
129 2
        if ($mediaTypeString === false || $mediaTypeString === 'directory') {
130 1
            return null;
131
        }
132
133 1
        return MediaType::fromString($mediaTypeString);
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 3
    public function getLastModifiedDate(): ?Date
140
    {
141
        try {
142 3
            $timestamp = $this->fileSystem->getTimestamp($this->filePath);
143 1
        } catch (FileNotFoundException $exception) {
144 1
            return null;
145
        }
146
147 2
        if ($timestamp === false) {
148
            // @codeCoverageIgnoreStart
149
            return null;
150
            // @codeCoverageIgnoreEnd
151
        }
152
153 2
        return Date::fromTimestamp($timestamp);
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 2
    public function getEntityTag(): ?EntityTag
160
    {
161
        try {
162 2
            $content = $this->fileSystem->read($this->filePath);
163 1
        } catch (FileNotFoundException $exception) {
164 1
            return null;
165
        }
166
167 1
        if ($content === false) {
168
            // @codeCoverageIgnoreStart
169
            return null;
170
            // @codeCoverageIgnoreEnd
171
        }
172
173 1
        return new EntityTag(md5($content));
174
    }
175
}
176