LogServiceProvider::formatLogConfig()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 23
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 39
rs 9.552
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel\Providers;
4
5
use Freyo\ApiGateway\Kernel\Log\LogManager;
6
use Pimple\Container;
7
use Pimple\ServiceProviderInterface;
8
9
class LogServiceProvider implements ServiceProviderInterface
10
{
11
    /**
12
     * Registers services on the given container.
13
     *
14
     * This method should only be used to configure services and parameters.
15
     * It should not get services.
16
     *
17
     * @param Container $pimple A container instance
18
     */
19
    public function register(Container $pimple)
20
    {
21
        $pimple['logger'] = $pimple['log'] = function ($app) {
22
            $config = $this->formatLogConfig($app);
23
24
            if (!empty($config)) {
25
                $app['config']->merge($config);
26
            }
27
28
            return new LogManager($app);
29
        };
30
    }
31
32
    public function formatLogConfig($app)
33
    {
34
        if (empty($app['config']->get('log'))) {
35
            return [
36
                'log' => [
37
                    'default' => 'errorlog',
38
                    'channels' => [
39
                        'errorlog' => [
40
                            'driver' => 'errorlog',
41
                            'level' => 'debug',
42
                        ],
43
                    ],
44
                ],
45
            ];
46
        }
47
48
        // 4.0 version
49
        if (empty($app['config']->get('log.driver'))) {
50
            return [
51
                'log' => [
52
                    'default' => 'single',
53
                    'channels' => [
54
                        'single' => [
55
                            'driver' => 'single',
56
                            'path' => $app['config']->get('log.file') ?: \sys_get_temp_dir().'/logs/apigateway.log',
57
                            'level' => $app['config']->get('log.level', 'debug'),
58
                        ],
59
                    ],
60
                ],
61
            ];
62
        }
63
64
        $name = $app['config']->get('log.driver');
65
66
        return [
67
            'log' => [
68
                'default' => $name,
69
                'channels' => [
70
                    $name => $app['config']->get('log'),
71
                ],
72
            ],
73
        ];
74
    }
75
}
76