Completed
Push — master ( 3dda54...befd6a )
by Michael
02:11
created

MonologServiceProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 43.4%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 2
c 4
b 0
f 1
lcom 0
cbo 6
dl 0
loc 105
ccs 23
cts 53
cp 0.434
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 93 2
1
<?php
2
3
namespace Stats\Providers;
4
5
use Joomla\DI\Container;
6
use Joomla\DI\ServiceProviderInterface;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger;
9
use Monolog\Processor\PsrLogMessageProcessor;
10
use Monolog\Processor\WebProcessor;
11
use Psr\Log\LoggerInterface;
12
13
/**
14
 * Monolog service provider
15
 *
16
 * @since  1.0
17
 */
18
class MonologServiceProvider implements ServiceProviderInterface
19
{
20
	/**
21
	 * Registers the service provider with a DI container.
22
	 *
23
	 * @param   Container  $container  The DI container.
24
	 *
25
	 * @return  void
26
	 *
27
	 * @since   1.0
28
	 */
29 1
	public function register(Container $container)
30
	{
31
		// Register the PSR-3 processor
32 1
		$container->share(
33 1
			'monolog.processor.psr3',
34
			function ()
35
			{
36
				return new PsrLogMessageProcessor;
37
			}
38 1
		);
39
40
		// Register the web processor
41 1
		$container->share(
42 1
			'monolog.processor.web',
43
			function ()
44
			{
45
				return new WebProcessor;
46
			}
47 1
		);
48
49
		// Register the main application handler
50 1
		$container->share(
51 1
			'monolog.handler.application',
52
			function (Container $container)
53
			{
54
				/** @var \Joomla\Registry\Registry $config */
55
				$config = $container->get('config');
56
57
				$level = strtoupper($config->get('log.application', $config->get('log.level', 'error')));
58
59
				return new StreamHandler(
60
					APPROOT . '/logs/stats.log',
61
					constant('\\Monolog\\Logger::' . $level)
62
				);
63
			}
64 1
		);
65
66
		// Register the database handler
67 1
		$container->share(
68 1
			'monolog.handler.database',
69
			function (Container $container)
70
			{
71
				/** @var \Joomla\Registry\Registry $config */
72
				$config = $container->get('config');
73
74
				// If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config
75
				$level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error')));
76
77
				return new StreamHandler(
78
					APPROOT . '/logs/stats.log',
79
					constant('\\Monolog\\Logger::' . $level)
80
				);
81
			}
82 1
		);
83
84
		// Register the main Logger
85 1
		$container->alias('monolog', Logger::class)
86 1
			->alias('monolog.logger.application', Logger::class)
87 1
			->alias(LoggerInterface::class, Logger::class)
88 1
			->share(
89 1
				Logger::class,
90
				function (Container $container)
91
				{
92
					return new Logger(
93
						'Application',
94
						[
95
							$container->get('monolog.handler.application')
96
						],
97
						[
0 ignored issues
show
Documentation introduced by
array($container->get('monolog.processor.web')) is of type array<integer,*,{"0":"*"}>, but the function expects a array<integer,callable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
							$container->get('monolog.processor.web')
99
						]
100
					);
101
				}
102 1
			);
103
104
		// Register the database Logger
105 1
		$container->share(
106 1
			'monolog.logger.database',
107
			function (Container $container)
108
			{
109
				return new Logger(
110
					'Database',
111
					[
112
						$container->get('monolog.handler.database')
113
					],
114
					[
0 ignored issues
show
Documentation introduced by
array($container->get('m...onolog.processor.web')) is of type array<integer,*,{"0":"*","1":"*"}>, but the function expects a array<integer,callable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
						$container->get('monolog.processor.psr3'),
116
						$container->get('monolog.processor.web')
117
					]
118
				);
119
			}
120 1
		);
121 1
	}
122
}
123