LoggerProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) 2015 Juan José Torroglosa Ramón
4
 *
5
 * This file is part of the Cliphar package.
6
 *
7
 * For the full copyright and license information, please view
8
 * the LICENSE file that was distributed with this source code.
9
 */
10
/**
11
 *
12
 * @author Juan José Torroglosa Ramón
13
 */
14
15
namespace Cliphar\ServiceProvider;
16
17
use Cliphar\Binder;
18
use Cliphar\Logger\Decorator\ConsoleLevelDecorator;
19
use Cliphar\Logger\Decorator\ConsoleMessageDecorator;
20
use Cliphar\Logger\Decorator\ConsoleTagForLevelDecorator;
21
use Cliphar\ServiceProvider;
22
use Cliphar\Logger\ConsoleLogger;
23
use Interop\Container\ContainerInterface;
24
use Psr\Log\LogLevel;
25
use Symfony\Component\Console\Output\ConsoleOutput;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class LoggerProvider implements ServiceProvider
29
{
30
    const FORMAT_INFO = false;
31
    const FORMAT_NOTICE = 'fg=green';
32
    const FORMAT_ERROR = 'fg=red';
33
    const FORMAT_DEBUG = 'fg=cyan';
34
    const FORMAT_WARNING = 'fg=yellow';
35
36
    protected $binder;
37
38
    /**
39
     * @var ContainerInterface
40
     */
41
    private $container;
42
43
    public function __construct(Binder $binder, ContainerInterface $container)
44
    {
45
        $this->binder = $binder;
46
        $this->container = $container;
47
    }
48
49
    public function register()
50
    {
51
        $this->binder->bind('Psr\Log\LoggerInterface', function() {
52
            $formatLevelMap = array(
53
                LogLevel::EMERGENCY => self::FORMAT_ERROR,
54
                LogLevel::ALERT => self::FORMAT_ERROR,
55
                LogLevel::CRITICAL => self::FORMAT_ERROR,
56
                LogLevel::ERROR => self::FORMAT_ERROR,
57
                LogLevel::WARNING => self::FORMAT_WARNING,
58
                LogLevel::NOTICE => self::FORMAT_NOTICE,
59
                LogLevel::INFO => self::FORMAT_INFO,
60
                LogLevel::DEBUG => self::FORMAT_DEBUG,
61
            );
62
63
            $verbosityLevelMap = array(
64
                LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
65
                LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
66
                LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
67
                LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
68
                LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
69
                LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
70
                LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
71
                LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE,
72
            );
73
74
            $output = $this->getOutput();
75
76
            $decorator = new ConsoleMessageDecorator();
77
            $decorator = new ConsoleLevelDecorator($decorator);
78
79
            if ($output->isDecorated()) {
80
                $decorator = new ConsoleTagForLevelDecorator($decorator, $formatLevelMap);
81
            }
82
83
            return new ConsoleLogger($output, LogLevel::WARNING, $verbosityLevelMap, $decorator);
84
        });
85
    }
86
87
    /**
88
     * @return ConsoleOutput
89
     */
90
    public function getOutput()
91
    {
92
        return $this->container->get('Symfony\Component\Console\Output\OutputInterface');
93
    }
94
}