Completed
Push — master ( 06c49a...d653f4 )
by Rob
02:07
created

FileSystemLoader::absolutePathLocate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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\Exception\Binary\Loader\NotLoadableException;
15
use Liip\ImagineBundle\Exception\InvalidArgumentException;
16
use Liip\ImagineBundle\Model\FileBinary;
17
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
18
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
19
20
class FileSystemLoader implements LoaderInterface
21
{
22
    /**
23
     * @var MimeTypeGuesserInterface
24
     */
25
    protected $mimeTypeGuesser;
26
27
    /**
28
     * @var ExtensionGuesserInterface
29
     */
30
    protected $extensionGuesser;
31
32
    /**
33
     * @var string
34
     */
35
    protected $dataRoots;
36
37
    /**
38
     * @param MimeTypeGuesserInterface  $mimeTypeGuesser
39
     * @param ExtensionGuesserInterface $extensionGuesser
40
     * @param string[]                  $dataRoots
41
     */
42
    public function __construct(
43
        MimeTypeGuesserInterface $mimeTypeGuesser,
44
        ExtensionGuesserInterface $extensionGuesser,
45
        $dataRoots
46
    ) {
47
        $this->mimeTypeGuesser = $mimeTypeGuesser;
48
        $this->extensionGuesser = $extensionGuesser;
49
50
        $this->dataRoots = array_map(function ($root) {
0 ignored issues
show
Documentation Bug introduced by
It seems like array_map(function ($roo... }, (array) $dataRoots) of type array is incompatible with the declared type string of property $dataRoots.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
            if (!empty($root) && false !== $realRoot = realpath($root)) {
52
                return $realRoot;
53
            }
54
55
            throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root));
56
        }, (array) $dataRoots);
57
58
        if (count($this->dataRoots) === 0) {
59
            throw new InvalidArgumentException('One or more data root paths must be specified.');
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function find($path)
67
    {
68
        $path = $this->absolutePathRestrict($this->absolutePathLocate($path));
69
        $mime = $this->mimeTypeGuesser->guess($path);
70
71
        return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime));
72
    }
73
74
    /**
75
     * @param string $path
76
     *
77
     * @return string
78
     */
79
    private function absolutePathLocate($path)
80
    {
81
        foreach ($this->dataRoots as $root) {
0 ignored issues
show
Bug introduced by
The expression $this->dataRoots of type string is not traversable.
Loading history...
82
            if (false !== $realPath = realpath($root.DIRECTORY_SEPARATOR.$path)) {
83
                return $realPath;
84
            }
85
        }
86
87
        throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"',
88
            $path, implode(':', $this->dataRoots)));
89
    }
90
91
    /**
92
     * @param string $path
93
     *
94
     * @return mixed
95
     */
96
    private function absolutePathRestrict($path)
97
    {
98
        foreach ($this->dataRoots as $root) {
0 ignored issues
show
Bug introduced by
The expression $this->dataRoots of type string is not traversable.
Loading history...
99
            if (0 === strpos($path, $root)) {
100
                return $path;
101
            }
102
        }
103
104
        throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"',
105
            $path, implode(':', $this->dataRoots)));
106
    }
107
}
108