Passed
Push — master ( bac7ba...880767 )
by Koldo
03:07
created

PugConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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  */
33
    private $config;
34
    /** @var array  */
35
    private $templates;
36
37 4
    private function __construct(array $config, array $templates)
38
    {
39 4
        $this->config = $config;
40 4
        $this->templates = $templates;
41 4
    }
42
43 4
    public static function createFromAntidotConfig(array $config): self
44
    {
45 4
        $templates = self::DEFAULT_TEMPLATE_CONFIG;
46 4
        $pugConfig = self::DEFAULT_PUG_CONFIG;
47
48 4
        if (array_key_exists('templates', $config)) {
49
            $templates = array_merge($templates, $config['templates']);
50
        }
51
52 4
        if (array_key_exists('pug', $config)) {
53
            $pugConfig = $config['pug'];
54
        }
55
56 4
        if (array_key_exists('templating', $config)) {
57
            $pugConfig = array_replace_recursive($pugConfig, $config['templating']);
58
        }
59
60 4
        return new self($pugConfig, $templates);
61
    }
62
63 4
    public function get(string $key)
64
    {
65 4
        if (false === array_key_exists($key, $this->config)) {
66 1
            throw new InvalidArgumentException(sprintf(
67 1
                'Missing key "%s" in pug template config',
68 1
                $key
69
            ));
70
        }
71
72 3
        return $this->config[$key];
73
    }
74
75 2
    public function templates(): array
76
    {
77 2
        return $this->templates;
78
    }
79
80 1
    public function jsonSerialize(): array
81
    {
82 1
        return $this->config;
83
    }
84
}
85