LogServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatLogConfig() 0 39 4
A register() 0 10 2
1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Providers;
10
11
use Pimple\ServiceProviderInterface;
12
use Aplisin\DingTalk\Kernel\Log\LogManager;
13
use Pimple\Container;
14
15
class LogServiceProvider implements ServiceProviderInterface
16
{
17
    public function register(Container $pimple)
18
    {
19
        $pimple['logger'] = $pimple['log'] = function($app) {
20
            $config = $this->formatLogConfig($app);
21
22
            if (!empty($config)) {
23
                $app['config']->merge($config);
24
            }
25
26
            return new LogManager($app);
27
        };
28
    }
29
30
    public function formatLogConfig($app)
31
    {
32
        if (empty($app['config']->get('log'))) {
33
            return [
34
                'log' => [
35
                    'default' => 'errorlog',
36
                    'channels' => [
37
                        'errorlog' => [
38
                            'driver' => 'errorlog',
39
                            'level' => 'debug',
40
                        ],
41
                    ],
42
                ],
43
            ];
44
        }
45
46
        // 4.0 version
47
        if (empty($app['config']->get('log.driver'))) {
48
            return [
49
                'log' => [
50
                    'default' => 'single',
51
                    'channels' => [
52
                        'single' => [
53
                            'driver' => 'single',
54
                            'path' => $app['config']->get('log.file') ?: \sys_get_temp_dir().'/logs/dingtalk.log',
55
                            'level' => $app['config']->get('log.level', 'debug'),
56
                        ],
57
                    ],
58
                ],
59
            ];
60
        }
61
62
        $name = $app['config']->get('log.driver');
63
64
        return [
65
            'log' => [
66
                'default' => $name,
67
                'channels' => [
68
                    $name => $app['config']->get('log'),
69
                ],
70
            ],
71
        ];
72
    }
73
}
74