Completed
Pull Request — 2.x (#1349)
by Bojidar
12:11 queued 08:28
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\FilesystemException;
15
use League\Flysystem\FilesystemOperator;
16
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
17
use Liip\ImagineBundle\Model\Binary;
18
use Symfony\Component\Mime\MimeTypesInterface;
19
20
class FlysystemV2Loader implements LoaderInterface
21
{
22
    /**
23
     * @var FilesystemOperator
24
     */
25
    protected $filesystem;
26
27
    /**
28
     * @var MimeTypesInterface
29
     */
30
    protected $extensionGuesser;
31
32
    public function __construct(
33
        MimeTypesInterface $extensionGuesser,
34
        FilesystemOperator $filesystem
35
    ) {
36
        $this->extensionGuesser = $extensionGuesser;
37
        $this->filesystem = $filesystem;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function find($path)
44
    {
45
        try {
46
            $mimeType = $this->filesystem->mimeType($path);
47
48
            $extension = $this->getExtension($mimeType);
49
50
            return new Binary(
51
                $this->filesystem->read($path),
52
                $mimeType,
53
                $extension
54
            );
55
        } catch (FilesystemException $exception) {
0 ignored issues
show
Bug introduced by
The class League\Flysystem\FilesystemException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
56
            throw new NotLoadableException(sprintf('Source image "%s" not found.', $path), null, $exception);
57
        }
58
    }
59
60
    private function getExtension(?string $mimeType): ?string
61
    {
62
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
63
    }
64
}
65