Completed
Push — master ( 5bbe5b...a43b1d )
by Nicolai
26:12 queued 14:22
created

TemplateFactory::validateTemplatePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Template;
5
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\File;
9
use Illuminate\Support\Str;
10
use SmartWeb\ModuleTesting\Codeception\Exceptions\TemplateNotFound;
11
12
13
/**
14
 * Class TemplateFactory
15
 *
16
 * @package SmartWeb\ModuleTesting\Template
17
 */
18
class TemplateFactory implements TemplateFactoryInterface
19
{
20
    
21
    /**
22
     * @var string[]
23
     */
24
    protected static $cachedTemplates = [];
25
    
26
    /**
27
     * @var array
28
     */
29
    protected static $templates = [
30
        'test' => [
31
            'unit' => self::DEFAULT_TEMPLATE_DIR . 'test/unit.tpl',
32
        ],
33
        'cest' => [
34
            'integration' => self::DEFAULT_TEMPLATE_DIR . 'cest/integration.tpl',
35
            'graphql'     => self::DEFAULT_TEMPLATE_DIR . 'cest/graphql.tpl',
36
        ],
37
    ];
38
    
39
    /**
40
     * @var string
41
     */
42
    protected $templatesPath;
43
    
44
    /**
45
     * TemplateFactory constructor.
46
     *
47
     * @param string $templatesPath
48
     */
49
    public function __construct(string $templatesPath = self::DEFAULT_TEMPLATE_DIR)
50
    {
51
        $this->templatesPath = $templatesPath;
52
    }
53
    
54
    /**
55
     * @param string $name
56
     * @param string $path
57
     *
58
     * @return bool
59
     * @throws TemplateNotFound
60
     */
61
    public static function registerTemplate(string $name, string $path) : bool
62
    {
63
        self::validateTemplatePath($path);
64
        $before = static::$templates;
65
        Arr::set(static::$templates, $name, $path);
66
        
67
        return ($before == static::$templates);
68
    }
69
    
70
    /**
71
     * @param string $path
72
     *
73
     * @throws TemplateNotFound
74
     */
75
    protected static function validateTemplatePath(string $path)
76
    {
77
        if (!File::exists($path)) {
78
            throw new TemplateNotFound($path);
79
        }
80
    }
81
    
82
    /**
83
     * @param string $path
84
     *
85
     * @return TemplateInterface
86
     * @throws FileNotFoundException
87
     */
88
    public function getTemplate(string $path) : TemplateInterface
89
    {
90
        return new Template($this->resolveTemplate($path));
91
    }
92
    
93
    /**
94
     * @param string $path
95
     *
96
     * @return string
97
     * @throws FileNotFoundException
98
     */
99
    private function resolveTemplate(string $path) : string
100
    {
101
        $path = $this->resolveTemplatePath($path);
102
        self::validateTemplatePath($path);
103
        
104
        return static::$cachedTemplates[$path] = static::$cachedTemplates[$path] ?? File::get($path);
105
    }
106
    
107
    /**
108
     * @param string $path
109
     *
110
     * @return string
111
     */
112
    protected function resolveTemplatePath(string $path) : string
113
    {
114
        return Arr::get(static::$templates, $path, $this->templatesPath . $this->prepareTemplatePath($path));
115
    }
116
    
117
    /**
118
     * @param string $path
119
     *
120
     * @return string
121
     */
122
    protected function prepareTemplatePath(string $path) : string
123
    {
124
        return Str::endsWith($path, '.' . self::TEMPLATE_FILE_EXT)
125
            ? $path
126
            : $path . '.' . self::TEMPLATE_FILE_EXT;
127
    }
128
    
129
}
130