Completed
Push — master ( 09df6b...307ee1 )
by Branko
22s queued 10s
created

FaceRecognitionLogger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 43
ccs 10
cts 22
cp 0.4545
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 4
A logInfo() 0 7 3
A logDebug() 0 7 3
A getLogger() 0 2 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\BackgroundJob;
25
26
use OCP\ILogger;
27
28
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
/**
31
 * Logger class that encapsulates logging for background tasks. Reason for this
32
 * class is because background processing can be called as either command, as
33
 * well as background job, so we need a way to unify logging for both.
34
 */
35
class FaceRecognitionLogger {
36
	/** @var ILogger */
37
	private $logger;
38
39
	/** @var OutputInterface */
40
	private $output;
41
42 18
	public function __construct($logger) {
43 18
		if (method_exists($logger, 'info') && method_exists($logger, 'debug')) {
44 18
			$this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
$logger is of type object, but the property $logger was declared to be of type OCP\ILogger. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
		} else if ($logger instanceof OutputInterface) {
46
			$this->output = $logger;
47
		} else {
48
			throw new \InvalidArgumentException("Logger must be either instance of ILogger or OutputInterface");
49
		}
50 18
	}
51
52
	/**
53
	 * Returns logger, if it is set
54
	 *
55
	 * @return ILogger|null Logger, if it is set.
56
	 */
57 4
	public function getLogger() {
58 4
		return $this->logger;
59
	}
60
61 11
	public function logInfo(string $message) {
62 11
		if (!is_null($this->logger)) {
63 11
			$this->logger->info($message);
64
		} else if (!is_null($this->output)) {
65
			$this->output->writeln($message);
66
		} else {
67
			throw new \RuntimeException("There are no configured loggers. Please file an issue at https://github.com/matiasdelellis/facerecognition/issues");
68
		}
69 11
	}
70
71
	public function logDebug(string $message) {
72
		if (!is_null($this->logger)) {
73
			$this->logger->debug($message);
74
		} else if (!is_null($this->output)) {
75
			$this->output->writeln($message);
76
		} else {
77
			throw new \RuntimeException("There are no configured loggers. Please file an issue at https://github.com/matiasdelellis/facerecognition/issues");
78
		}
79
	}
80
}