Completed
Pull Request — 1.0 (#937)
by
unknown
02:35
created

FileSystemLoader::__construct()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 33
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[]                  $locatorOrDataRoots
42
     * @param LocatorInterface          $locator
0 ignored issues
show
Documentation introduced by
There is no parameter named $locator. Did you maybe mean $locatorOrDataRoots?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
43
     */
44
    public function __construct(
45
        MimeTypeGuesserInterface $mimeGuesser,
46
        ExtensionGuesserInterface $extensionGuesser,
47
        $locatorOrDataRoots
48
        /* LocatorInterface $locator */
49
    ) {
50
        $this->mimeTypeGuesser = $mimeGuesser;
51
        $this->extensionGuesser = $extensionGuesser;
52
53
        if (is_array($locatorOrDataRoots) || is_string($locatorOrDataRoots)) { // pre-1.9.0 behaviour
54
            if (count((array) $locatorOrDataRoots) === 0) {
55
                throw new InvalidArgumentException('One or more data root paths must be specified.');
56
            }
57
58
            if (func_num_args() >= 4) {
59
                if (func_get_arg(3) instanceof LocatorInterface) {
60
                    @trigger_error(
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...
61
                        sprintf(
62
                            'Passing a LocatorInterface as fourth parameter to %s() is deprecated. It needs to be the third parameter. ' .
63
                            'The previous third parameter (data roots) is removed and the data roots must now be passed as a constructor argument ' .
64
                            'to the LocatorInterface passed to this method.',
65
                            __METHOD__
66
                        ),
67
                        E_USER_DEPRECATED
68
                    );
69
70
                    $this->locator = func_get_arg(3);
71
                } else {
72
                    throw new \InvalidArgumentException(sprintf('Unknown call to %s(). Please check the method signature.', __METHOD__));
73
                }
74
            } else {
75
                @trigger_error(
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...
76
                    sprintf(
77
                        'Method %s() will expect the third parameter to be a LocatorInterface in version 2.0. Defining dataroots instead is deprecated since version 1.9.0',
78
                        __METHOD__
79
                    ),
80
                    E_USER_DEPRECATED
81
                );
82
83
                $this->locator = new FileSystemLocator();
84
            }
85
86
            $this->locator->setOptions(array('roots' => (array) $locatorOrDataRoots));
87
        } elseif ($locatorOrDataRoots instanceof LocatorInterface) {
88
            $this->locator = $locatorOrDataRoots;
89
        } else {
90
            throw new \InvalidArgumentException(sprintf('Method %s() expects a LocatorInterface for the third argument.', __METHOD__));
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function find($path)
98
    {
99
        $path = $this->locator->locate($path);
100
        $mime = $this->mimeTypeGuesser->guess($path);
101
102
        return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime));
103
    }
104
}
105