LogServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 84
rs 10
c 1
b 0
f 0
wmc 9
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 28 5
A formatLogConfig() 0 44 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/15/2018
6
 * Time: 9:22 AM
7
 */
8
9
namespace TimSDK\Foundation\ServiceProviders;
10
11
use Monolog\Handler\NullHandler;
12
use Pimple\Container;
13
use TimSDK\Foundation\Log\LogManager;
14
use TimSDK\Support\Arr;
15
16
class LogServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Registers services on the given container.
20
     *
21
     * This method should only be used to configure services and parameters.
22
     * It should not get services.
23
     *
24
     * @param Container $pimple A container instance
25
     */
26
    public function register(Container $pimple)
27
    {
28
        $pimple['logger'] = $pimple['log'] = function ($app) {
29
            $config = $this->formatLogConfig($app);
30
31
            if (!empty($config)) {
32
                $app['config']->merge($config);
33
            }
34
35
            $log = new LogManager($app);
36
37
            if (defined('PHPUNIT_RUNNING') || 'cli' === php_sapi_name()) {
38
                if (Arr::get($config, 'cli_on', false)) {
39
                    $log->setDefaultDriver('errorlog')
40
                        ->addChannels([
41
                            'errorlog' => [
42
                                'driver' => 'errorlog',
43
                                'level'  => 'info',
44
                            ],
45
                        ]);
46
                } else {
47
                    $log->driver()->pushHandler(new NullHandler());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Log\LoggerInterface as the method pushHandler() does only exist in the following implementations of said interface: Monolog\Logger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48
                }
49
            }
50
51
            return $log;
52
        };
53
    }
54
55
    public function formatLogConfig($app)
56
    {
57
        if (empty($app['config']->get('log'))) {
58
            return [
59
                'log' => [
60
                    'cli_on'   => false,
61
                    'default'  => 'errorlog',
62
                    'channels' => [
63
                        'errorlog' => [
64
                            'driver' => 'errorlog',
65
                            'level'  => 'debug',
66
                        ],
67
                    ],
68
                ],
69
            ];
70
        }
71
        // 4.0 version
72
        if (empty($app['config']->get('log.driver'))) {
73
            return [
74
                'log' => [
75
                    'cli_on'   => false,
76
                    'default'  => 'single',
77
                    'channels' => [
78
                        'single' => [
79
                            'driver' => 'single',
80
                            'path'   => $app['config']->get('log.file') ?: \sys_get_temp_dir() . '/logs/tim-sdk.log',
81
                            'level'  => $app['config']->get('log.level', 'debug'),
82
                        ],
83
                    ],
84
                ],
85
            ];
86
        }
87
        $name = $app['config']->get('log.driver');
88
89
        return [
90
            'log' => [
91
                'cli_on'   => false,
92
                'default'  => $name,
93
                'channels' => [
94
                    $name => $app['config']->get('log'),
95
                ],
96
            ],
97
        ];
98
    }
99
}
100