Config::getInt()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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