Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

TemplateFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

7 Methods

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