Completed
Pull Request — master (#732)
by 12345
03:41
created

FlysystemLoader::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Liip\ImagineBundle\Binary\Loader;
4
5
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
6
use Liip\ImagineBundle\Model\Binary;
7
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
8
use League\Flysystem\FilesystemInterface;
9
10
class FlysystemLoader implements LoaderInterface
11
{
12
    /**
13
     * @var FilesystemInterface
14
     */
15
    protected $filesystem;
16
17
    /**
18
     * @var ExtensionGuesserInterface
19
     */
20
    protected $extensionGuesser;
21
22
    public function __construct(
23
        ExtensionGuesserInterface $extensionGuesser,
24
        FilesystemInterface $filesystem)
25
    {
26
        $this->extensionGuesser = $extensionGuesser;
27
        $this->filesystem = $filesystem;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function find($path)
34
    {
35
        if ($this->filesystem->has($path) === false) {
36
            throw new NotLoadableException(sprintf('Source image "%s" not found.', $path));
37
        }
38
39
        $mimeType = $this->filesystem->getMimetype($path);
40
41
        return new Binary(
42
            $this->filesystem->read($path),
43
            $mimeType,
44
            $this->extensionGuesser->guess($mimeType)
45
        );
46
    }
47
}
48