Completed
Pull Request — master (#1217)
by
unknown
01:31
created

FlysystemLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 5
A find() 0 16 2
A getExtension() 0 12 3
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\FilesystemInterface;
15
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
16
use Liip\ImagineBundle\Exception\InvalidArgumentException;
17
use Liip\ImagineBundle\Model\Binary;
18
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
19
use Symfony\Component\Mime\MimeTypesInterface;
20
21
class FlysystemLoader implements LoaderInterface
22
{
23
    /**
24
     * @var FilesystemInterface
25
     */
26
    protected $filesystem;
27
28
    /**
29
     * @var MimeTypesInterface|DeprecatedExtensionGuesserInterface
30
     */
31
    protected $extensionGuesser;
32
33
    public function __construct(
34
        $extensionGuesser,
35
        FilesystemInterface $filesystem)
36
    {
37
        if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
38
            throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface');
39
        }
40
41
        if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
42
            @trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.', DeprecatedExtensionGuesserInterface::class, __METHOD__, MimeTypesInterface::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
        }
44
45
        $this->extensionGuesser = $extensionGuesser;
46
        $this->filesystem = $filesystem;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function find($path)
53
    {
54
        if (false === $this->filesystem->has($path)) {
55
            throw new NotLoadableException(sprintf('Source image "%s" not found.', $path));
56
        }
57
58
        $mimeType = $this->filesystem->getMimetype($path);
59
60
        $extension = $this->getExtension($mimeType);
61
62
        return new Binary(
63
            $this->filesystem->read($path),
0 ignored issues
show
Security Bug introduced by
It seems like $this->filesystem->read($path) targeting League\Flysystem\FilesystemInterface::read() can also be of type false; however, Liip\ImagineBundle\Model\Binary::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
64
            $mimeType,
0 ignored issues
show
Security Bug introduced by
It seems like $mimeType defined by $this->filesystem->getMimetype($path) on line 58 can also be of type false; however, Liip\ImagineBundle\Model\Binary::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
65
            $extension
66
        );
67
    }
68
69
    private function getExtension(?string $mimeType): ?string
70
    {
71
        if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
72
            return $this->extensionGuesser->guess($mimeType);
73
        }
74
75
        if (null === $mimeType) {
76
            return null;
77
        }
78
79
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
80
    }
81
}
82