LogServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 24
c 1
b 0
f 1
dl 0
loc 51
rs 10
wmc 6

2 Methods

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