FileSystemLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-svg-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension\Svg\Provider\FileSystem;
13
14
use Ocubom\Twig\Extension\Svg\Exception\LoaderException;
15
use Ocubom\Twig\Extension\Svg\Loader\LoaderInterface;
16
use Ocubom\Twig\Extension\Svg\Svg;
17
use Ocubom\Twig\Extension\Svg\Util\PathCollection;
18
use Symfony\Component\Filesystem\Path;
19
20
class FileSystemLoader implements LoaderInterface
21
{
22
    private PathCollection $searchPath;
23
24 20
    public function __construct(PathCollection $searchPath)
25
    {
26 20
        $this->searchPath = $searchPath;
27
    }
28
29 5
    public function resolve(string $ident, iterable $options = null): Svg
30
    {
31 5
        return new Svg($this->findPath($ident), $options);
32
    }
33
34 8
    protected function findPath(string $ident): \SplFileInfo
35
    {
36 8
        foreach ($this->searchPath as $basePath) {
37
            /** @var \SplFileInfo|null $basePath */
38 8
            $fullPath = Path::join((string) ($basePath ?? ''), $ident);
39 8
            if (!Path::hasExtension($fullPath, 'svg')) {
40 6
                $fullPath .= '.svg'; // Add svg extension
41
            }
42
43 8
            $realPath = realpath($fullPath);
44 8
            if ($realPath && is_file($realPath)) {
45 5
                return new \SplFileInfo($fullPath);
46
            }
47
        }
48
49 4
        throw new LoaderException($ident, new \ReflectionClass(__CLASS__), (string) $this->searchPath);
50
    }
51
}
52