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

FlysystemV2Loader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A find() 0 16 2
A getExtension() 0 4 1
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