TemplateFactory::getTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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
use SmartWeb\ModuleTesting\Generator\Test\CodeceptionTestType;
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
    
31
    /**
32
     * @var string
33
     */
34
    protected $templatesPath;
35
    
36
    /**
37
     * TemplateFactory constructor.
38
     *
39
     * @param string $templatesPath
40
     */
41
    public function __construct(string $templatesPath = self::DEFAULT_TEMPLATE_DIR)
42
    {
43
        $this->templatesPath = $templatesPath;
44
    }
45
    
46
    /**
47
     * @inheritDoc
48
     */
49
    public static function registerTemplate(CodeceptionTestType $type, string $suite, string $path)
50
    {
51
        self::validateTemplatePath($path);
52
        self::$templates[$type->key()][$suite] = $path;
53
    }
54
    
55
    /**
56
     * @param string $path
57
     *
58
     * @throws TemplateNotFound
59
     */
60
    protected static function validateTemplatePath(string $path)
61
    {
62
        if (!File::exists($path)) {
63
            throw new TemplateNotFound($path);
64
        }
65
    }
66
    
67
    /**
68
     * @param string $path
69
     *
70
     * @return TemplateInterface
71
     * @throws FileNotFoundException
72
     */
73
    public function getTemplate(string $path) : TemplateInterface
74
    {
75
        return new Template($this->resolveTemplate($path));
76
    }
77
    
78
    /**
79
     * @param string $path
80
     *
81
     * @return string
82
     * @throws FileNotFoundException
83
     */
84
    private function resolveTemplate(string $path) : string
85
    {
86
        $path = $this->resolveTemplatePath($path);
87
        self::validateTemplatePath($path);
88
        
89
        return static::$cachedTemplates[$path] = static::$cachedTemplates[$path] ?? File::get($path);
90
    }
91
    
92
    /**
93
     * @param string $path
94
     *
95
     * @return string
96
     */
97
    protected function resolveTemplatePath(string $path) : string
98
    {
99
        return Arr::get(static::$templates, $path, $this->templatesPath . $this->prepareTemplatePath($path));
100
    }
101
    
102
    /**
103
     * @param string $path
104
     *
105
     * @return string
106
     */
107
    protected function prepareTemplatePath(string $path) : string
108
    {
109
        return Str::endsWith($path, '.' . self::TEMPLATE_FILE_EXT)
110
            ? $path
111
            : $path . '.' . self::TEMPLATE_FILE_EXT;
112
    }
113
    
114
}
115