Completed
Push — master ( 0a7932...aee286 )
by Maksim
13s
created

FileSystemLoader::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
3
namespace Liip\ImagineBundle\Binary\Loader;
4
5
use Liip\ImagineBundle\Model\FileBinary;
6
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
7
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
8
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
9
10
class FileSystemLoader implements LoaderInterface
11
{
12
    /**
13
     * @var MimeTypeGuesserInterface
14
     */
15
    protected $mimeTypeGuesser;
16
17
    /**
18
     * @var ExtensionGuesserInterface
19
     */
20
    protected $extensionGuesser;
21
22
    /**
23
     * @var string
24
     */
25
    protected $rootPath;
26
27
    /**
28
     * @param MimeTypeGuesserInterface  $mimeTypeGuesser
29
     * @param ExtensionGuesserInterface $extensionGuesser
30
     * @param string                    $rootPath
31
     */
32
    public function __construct(
33
        MimeTypeGuesserInterface $mimeTypeGuesser,
34
        ExtensionGuesserInterface $extensionGuesser,
35
        $rootPath
36
    ) {
37
        $this->mimeTypeGuesser = $mimeTypeGuesser;
38
        $this->extensionGuesser = $extensionGuesser;
39
40
        $this->rootPath = rtrim($rootPath, '/');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function find($path)
47
    {
48
        if (false !== strpos($path, '../')) {
49
            throw new NotLoadableException(sprintf("Source image was searched with '%s' out side of the defined root path", $path));
50
        }
51
52
        $absolutePath = $this->rootPath.'/'.ltrim($path, '/');
53
54
        if (false == file_exists($absolutePath)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
55
            throw new NotLoadableException(sprintf('Source image not found in "%s"', $absolutePath));
56
        }
57
58
        $mimeType = $this->mimeTypeGuesser->guess($absolutePath);
59
60
        return new FileBinary(
61
            $absolutePath,
62
            $mimeType,
63
            $this->extensionGuesser->guess($mimeType)
64
        );
65
    }
66
}
67