PugConfig   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
c 1
b 0
f 1
dl 0
loc 86
ccs 24
cts 24
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A templates() 0 3 1
A jsonSerialize() 0 3 1
A get() 0 10 2
A __construct() 0 4 1
A createFromAntidotConfig() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Render\Phug\Container\Config;
6
7
use InvalidArgumentException;
8
use JsonSerializable;
9
use function array_key_exists;
10
use function array_merge;
11
use function array_replace_recursive;
12
use function sprintf;
13
14
class PugConfig implements JsonSerializable
15
{
16
    public const DEFAULT_PUG_CONFIG = [
17
        'pretty' => true,
18
        'expressionLanguage' => 'js',
19
        'pugjs' => false,
20
        'localsJsonFile' => false,
21
        'cache' => 'var/cache/pug',
22
        'template_path' => 'templates/',
23
        'globals' => [],
24
        'filters' => [],
25
        'keywords' => [],
26
        'helpers' => [],
27
        'default_params' => [],
28
    ];
29
    public const DEFAULT_TEMPLATE_CONFIG = [
30
        'extension' => 'pug',
31
    ];
32
    /** @var array<mixed>  */
33
    private $config;
34
    /** @var array<string>  */
35
    private $templates;
36
37
    /**
38
     * @param array<mixed> $config
39
     * @param array<string> $templates
40
     */
41 6
    private function __construct(array $config, array $templates)
42
    {
43 6
        $this->config = $config;
44 6
        $this->templates = $templates;
45 6
    }
46
47
    /**
48
     * @param array<mixed> $config
49
     */
50 6
    public static function createFromAntidotConfig(array $config): self
51
    {
52 6
        $templates = self::DEFAULT_TEMPLATE_CONFIG;
53 6
        $pugConfig = self::DEFAULT_PUG_CONFIG;
54
55 6
        if (array_key_exists('templates', $config)) {
56 2
            $templates = array_merge($templates, $config['templates']);
57
        }
58
59 6
        if (array_key_exists('pug', $config)) {
60 2
            $pugConfig = $config['pug'];
61
        }
62
63 6
        if (array_key_exists('templating', $config)) {
64 1
            $pugConfig = array_replace_recursive($pugConfig, $config['templating']);
65
        }
66
67 6
        return new self($pugConfig, $templates);
68
    }
69
70
    /**
71
     * @param string $key
72
     * @return mixed
73
     */
74 6
    public function get(string $key)
75
    {
76 6
        if (false === array_key_exists($key, $this->config)) {
77 1
            throw new InvalidArgumentException(sprintf(
78 1
                'Missing key "%s" in pug template config',
79 1
                $key
80
            ));
81
        }
82
83 5
        return $this->config[$key];
84
    }
85
86
    /**
87
     * @return array<mixed>
88
     */
89 4
    public function templates(): array
90
    {
91 4
        return $this->templates;
92
    }
93
94
    /**
95
     * @return array<mixed>
96
     */
97 1
    public function jsonSerialize(): array
98
    {
99 1
        return $this->config;
100
    }
101
}
102