FileResolver::getPaths()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 5
nop 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Wandu\View\Phiew;
3
4
use Wandu\View\FileNotFoundException;
5
use Wandu\View\Phiew\Contracts\ResolverInterface;
6
use SplFileInfo;
7
use SplFileObject;
8
9
class FileResolver implements ResolverInterface 
10
{
11
    /** @var \Wandu\View\Phiew\Configuration */
12
    protected $config;
13
14 6
    public function __construct(Configuration $config)
15
    {
16 6
        $this->config = $config;
17 6
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 6
    public function resolve(string $name): Template
23
    {
24
        /** @var \SplFileInfo $path */
25 6
        foreach ($this->getPaths() as $path) {
26 6
            if (file_exists($templateFilePath = $path->getRealPath() . "/{$name}")) {
27 6
                return new Template(new SplFileObject($templateFilePath), $this);
28
            }
29
        }
30 2
        throw new FileNotFoundException("Cannot find the template file named '{$name}'.");
31
    }
32
33
    /**
34
     * @return \Generator
35
     */
36 6
    private function getPaths()
37
    {
38 6
        foreach ((array)($this->config->path) as $path) {
39 6
            if (is_string($path)) {
40 6
                $path = new SplFileInfo($path);
41
            }
42 6
            if (!$path instanceof SplFileInfo || !$path->isDir()) {
43
                continue;
44
            }
45 6
            yield $path;
46
        }
47 2
    }
48
}
49