Passed
Push — master ( a4d511...6fbf8f )
by Morris
20:15 queued 11s
created

Manager::setupProvider()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 19
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\UserStatus;
27
28
use OCP\ILogger;
29
use OCP\IServerContainer;
30
use OCP\UserStatus\IManager;
31
use OCP\UserStatus\IProvider;
32
use Psr\Container\ContainerExceptionInterface;
33
34
class Manager implements IManager {
35
36
	/** @var IServerContainer */
37
	private $container;
38
39
	/** @var ILogger */
40
	private $logger;
41
42
	/** @var null */
43
	private $providerClass;
44
45
	/** @var IProvider */
46
	private $provider;
47
48
	/**
49
	 * Manager constructor.
50
	 *
51
	 * @param IServerContainer $container
52
	 * @param ILogger $logger
53
	 */
54
	public function __construct(IServerContainer $container,
55
								ILogger $logger) {
56
		$this->container = $container;
57
		$this->logger = $logger;
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function getUserStatuses(array $userIds): array {
64
		$this->setupProvider();
65
		if (!$this->provider) {
66
			return [];
67
		}
68
69
		return $this->provider->getUserStatuses($userIds);
70
	}
71
72
	/**
73
	 * @param string $class
74
	 * @since 20.0.0
75
	 * @internal
76
	 */
77
	public function registerProvider(string $class): void {
78
		$this->providerClass = $class;
0 ignored issues
show
Documentation Bug introduced by
It seems like $class of type string is incompatible with the declared type null of property $providerClass.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
		$this->provider = null;
80
	}
81
82
	/**
83
	 * Lazily set up provider
84
	 */
85
	private function setupProvider(): void {
86
		if ($this->provider !== null) {
87
			return;
88
		}
89
		if ($this->providerClass === null) {
90
			return;
91
		}
92
93
		try {
94
			$provider = $this->container->get($this->providerClass);
95
		} catch (ContainerExceptionInterface $e) {
96
			$this->logger->logException($e, [
97
				'message' => 'Could not load user-status provider dynamically: ' . $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

97
				'message' => 'Could not load user-status provider dynamically: ' . $e->/** @scrutinizer ignore-call */ getMessage(),
Loading history...
98
				'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

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

98
				'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
99
			]);
100
			return;
101
		}
102
103
		$this->provider = $provider;
104
	}
105
}
106