Completed
Push — mysql_improvements ( 2e95ce...dedbef )
by Michael
03:52
created

getMonologLoggerDatabaseService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Providers;
10
11
use Joomla\DI\{
12
	Container, ServiceProviderInterface
13
};
14
use Monolog\Handler\StreamHandler;
15
use Monolog\Logger;
16
use Monolog\Processor\{
17
	PsrLogMessageProcessor, WebProcessor
18
};
19
20
/**
21
 * Monolog service provider
22
 */
23
class MonologServiceProvider implements ServiceProviderInterface
24
{
25
	/**
26
	 * Registers the service provider with a DI container.
27
	 *
28
	 * @param   Container  $container  The DI container.
29
	 *
30
	 * @return  void
31
	 */
32 1
	public function register(Container $container)
33
	{
34
		// Register the PSR-3 processor
35 1
		$container->share('monolog.processor.psr3', [$this, 'getMonologProcessorPsr3Service'], true);
36
37
		// Register the web processor
38 1
		$container->share('monolog.processor.web', [$this, 'getMonologProcessorWebService'], true);
39
40
		// Register the web application handler
41 1
		$container->share('monolog.handler.application', [$this, 'getMonologHandlerApplicationService'], true);
42
43
		// Register the database handler
44 1
		$container->share('monolog.handler.database', [$this, 'getMonologHandlerDatabaseService'], true);
45
46
		// Register the web application Logger
47 1
		$container->share('monolog.logger.application', [$this, 'getMonologLoggerApplicationService'], true);
48
49
		// Register the CLI application Logger
50 1
		$container->share('monolog.logger.cli', [$this, 'getMonologLoggerCliService'], true);
51
52
		// Register the database Logger
53 1
		$container->share('monolog.logger.database', [$this, 'getMonologLoggerDatabaseService'], true);
54 1
	}
55
56
	/**
57
	 * Get the `monolog.processor.psr3` service
58
	 *
59
	 * @param   Container  $container  The DI container.
60
	 *
61
	 * @return  PsrLogMessageProcessor
62
	 */
63 1
	public function getMonologProcessorPsr3Service(Container $container) : PsrLogMessageProcessor
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
	{
65 1
		return new PsrLogMessageProcessor;
66
	}
67
68
	/**
69
	 * Get the `monolog.processor.web` service
70
	 *
71
	 * @param   Container  $container  The DI container.
72
	 *
73
	 * @return  WebProcessor
74
	 */
75 1
	public function getMonologProcessorWebService(Container $container) : WebProcessor
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
	{
77 1
		return new WebProcessor;
78
	}
79
80
	/**
81
	 * Get the `monolog.handler.application` service
82
	 *
83
	 * @param   Container  $container  The DI container.
84
	 *
85
	 * @return  StreamHandler
86
	 */
87 1
	public function getMonologHandlerApplicationService(Container $container) : StreamHandler
88
	{
89
		/** @var \Joomla\Registry\Registry $config */
90 1
		$config = $container->get('config');
91
92 1
		$level = strtoupper($config->get('log.application', $config->get('log.level', 'error')));
93
94 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
95
	}
96
97
	/**
98
	 * Get the `monolog.handler.database` service
99
	 *
100
	 * @param   Container  $container  The DI container.
101
	 *
102
	 * @return  StreamHandler
103
	 */
104 1
	public function getMonologHandlerDatabaseService(Container $container) : StreamHandler
105
	{
106
		/** @var \Joomla\Registry\Registry $config */
107 1
		$config = $container->get('config');
108
109
		// If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config
110 1
		$level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error')));
111
112 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
113
	}
114
115
	/**
116
	 * Get the `monolog.logger.application` service
117
	 *
118
	 * @param   Container  $container  The DI container.
119
	 *
120
	 * @return  Logger
121
	 */
122 1
	public function getMonologLoggerApplicationService(Container $container) : Logger
123
	{
124 1
		return new Logger(
125 1
			'Application',
126
			[
127 1
				$container->get('monolog.handler.application')
128
			],
129
			[
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...
130 1
				$container->get('monolog.processor.web')
131
			]
132
		);
133
	}
134
135
	/**
136
	 * Get the `monolog.logger.cli` service
137
	 *
138
	 * @param   Container  $container  The DI container.
139
	 *
140
	 * @return  Logger
141
	 */
142 1
	public function getMonologLoggerCliService(Container $container) : Logger
143
	{
144 1
		return new Logger(
145 1
			'Application',
146
			[
147 1
				$container->get('monolog.handler.application')
148
			]
149
		);
150
	}
151
152
	/**
153
	 * Get the `monolog.logger.database` service
154
	 *
155
	 * @param   Container  $container  The DI container.
156
	 *
157
	 * @return  Logger
158
	 */
159 1
	public function getMonologLoggerDatabaseService(Container $container) : Logger
160
	{
161 1
		return new Logger(
162 1
			'Application',
163
			[
164 1
				$container->get('monolog.handler.database')
165
			],
166
			[
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...
167 1
				$container->get('monolog.processor.psr3'),
168 1
				$container->get('monolog.processor.web')
169
			]
170
		);
171
	}
172
}
173