Passed
Push — 6.x ( 1f2552...dee522 )
by Jerome
13:17
created

CronLogHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 15
dl 0
loc 45
ccs 16
cts 17
cp 0.9412
rs 10
c 1
b 1
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A close() 0 2 1
A getDefaultFormatter() 0 6 1
A __construct() 0 13 3
A write() 0 2 1
1
<?php
2
3
namespace Elgg\Cli;
4
5
use Elgg\Application;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Elgg\Cli\Application. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Elgg\Exceptions\Exception;
7
use Elgg\Logger\ElggLogFormatter;
8
use Monolog\Formatter\FormatterInterface;
9
use Monolog\Handler\AbstractProcessingHandler;
10
use Monolog\Level;
11
use Monolog\LogRecord;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Additional output handler for the \Elgg\Logger\Cron
17
 * which outputs to the stdout
18
 *
19
 * @since 6.3
20
 * @internal
21
 */
22
class CronLogHandler extends AbstractProcessingHandler {
23
	
24
	protected ?OutputInterface $stdout;
25
	
26
	/**
27
	 * {@inheritdoc}
28
	 */
29 3
	public function __construct(bool $bubble = true) {
30 3
		if (!Application::isCli()) {
31
			throw new Exception(__CLASS__ . ' can only be used during CLI');
32
		}
33
		
34 3
		$this->stdout = _elgg_services()->cli_output;
35
		
36 3
		$level = Level::Emergency;
37 3
		if ($this->stdout->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) {
38 2
			$level = Level::Debug;
39
		}
40
		
41 3
		parent::__construct($level, $bubble);
42
	}
43
	
44
	/**
45
	 * {@inheritdoc}
46
	 */
47 2
	protected function write(LogRecord $record): void {
48 2
		$this->stdout->write($record->formatted);
0 ignored issues
show
Bug introduced by
The method write() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
		$this->stdout->/** @scrutinizer ignore-call */ 
49
                 write($record->formatted);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
	}
50
	
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 2
	public function getDefaultFormatter(): FormatterInterface {
55 2
		$formatter = new ElggLogFormatter();
56 2
		$formatter->allowInlineLineBreaks();
57 2
		$formatter->ignoreEmptyContextAndExtra();
58
		
59 2
		return $formatter;
60
	}
61
	
62
	/**
63
	 * {@inheritdoc}
64
	 */
65 3
	public function close(): void {
66 3
		$this->stdout = new NullOutput();
67
	}
68
}
69