Completed
Pull Request — 1.0 (#963)
by Rob
12:19
created

FileSystemLoader::__construct()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 6
nop 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 Liip\ImagineBundle\Binary\Locator\FileSystemLocator;
15
use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
16
use Liip\ImagineBundle\Exception\InvalidArgumentException;
17
use Liip\ImagineBundle\Model\FileBinary;
18
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
19
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
20
21
class FileSystemLoader implements LoaderInterface
22
{
23
    /**
24
     * @var MimeTypeGuesserInterface
25
     */
26
    protected $mimeTypeGuesser;
27
28
    /**
29
     * @var ExtensionGuesserInterface
30
     */
31
    protected $extensionGuesser;
32
33
    /**
34
     * @var LocatorInterface
35
     */
36
    protected $locator;
37
38
    /**
39
     * @param MimeTypeGuesserInterface  $mimeGuesser
40
     * @param ExtensionGuesserInterface $extensionGuesser
41
     * @param string[]|LocatorInterface $locator
42
     */
43
    public function __construct(MimeTypeGuesserInterface $mimeGuesser, ExtensionGuesserInterface $extensionGuesser, $locator)
44
    {
45
        $this->mimeTypeGuesser = $mimeGuesser;
46
        $this->extensionGuesser = $extensionGuesser;
47
48
        if ($locator instanceof LocatorInterface) { // post-1.9.0 behavior
49
            $this->locator = $locator;
50
        } elseif (is_array($locator) || is_string($locator)) { // pre-1.9.0 behaviour
51
            if (count((array) $locator) === 0) {
52
                throw new InvalidArgumentException('One or more data root paths must be specified.');
53
            }
54
55
            if (func_num_args() >= 4) {
56
                if (func_get_arg(3) instanceof LocatorInterface) {
57
                    @trigger_error(sprintf(
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...
58
                        'Passing a LocatorInterface as fourth parameter to %s() is deprecated. It needs to be the '.
59
                        'third parameter. The previous third parameter (data roots) is removed and the data roots must '.
60
                        'now be passed as a constructor argument to the LocatorInterface passed to this method.', __METHOD__
61
                    ), E_USER_DEPRECATED);
62
63
                    $this->locator = func_get_arg(3);
64
                    $this->locator->setOptions(array('roots' => (array) $locator));
65
                } else {
66
                    throw new \InvalidArgumentException(sprintf(
67
                        'Unknown call to %s(). Please check the method signature.', __METHOD__
68
                    ));
69
                }
70
            } else {
71
                @trigger_error(sprintf(
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...
72
                    'Method %s() will expect the third parameter to be a LocatorInterface in version 2.0. Defining '.
73
                    'data roots instead is deprecated since version 1.9.0', __METHOD__
74
                ), E_USER_DEPRECATED);
75
76
                $this->locator = new FileSystemLocator((array) $locator);
77
            }
78
        } else { // invalid behavior
79
            throw new \InvalidArgumentException(sprintf(
80
                'Method %s() expects a LocatorInterface for the third argument.', __METHOD__
81
            ));
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function find($path)
89
    {
90
        $path = $this->locator->locate($path);
91
        $mime = $this->mimeTypeGuesser->guess($path);
92
93
        return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime));
94
    }
95
}
96