GroupedTemplate   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resetItems() 0 4 1
A selectTemplate() 0 7 2
A loadTemplate() 0 6 2
1
<?php
2
3
namespace kalanis\kw_templates;
4
5
6
/**
7
 * Class GroupedTemplate
8
 * @package kalanis\kw_templates
9
 * Load external source as template
10
 */
11
abstract class GroupedTemplate extends ATemplate
12
{
13
    /** @var array<string, string> */
14
    protected static array $knownTemplates;
15
16 2
    protected function loadTemplate(): string
17
    {
18 2
        if (empty(static::$knownTemplates)) {
19 1
            static::$knownTemplates = $this->defineAvailableTemplates();
20
        }
21 2
        return '';
22
    }
23
24
    /**
25
     * Define templates available from this class
26
     * array key is to select one, value is for content
27
     * @return array<string, string>
28
     */
29
    abstract protected function defineAvailableTemplates(): array;
30
31 2
    protected function resetItems(): self
32
    {
33 2
        $this->items = [];
34 2
        return $this;
35
    }
36
37
    /**
38
     * @param string $key
39
     * @throws TemplateException
40
     * @return $this
41
     * Call only from method in extending class and be prepared for resetting items due unavailability some of them
42
     */
43 2
    protected function selectTemplate(string $key): self
44
    {
45 2
        if (!isset(static::$knownTemplates[$key])) {
46 1
            throw new TemplateException(sprintf('Unknown template %s from group %s', $key, get_class($this)));
47
        }
48 1
        $this->setTemplate(static::$knownTemplates[$key]);
49 1
        return $this;
50
    }
51
}
52