Completed
Pull Request — master (#5)
by Dan
04:20
created

Logger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace SixtyNine\Cloud\Factory;
4
5
use Monolog\Handler\ErrorLogHandler;
6
use Monolog\Handler\NullHandler;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger as MonologLogger;
9
10
class Logger
11
{
12
    const DEBUG = 100;
13
    const INFO = 200;
14
    const NOTICE = 250;
15
    const WARNING = 300;
16
    const ERROR = 400;
17
    const CRITICAL = 500;
18
    const ALERT = 550;
19
    const EMERGENCY = 600;
20
21
    /** @var Logger */
22
    protected static $instance;
23
    /** @var Logger */
24
    protected $logger;
25
    /** @var string */
26
    protected $filename;
27
    /** @var bool */
28
    protected $outputChosen = false;
29
30 1
    protected function __construct()
31
    {
32 1
        $this->logger = new MonologLogger('log');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Monolog\Logger('log') of type object<Monolog\Logger> is incompatible with the declared type object<SixtyNine\Cloud\Factory\Logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33 1
    }
34
35 5
    public  static function getInstance()
36
    {
37 5
        if (!self::$instance) {
38 1
            self::$instance = new self();
39
        }
40 5
        return self::$instance;
41
    }
42
43
    public function toConsole()
44
    {
45
        $this->outputChosen = true;
46
        $this->logger->pushHandler(new ErrorLogHandler());
0 ignored issues
show
Bug introduced by
The method pushHandler() does not seem to exist on object<SixtyNine\Cloud\Factory\Logger>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        return $this;
48
    }
49
50
    public function toFile($filename, $level = MonologLogger::DEBUG)
51
    {
52
        if ($this->filename) {
53
            throw new \InvalidArgumentException('Log file already set');
54
        }
55
        $this->outputChosen = true;
56
        $this->logger->pushHandler(new StreamHandler($filename, $level));
0 ignored issues
show
Bug introduced by
The method pushHandler() does not seem to exist on object<SixtyNine\Cloud\Factory\Logger>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $this->filename = $filename;
58
        return $this;
59
    }
60
61 5
    public function log($message, $level = MonologLogger::INFO, array $context = array())
62
    {
63 5
        if (!$this->outputChosen) {
64 1
            $this->outputChosen = true;
65 1
            $this->logger->pushHandler(new NullHandler());
0 ignored issues
show
Bug introduced by
The method pushHandler() does not seem to exist on object<SixtyNine\Cloud\Factory\Logger>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67 5
        $this->logger->log($level, $message, $context);
68 5
    }
69
70
    public function getLogger()
71
    {
72
        return $this->logger;
73
    }
74
}
75