FileResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 10 3
B getPaths() 0 12 5
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