Passed
Push — develop ( 683c8d...07cc15 )
by Daniel
05:33
created

FileSystemLoader::getImaginePath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
crap 4.3731
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Imagine;
4
5
use Liip\ImagineBundle\Binary\Loader\FileSystemLoader as BaseFileSystemLoader;
6
use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
7
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
8
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
9
10
class FileSystemLoader extends BaseFileSystemLoader
11
{
12
    /**
13
     * @var string[]
14
     */
15
    private $rootPaths;
16
17
    public function __construct(
18
        MimeTypeGuesserInterface $mimeGuesser,
19
        ExtensionGuesserInterface $extensionGuesser,
20
        LocatorInterface $locator,
21
        array $rootPaths = []
22
    )
23
    {
24
        parent::__construct($mimeGuesser, $extensionGuesser, $locator, $rootPaths);
25
        $this->rootPaths = $rootPaths;
26
    }
27
28
    /**
29
     * Strips root directory from the start of a file path
30
     * @param null|string $filePath
31
     * @return null|string
32
     */
33 1
    public function getImaginePath(?string $filePath = null): ?string
34
    {
35 1
        if (!$filePath) {
36
            return $filePath;
37
        }
38 1
        foreach ($this->rootPaths as $rootPath) {
39 1
            if (strpos($filePath, $rootPath) === 0) {
40 1
                return substr($filePath, \strlen($rootPath));
41
            }
42
        }
43
        return $filePath;
44
    }
45
}
46