Completed
Pull Request — 2.x (#1349)
by Bojidar
01:27
created

FlysystemV2Loader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Binary\Loader;
13
14
use League\Flysystem\FilesystemOperator;
15
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
16
use Liip\ImagineBundle\Model\Binary;
17
use Symfony\Component\Mime\MimeTypesInterface;
18
19
class FlysystemV2Loader implements LoaderInterface
20
{
21
    /**
22
     * @var FilesystemOperator
23
     */
24
    protected $filesystem;
25
26
    /**
27
     * @var MimeTypesInterface
28
     */
29
    protected $extensionGuesser;
30
31
    public function __construct(
32
        MimeTypesInterface $extensionGuesser,
33
        FilesystemOperator $filesystem
34
    ) {
35
        $this->extensionGuesser = $extensionGuesser;
36
        $this->filesystem = $filesystem;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function find($path)
43
    {
44
        if (false === $this->filesystem->fileExists($path)) {
45
            throw new NotLoadableException(sprintf('Source image "%s" not found.', $path));
46
        }
47
48
        $mimeType = $this->filesystem->mimeType($path);
49
50
        $extension = $this->getExtension($mimeType);
51
52
        return new Binary(
53
            $this->filesystem->read($path),
54
            $mimeType,
55
            $extension
56
        );
57
    }
58
59
    private function getExtension(?string $mimeType): ?string
60
    {
61
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
62
    }
63
}
64