ConfigHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CSlant\TelegramGitNotifier\Helpers;
4
5
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
6
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
7
use Throwable;
8
9
final class ConfigHelper
10
{
11
    /**
12
     * @var array<string, mixed>
13
     */
14
    public array $config;
15
16
    public function __construct()
17
    {
18
        $this->config = require __DIR__ . '/../../config/tg-notifier.php';
19
    }
20
21
    /**
22
     * Handle config and return value
23
     *
24
     * @param string $string
25
     *
26
     * @return array|mixed
27
     */
28
    public function execConfig(string $string): mixed
29
    {
30
        $config = explode('.', $string);
31
        $result = $this->config;
32
        foreach ($config as $value) {
33
            if (!isset($result[$value])) {
34
                return '';
35
            }
36
37
            $result = $result[$value];
38
        }
39
40
        return $result;
41
    }
42
43
    /**
44
     * Return template data
45
     *
46
     * @param string $partialPath
47
     * @param array $data
48
     *
49
     * @return bool|string
50
     */
51
    public function getTemplateData(string $partialPath, array $data = []): bool|string
52
    {
53
        $viewPathFile = $this->execConfig('telegram-git-notifier.view.path') . '/'
54
            . str_replace('.', '/', $partialPath) . '.php';
55
56
        if (!file_exists($viewPathFile)) {
57
            return '';
58
        }
59
60
        $content = '';
61
        ob_start();
62
63
        try {
64
            extract($data, EXTR_SKIP);
65
            require_once $viewPathFile;
66
            $content = ob_get_clean();
67
        } catch (EntryNotFoundException|InvalidViewTemplateException|Throwable $e) {
68
            ob_end_clean();
69
            error_log($e->getMessage());
70
        }
71
72
        return $content;
73
    }
74
}
75