Passed
Push — master ( 98fd66...bf4acd )
by Joas
18:11 queued 12s
created

Manager::revertUserStatus()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
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 GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
namespace OC\UserStatus;
27
28
use OCP\IServerContainer;
29
use OCP\UserStatus\IManager;
30
use OCP\UserStatus\IProvider;
31
use Psr\Container\ContainerExceptionInterface;
32
use Psr\Log\LoggerInterface;
33
34
class Manager implements IManager {
35
36
	/** @var IServerContainer */
37
	private $container;
38
39
	/** @var LoggerInterface */
40
	private $logger;
41
42
	/** @var class-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
43
	private $providerClass;
44
45
	/** @var IProvider */
46
	private $provider;
47
48
	/**
49
	 * Manager constructor.
50
	 *
51
	 * @param IServerContainer $container
52
	 * @param LoggerInterface $logger
53
	 */
54
	public function __construct(IServerContainer $container,
55
								LoggerInterface $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;
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
		/**
94
		 * @psalm-suppress InvalidCatch
95
		 */
96
		try {
97
			$provider = $this->container->get($this->providerClass);
98
		} catch (ContainerExceptionInterface $e) {
99
			$this->logger->error('Could not load user-status "' . $this->providerClass . '" 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

99
			$this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->/** @scrutinizer ignore-call */ getMessage(), [
Loading history...
100
				'exception' => $e,
101
			]);
102
			return;
103
		}
104
105
		$this->provider = $provider;
106
	}
107
108
	public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void {
109
		$this->setupProvider();
110
		if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
111
			return;
112
		}
113
114
		$this->provider->setUserStatus($userId, $messageId, $status, $createBackup);
115
	}
116
117
	public function revertUserStatus(string $userId, string $messageId, string $status): void {
118
		$this->setupProvider();
119
		if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
120
			return;
121
		}
122
		$this->provider->revertUserStatus($userId, $messageId, $status);
123
	}
124
125
	public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void {
126
		$this->setupProvider();
127
		if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
128
			return;
129
		}
130
		$this->provider->revertMultipleUserStatus($userIds, $messageId, $status);
131
	}
132
}
133