Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

getMonologLoggerApplicationService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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
 * @since  1.0
24
 */
25
class MonologServiceProvider implements ServiceProviderInterface
26
{
27
	/**
28
	 * Registers the service provider with a DI container.
29
	 *
30
	 * @param   Container  $container  The DI container.
31
	 *
32
	 * @return  void
33
	 *
34
	 * @since   1.0
35
	 */
36 1
	public function register(Container $container)
37
	{
38
		// Register the PSR-3 processor
39 1
		$container->share('monolog.processor.psr3', [$this, 'getMonologProcessorPsr3Service'], true);
40
41
		// Register the web processor
42 1
		$container->share('monolog.processor.web', [$this, 'getMonologProcessorWebService'], true);
43
44
		// Register the web application handler
45 1
		$container->share('monolog.handler.application', [$this, 'getMonologHandlerApplicationService'], true);
46
47
		// Register the database handler
48 1
		$container->share('monolog.handler.database', [$this, 'getMonologHandlerDatabaseService'], true);
49
50
		// Register the web application Logger
51 1
		$container->share('monolog.logger.application', [$this, 'getMonologLoggerApplicationService'], true);
52
53
		// Register the CLI application Logger
54 1
		$container->share('monolog.logger.cli', [$this, 'getMonologLoggerCliService'], true);
55
56
		// Register the database Logger
57 1
		$container->share('monolog.logger.database', [$this, 'getMonologLoggerDatabaseService'], true);
58 1
	}
59
60
	/**
61
	 * Get the `monolog.processor.psr3` service
62
	 *
63
	 * @param   Container  $container  The DI container.
64
	 *
65
	 * @return  PsrLogMessageProcessor
66
	 *
67
	 * @since   1.0
68
	 */
69 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...
70
	{
71 1
		return new PsrLogMessageProcessor;
72
	}
73
74
	/**
75
	 * Get the `monolog.processor.web` service
76
	 *
77
	 * @param   Container  $container  The DI container.
78
	 *
79
	 * @return  WebProcessor
80
	 *
81
	 * @since   1.0
82
	 */
83 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...
84
	{
85 1
		return new WebProcessor;
86
	}
87
88
	/**
89
	 * Get the `monolog.handler.application` service
90
	 *
91
	 * @param   Container  $container  The DI container.
92
	 *
93
	 * @return  StreamHandler
94
	 *
95
	 * @since   1.0
96
	 */
97 1
	public function getMonologHandlerApplicationService(Container $container) : StreamHandler
98
	{
99
		/** @var \Joomla\Registry\Registry $config */
100 1
		$config = $container->get('config');
101
102 1
		$level = strtoupper($config->get('log.application', $config->get('log.level', 'error')));
103
104 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
105
	}
106
107
	/**
108
	 * Get the `monolog.handler.database` service
109
	 *
110
	 * @param   Container  $container  The DI container.
111
	 *
112
	 * @return  StreamHandler
113
	 *
114
	 * @since   1.0
115
	 */
116 1
	public function getMonologHandlerDatabaseService(Container $container) : StreamHandler
117
	{
118
		/** @var \Joomla\Registry\Registry $config */
119 1
		$config = $container->get('config');
120
121
		// If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config
122 1
		$level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error')));
123
124 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
125
	}
126
127
	/**
128
	 * Get the `monolog.logger.application` service
129
	 *
130
	 * @param   Container  $container  The DI container.
131
	 *
132
	 * @return  Logger
133
	 *
134
	 * @since   1.0
135
	 */
136 1
	public function getMonologLoggerApplicationService(Container $container) : Logger
137
	{
138 1
		return new Logger(
139 1
			'Application',
140
			[
141 1
				$container->get('monolog.handler.application')
142
			],
143
			[
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...
144 1
				$container->get('monolog.processor.web')
145
			]
146
		);
147
	}
148
149
	/**
150
	 * Get the `monolog.logger.cli` service
151
	 *
152
	 * @param   Container  $container  The DI container.
153
	 *
154
	 * @return  Logger
155
	 *
156
	 * @since   1.0
157
	 */
158 1
	public function getMonologLoggerCliService(Container $container) : Logger
159
	{
160 1
		return new Logger(
161 1
			'Application',
162
			[
163 1
				$container->get('monolog.handler.application')
164
			]
165
		);
166
	}
167
168
	/**
169
	 * Get the `monolog.logger.database` service
170
	 *
171
	 * @param   Container  $container  The DI container.
172
	 *
173
	 * @return  Logger
174
	 *
175
	 * @since   1.0
176
	 */
177 1
	public function getMonologLoggerDatabaseService(Container $container) : Logger
178
	{
179 1
		return new Logger(
180 1
			'Application',
181
			[
182 1
				$container->get('monolog.handler.database')
183
			],
184
			[
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...
185 1
				$container->get('monolog.processor.psr3'),
186 1
				$container->get('monolog.processor.web')
187
			]
188
		);
189
	}
190
}
191