Passed
Push — develop ( 89ed24...6b9531 )
by Nikolay
04:52
created

ConfigProvider::register()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 4 2020
7
 *
8
 */
9
10
declare(strict_types=1);
11
12
namespace MikoPBX\Common\Providers;
13
14
use Phalcon\Config\Adapter\Json;
15
use Phalcon\Di;
16
use Phalcon\Di\DiInterface;
17
use Phalcon\Di\ServiceProviderInterface;
18
use Phalcon\Exception;
19
20
/**
21
 * Read the configuration
22
 */
23
class ConfigProvider implements ServiceProviderInterface
24
{
25
    public const SERVICE_NAME = 'config';
26
27
    /**
28
     * Register config provider
29
     *
30
     * @param \Phalcon\Di\DiInterface $di
31
     *
32
     * @throws \Phalcon\Exception
33
     */
34
    public function register(DiInterface $di): void
35
    {
36
        $configPath = '/etc/inc/mikopbx-settings.json';
37
38
        if ( ! file_exists($configPath)
39
            || ! is_readable($configPath)
40
        ) {
41
            throw new Exception('Config file does not exist: ' . $configPath);
42
        }
43
44
        $di->setShared(
45
            self::SERVICE_NAME,
46
            function () use ($configPath) {
47
                return new Json($configPath);
48
            }
49
        );
50
    }
51
52
    /**
53
     * Recreates modules service after enable or disable them
54
     */
55
    public static function recreateConfigProvider(): void
56
    {
57
        $di = Di::getDefault();
58
        $di->remove(self::SERVICE_NAME);
59
        $di->register(new self());
60
    }
61
}