Passed
Push — main ( 415e55...cf1b5a )
by Marc
03:47
created

Config::checkSetOrDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
namespace html_go\model;
3
4
use InvalidArgumentException;
5
use html_go\exceptions\InternalException;
6
7
final class Config extends AdminConfig
8
{
9
    public const KEY_SITE_URL = 'site.url';
10
    public const KEY_SITE_NAME = 'site.name';
11
    public const KEY_SITE_TITLE = 'site.title';
12
    public const KEY_SITE_DESCRIPTION = 'site.description';
13
    public const KEY_SITE_TAGLINE = 'site.tagline';
14
    public const KEY_SITE_COPYRIGHT = 'site.copyright';
15
    public const KEY_LANG = 'site.language';
16
    public const KEY_TPL_ENGINE = 'template.engine';
17
    public const KEY_TPL_CACHING = 'template.engine.caching';
18
    public const KEY_TPL_FILE_EXT = 'template.engine.file.ext';
19
    public const KEY_TPL_STRICT_VARS_TWIG = 'template.engine.twig.strict_variables';
20
    public const KEY_STATIC_INDEX = 'static.index';
21
    public const KEY_THEME_NAME = 'theme.name';
22
    public const KEY_POSTS_PERPAGE = 'blog.posts_per_page';
23
    public const KEY_POST_DATE_FMT = 'blog.post_date_format';
24
25
    /**
26
     * Config constructor.
27
     * @param string $configRoot The root directory containing the 'config.ini' file.
28
     * @throws InvalidArgumentException
29
     * @throws InternalException
30
     */
31
    public function __construct(string $configRoot) {
32
        parent::__construct($configRoot);
33
        $this->config = $this->validateConfig($this->config);
34
    }
35
36
    public function getString(string $key, string $default = ''): string {
37
        $var = $this->checkAndGet($key);
38
        if (empty($var) || \is_string($var) === false) {
39
            return $default;
40
        }
41
        return $var;
42
    }
43
44
    public function getInt(string $key, int $default = -1): int {
45
        $var = $this->checkAndGet($key);
46
        if (empty($var) || \is_int($var) === false) {
47
            return $default;
48
        }
49
        return $var;
50
    }
51
52
    public function getBool(string $key, bool $default = false): bool {
53
        $var = $this->checkAndGet($key);
54
        if (empty($var) || \is_bool($var) === false) {
55
            return $default;
56
        }
57
        return $var;
58
    }
59
60
    /**
61
     * Check required options are set and set defaults.
62
     * @param array<string, mixed> $config
63
     * @return array<string, mixed>
64
     * @throws InvalidArgumentException
65
     */
66
    private function validateConfig(array $config): array {
67
        if (isset($config[self::KEY_SITE_URL]) === false) {
68
            throw new InvalidArgumentException("Configuration option 'site.url' not set.");
69
        }
70
        $config = $this->checkSetOrDefault($config, self::KEY_SITE_TITLE, ' | HTML-go');
71
        $config = $this->checkSetOrDefault($config, self::KEY_SITE_NAME, 'HTML-go');
72
        $config = $this->checkSetOrDefault($config, self::KEY_SITE_DESCRIPTION, 'Powered by HTML-go, a databaseless, flat-file blogging platform');
73
        $config = $this->checkSetOrDefault($config, self::KEY_SITE_TAGLINE, 'Another HTML-go website');
74
        $config = $this->checkSetOrDefault($config, self::KEY_SITE_COPYRIGHT, '(c) Copyright, Your Name');
75
        $config = $this->checkSetOrDefault($config, self::KEY_LANG, 'en');
76
        $config = $this->checkSetOrDefault($config, self::KEY_TPL_ENGINE, 'twig');
77
        $config = $this->checkSetOrDefault($config, self::KEY_TPL_CACHING, false);
78
        $config = $this->checkSetOrDefault($config, self::KEY_TPL_FILE_EXT, 'twig');
79
        $config = $this->checkSetOrDefault($config, self::KEY_TPL_STRICT_VARS_TWIG, true);
80
        $config = $this->checkSetOrDefault($config, self::KEY_THEME_NAME, 'default');
81
        $config = $this->checkSetOrDefault($config, self::KEY_STATIC_INDEX, true);
82
        $config = $this->checkSetOrDefault($config, self::KEY_POSTS_PERPAGE, 5);
83
        $config = $this->checkSetOrDefault($config, self::KEY_POST_DATE_FMT, 'F d, Y');
84
85
        return $config;
86
    }
87
}
88