Completed
Push — master ( 9a3a3c...24148b )
by Michael
14s
created

getMonologLoggerCliService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
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\Container;
12
use Joomla\DI\ServiceProviderInterface;
13
use Monolog\Handler\StreamHandler;
14
use Monolog\Logger;
15
use Monolog\Processor\PsrLogMessageProcessor;
16
use Monolog\Processor\WebProcessor;
17
18
/**
19
 * Monolog service provider
20
 */
21
class MonologServiceProvider implements ServiceProviderInterface
22
{
23
	/**
24
	 * Registers the service provider with a DI container.
25
	 *
26
	 * @param   Container  $container  The DI container.
27
	 *
28
	 * @return  void
29
	 */
30 11
	public function register(Container $container): void
31
	{
32
		// Register the PSR-3 processor
33 11
		$container->share('monolog.processor.psr3', [$this, 'getMonologProcessorPsr3Service']);
34
35
		// Register the web processor
36 11
		$container->share('monolog.processor.web', [$this, 'getMonologProcessorWebService']);
37
38
		// Register the web application handler
39 11
		$container->share('monolog.handler.application', [$this, 'getMonologHandlerApplicationService']);
40
41
		// Register the database handler
42 11
		$container->share('monolog.handler.database', [$this, 'getMonologHandlerDatabaseService']);
43
44
		// Register the web application Logger
45 11
		$container->share('monolog.logger.application', [$this, 'getMonologLoggerApplicationService']);
46
47
		// Register the CLI application Logger
48 11
		$container->share('monolog.logger.cli', [$this, 'getMonologLoggerCliService']);
49
50
		// Register the database Logger
51 11
		$container->share('monolog.logger.database', [$this, 'getMonologLoggerDatabaseService']);
52 11
	}
53
54
	/**
55
	 * Get the `monolog.processor.psr3` service
56
	 *
57
	 * @param   Container  $container  The DI container.
58
	 *
59
	 * @return  PsrLogMessageProcessor
60
	 */
61
	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...
62
	{
63
		return new PsrLogMessageProcessor;
64
	}
65
66
	/**
67
	 * Get the `monolog.processor.web` service
68
	 *
69
	 * @param   Container  $container  The DI container.
70
	 *
71
	 * @return  WebProcessor
72
	 */
73 6
	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...
74
	{
75 6
		return new WebProcessor;
76
	}
77
78
	/**
79
	 * Get the `monolog.handler.application` service
80
	 *
81
	 * @param   Container  $container  The DI container.
82
	 *
83
	 * @return  StreamHandler
84
	 */
85 7 View Code Duplication
	public function getMonologHandlerApplicationService(Container $container): StreamHandler
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
	{
87
		/** @var \Joomla\Registry\Registry $config */
88 7
		$config = $container->get('config');
89
90 7
		$level = strtoupper($config->get('log.application', $config->get('log.level', 'error')));
91
92 7
		return new StreamHandler(APPROOT . '/logs/stats.log', \constant('\\Monolog\\Logger::' . $level));
93
	}
94
95
	/**
96
	 * Get the `monolog.handler.database` service
97
	 *
98
	 * @param   Container  $container  The DI container.
99
	 *
100
	 * @return  StreamHandler
101
	 */
102 View Code Duplication
	public function getMonologHandlerDatabaseService(Container $container): StreamHandler
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
	{
104
		/** @var \Joomla\Registry\Registry $config */
105
		$config = $container->get('config');
106
107
		$level = strtoupper($config->get('log.database', $config->get('log.level', 'error')));
108
109
		return new StreamHandler(APPROOT . '/logs/stats.log', \constant('\\Monolog\\Logger::' . $level));
110
	}
111
112
	/**
113
	 * Get the `monolog.logger.application` service
114
	 *
115
	 * @param   Container  $container  The DI container.
116
	 *
117
	 * @return  Logger
118
	 */
119 6
	public function getMonologLoggerApplicationService(Container $container): Logger
120
	{
121 6
		return new Logger(
122 6
			'Application',
123
			[
124 6
				$container->get('monolog.handler.application'),
125
			],
126
			[
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...
127 6
				$container->get('monolog.processor.web'),
128
			]
129
		);
130
	}
131
132
	/**
133
	 * Get the `monolog.logger.cli` service
134
	 *
135
	 * @param   Container  $container  The DI container.
136
	 *
137
	 * @return  Logger
138
	 */
139 1
	public function getMonologLoggerCliService(Container $container): Logger
140
	{
141 1
		return new Logger(
142 1
			'Application',
143
			[
144 1
				$container->get('monolog.handler.application'),
145
			]
146
		);
147
	}
148
149
	/**
150
	 * Get the `monolog.logger.database` service
151
	 *
152
	 * @param   Container  $container  The DI container.
153
	 *
154
	 * @return  Logger
155
	 */
156
	public function getMonologLoggerDatabaseService(Container $container): Logger
157
	{
158
		return new Logger(
159
			'Application',
160
			[
161
				$container->get('monolog.handler.database'),
162
			],
163
			[
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...
164
				$container->get('monolog.processor.psr3'),
165
				$container->get('monolog.processor.web'),
166
			]
167
		);
168
	}
169
}
170