MonologBuilder::getPackageName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Factory\Builder;
12
13
use FeedIo\Factory\LoggerBuilderInterface;
14
use Monolog\Handler\HandlerInterface;
15
use Monolog\Logger;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * @package FeedIo
20
 */
21
class MonologBuilder implements LoggerBuilderInterface
22
{
23
    protected $loggerName = 'feed-io';
24
25
    protected $handlersConfig = [
26
        [
27
            'class' => 'Monolog\Handler\StreamHandler',
28
            'params' => ['php://stdout', Logger::DEBUG],
29
        ],
30
    ];
31
32
    /**
33
     * @param array $config
34
     */
35 7
    public function __construct(array $config = [])
36
    {
37 7
        $this->loggerName = isset($config['name']) ? $config['name']:$this->loggerName;
38
        
39 7
        $this->handlersConfig = isset($config['handlers']) ? $config['handlers']:$this->handlersConfig;
40 7
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 2
    public function getLogger() : LoggerInterface
46
    {
47 2
        $logger = new Logger($this->loggerName);
48
        
49 2
        foreach ($this->handlersConfig as $config) {
50 2
            $handler = $this->newHandler($config['class'], $config['params']);
51 2
            $logger->pushHandler($handler);
52
        }
53
        
54 2
        return $logger;
55
    }
56
    
57
    /**
58
     * @param string $class
59
     * @param array $params
60
     * @return \Monolog\Handler\HandlerInterface
61
     */
62 4
    public function newHandler(string $class, array $params = []) : HandlerInterface
63
    {
64 4
        $reflection = new \ReflectionClass($class);
65
        
66 4
        if (! $reflection->implementsInterface('Monolog\Handler\HandlerInterface')) {
67 1
            throw new \InvalidArgumentException();
68
        }
69
        
70 3
        return $reflection->newInstanceArgs($params);
71
    }
72
73
    /**
74
     * This method MUST return the name of the main class
75
     * @return string
76
     */
77 1
    public function getMainClassName() : string
78
    {
79 1
        return 'Monolog\Logger';
80
    }
81
    
82
    /**
83
     * This method MUST return the name of the package name
84
     * @return string
85
     */
86 1
    public function getPackageName() : string
87
    {
88 1
        return 'monolog/monolog';
89
    }
90
}
91