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

FileSystemLoader::getExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 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 Liip\ImagineBundle\Binary\Locator\LocatorInterface;
15
use Liip\ImagineBundle\Model\FileBinary;
16
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
17
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface as DeprecatedMimeTypeGuesserInterface;
18
use Symfony\Component\Mime\MimeTypeGuesserInterface;
19
use Symfony\Component\Mime\MimeTypesInterface;
20
21
class FileSystemLoader implements LoaderInterface
22
{
23
    /**
24
     * @var MimeTypeGuesserInterface|DeprecatedMimeTypeGuesserInterface
25
     */
26
    protected $mimeTypeGuesser;
27
28
    /**
29
     * @var MimeTypesInterface|DeprecatedExtensionGuesserInterface
30
     */
31
    protected $extensionGuesser;
32
33
    /**
34
     * @var LocatorInterface
35
     */
36
    protected $locator;
37
38
    /**
39
     * @param MimeTypeGuesserInterface|DeprecatedMimeTypeGuesserInterface $mimeGuesser
40
     * @param MimeTypesInterface|DeprecatedExtensionGuesserInterface      $extensionGuesser
41
     * @param LocatorInterface                                            $locator
42
     */
43
    public function __construct(
44
        $mimeGuesser,
45
        $extensionGuesser,
46
        LocatorInterface $locator
47
    ) {
48
        if (interface_exists(MimeTypeGuesserInterface::class) && $mimeGuesser instanceof DeprecatedMimeTypeGuesserInterface) {
49
            @trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.', DeprecatedMimeTypeGuesserInterface::class, __METHOD__, MimeTypeGuesserInterface::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...
50
        }
51
52
        if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
53
            @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...
54
        }
55
56
        $this->mimeTypeGuesser = $mimeGuesser;
57
        $this->extensionGuesser = $extensionGuesser;
58
        $this->locator = $locator;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function find($path)
65
    {
66
        $path = $this->locator->locate($path);
67
        $mimeType = $this->mimeTypeGuesser instanceof DeprecatedMimeTypeGuesserInterface ? $this->mimeTypeGuesser->guess($path) : $this->mimeTypeGuesser->guessMimeType($path);
68
        $extension = $this->getExtension($mimeType);
69
70
        return new FileBinary($path, $mimeType, $extension);
71
    }
72
73
    private function getExtension(?string $mimeType): ?string
74
    {
75
        if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
76
            return $this->extensionGuesser->guess($mimeType);
77
        }
78
79
        if (null === $mimeType) {
80
            return null;
81
        }
82
83
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
84
    }
85
}
86