FileResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
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