Completed
Pull Request — master (#30)
by vincent
03:53
created

MonologBuilder::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 1
crap 3
1
<?php declare(strict_types = 1);
2
/**
3
 * This file is part of the Tmdb package.
4
 *
5
 * (c) Vincent Faliès <[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
 * @author Vincent Faliès <[email protected]>
11
 * @copyright Copyright (c) 2017
12
 */
13
14
15
namespace VfacTmdb\Factory\Builder;
16
17
use Monolog\Logger;
18
use VfacTmdb\Interfaces\Factory\LoggerBuilderInterface;
19
20
/**
21
 * Builder for Monolog logger
22
 * @package Tmdb
23
 * @author Vincent Faliès <[email protected]>
24
 * @copyright Copyright (c) 2017
25
 */
26
class MonologBuilder implements LoggerBuilderInterface
27
{
28
    /**
29
     * Logger name string
30
     * @var string
31
     */
32
    protected $loggerName = 'tmdb';
33
34
    /**
35
     * Handler config
36
     * @var array
37
     */
38
    protected $handlersConfig = [
39
        [
40
            'class' => 'Monolog\Handler\StreamHandler',
41
            'params' => ['php://stdout', Logger::DEBUG],
42
        ],
43
    ];
44
45
    /**
46
     * Constructor
47
     * @param array $config
48
     */
49 4
    public function __construct(array $config = [])
50
    {
51 4
        $this->loggerName = isset($config['name']) ? $config['name'] : $this->loggerName;
52
53 4
        $this->handlersConfig = isset($config['handlers']) ? $config['handlers'] : $this->handlersConfig;
54 4
    }
55
56
    /**
57
     * Get Logger
58
     * This method MUST return a valid PSR3 logger
59
     * @return \Psr\Log\LoggerInterface
60
     */
61 1
    public function getLogger() : \Psr\Log\LoggerInterface
62
    {
63 1
        $logger = new Logger($this->loggerName);
64
65 1
        foreach ($this->handlersConfig as $config) {
66 1
            $handler = $this->newHandler($config['class'], $config['params']);
67 1
            $logger->pushHandler($handler);
68
        }
69
70 1
        return $logger;
71
    }
72
73
    /**
74
     * Create new handler
75
     * @param string $class
76
     * @param array $params
77
     * @return \Monolog\Handler\HandlerInterface
78
     */
79 2
    public function newHandler(string $class, array $params = []) : \Monolog\Handler\HandlerInterface
80
    {
81 2
        $reflection = new \ReflectionClass($class);
82
83 2
        if (!$reflection->implementsInterface('Monolog\Handler\HandlerInterface')) {
84 1
            throw new \InvalidArgumentException();
85
        }
86
87 1
        return $reflection->newInstanceArgs($params);
88
    }
89
90
    /**
91
     * Get main class name
92
     * This method MUST return the name of the main class
93
     * @return string
94
     */
95 1
    public function getMainClassName() : string
96
    {
97 1
        return 'Monolog\Logger';
98
    }
99
100
    /**
101
     * Get package name
102
     * This method MUST return the name of the package name
103
     * @return string
104
     */
105 1
    public function getPackageName() : string
106
    {
107 1
        return 'monolog/monolog';
108
    }
109
}
110