Completed
Push — 1.0 ( 657f08...d94044 )
by Rob
09:07
created

FileSystemLocator::generateAbsolutePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\Locator;
13
14
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
15
use Liip\ImagineBundle\Exception\InvalidArgumentException;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
class FileSystemLocator implements LocatorInterface
19
{
20
    /**
21
     * @var string[]
22
     */
23
    protected $roots;
24
25
    /**
26
     * @param array[] $options
27
     */
28
    public function setOptions(array $options = array())
29
    {
30
        $optionsResolver = new OptionsResolver();
31
        $optionsResolver->setDefaults(array('roots' => array()));
32
        $options = $optionsResolver->resolve($options);
33
34
        $this->roots = array_map(array($this, 'sanitizeRootPath'), (array) $options['roots']);
35
    }
36
37
    /**
38
     * @param string $path
39
     *
40
     * @throws NotLoadableException
41
     *
42
     * @return string
43
     */
44
    public function locate($path)
45
    {
46
        foreach ($this->roots as $root) {
47
            if (false !== $absolute = $this->generateAbsolutePath($root, $path)) {
48
                return $this->sanitizeAbsolutePath($absolute);
49
            }
50
        }
51
52
        throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"',
53
            $path, implode(':', $this->roots)));
54
    }
55
56
    /**
57
     * @param string $root
58
     * @param string $path
59
     *
60
     * @return string|false
61
     */
62
    protected function generateAbsolutePath($root, $path)
63
    {
64
        return realpath($root.DIRECTORY_SEPARATOR.$path);
65
    }
66
67
    /**
68
     * @param string $root
69
     *
70
     * @throws InvalidArgumentException
71
     *
72
     * @return string
73
     */
74
    protected function sanitizeRootPath($root)
75
    {
76
        if (!empty($root) && false !== $realRoot = realpath($root)) {
77
            return $realRoot;
78
        }
79
80
        throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root));
81
    }
82
83
    /**
84
     * @param string $path
85
     *
86
     * @throws NotLoadableException
87
     *
88
     * @return string
89
     */
90
    private function sanitizeAbsolutePath($path)
91
    {
92
        foreach ($this->roots as $root) {
93
            if (0 === strpos($path, $root)) {
94
                return $path;
95
            }
96
        }
97
98
        throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"',
99
            $path, implode(':', $this->roots)));
100
    }
101
}
102