Completed
Pull Request — v3 (#279)
by Gabriel
11:00
created

ThemeResolveTemplatePath   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 23 3
A exists() 0 9 2
1
<?php
2
3
namespace League\Plates\Template\ResolveTemplatePath;
4
5
use League\Plates\Exception\TemplateNotFound;
6
use League\Plates\Template\Name;
7
use League\Plates\Template\ResolveTemplatePath;
8
use League\Plates\Template\Theme;
9
10
final class ThemeResolveTemplatePath implements ResolveTemplatePath
11
{
12
    private $theme;
13
14
    public function __construct(Theme $theme) {
15
        $this->theme = $theme;
16
    }
17
18
    public function __invoke(Name $name): string {
19
        $searchedPaths = [];
20
        foreach ($this->theme->listThemeHierarchy() as $theme) {
21
            $path = $theme->dir() . '/' . $name->getName() . '.' . $name->getEngine()->getFileExtension();
22
            if (is_file($path)) {
23
                return $path;
24
            }
25
            $searchedPaths[] = [$theme->name(), $path];
26
        }
27
28
        throw new TemplateNotFound(
29
            $name->getName(),
30
            array_map(function(array $tup) {
31
                return $tup[1];
32
            }, $searchedPaths),
33
            sprintf('The template "%s" was not found in the following themes: %s',
34
                $name->getName(),
35
                implode(', ', array_map(function(array $tup) {
36
                    return implode(':', $tup);
37
                }, $searchedPaths))
38
            )
39
        );
40
    }
41
42
    public function exists(Name $name): bool
43
    {
44
        try {
45
            $this($name);
46
            return true;
47
        } catch (TemplateNotFound $exception) {
48
            return false;
49
        }
50
    }
51
}
52