Passed
Push — main ( 77e259...371264 )
by Oscar
08:09 queued 06:34
created

Finder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 3 1
A resolve() 0 17 4
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;
13
14
use Ocubom\Twig\Extension\Svg\Exception\FileNotFoundException;
15
16
/**
17
 * Finder.
18
 *
19
 * @author Oscar Cubo Medina <[email protected]>
20
 */
21
class Finder implements FinderInterface
22
{
23
    private array $searchPath;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param mixed ...$searchPath The paths where SVG files are located
29
     */
30 9
    public function __construct(...$searchPath)
31
    {
32 9
        $this->searchPath = $searchPath;
33
    }
34
35 4
    public function resolve(string $ident): \SplFileInfo
36
    {
37 4
        foreach ($this->searchPath as $basePath) {
38 4
            $fullPath = rtrim($basePath, '/\\').'/'.$ident;
39 4
            if ('.svg' !== substr($fullPath, -4)) {
40 2
                $fullPath .= '.svg'; // Add svg extension
41
            }
42
43 4
            if (is_file($fullPath)) {
44 3
                return new \SplFileInfo($fullPath);
45
            }
46
        }
47
48 1
        throw new FileNotFoundException(sprintf(
49 1
            'SVG file for "%s" could not be found on "%s".',
50 1
            $ident,
51 1
            (string) $this,
52 1
        ));
53
    }
54
55 1
    public function __toString(): string
56
    {
57 1
        return implode(':', $this->searchPath);
58
    }
59
}
60