Passed
Push — master ( c82c1d...5e4ee4 )
by Roeland
10:52 queued 11s
created

Registry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 *
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OC\Support\Subscription;
29
30
use OCP\IConfig;
31
use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
32
use OCP\Support\Subscription\IRegistry;
33
use OCP\Support\Subscription\ISubscription;
34
use OCP\Support\Subscription\ISupportedApps;
35
36
class Registry implements IRegistry {
37
38
	/** @var ISubscription */
39
	private $subscription = null;
40
41
	/** @var IConfig */
42
	private $config;
43
44
	public function __construct(IConfig $config) {
45
		$this->config = $config;
46
	}
47
48
	/**
49
	 * Register a subscription instance. In case it is called multiple times the
50
	 * first one is used.
51
	 *
52
	 * @param ISubscription $subscription
53
	 * @throws AlreadyRegisteredException
54
	 *
55
	 * @since 17.0.0
56
	 */
57
	public function register(ISubscription $subscription): void {
58
		if ($this->subscription !== null) {
59
			throw new AlreadyRegisteredException();
60
		}
61
		$this->subscription = $subscription;
62
	}
63
64
	/**
65
	 * Fetches the list of app IDs that are supported by the subscription
66
	 *
67
	 * @since 17.0.0
68
	 */
69
	public function delegateGetSupportedApps(): array {
70
		if ($this->subscription instanceof ISupportedApps) {
71
			return $this->subscription->getSupportedApps();
72
		}
73
		return [];
74
	}
75
76
	/**
77
	 * Indicates if a valid subscription is available
78
	 *
79
	 * @since 17.0.0
80
	 */
81
	public function delegateHasValidSubscription(): bool {
82
		// Allow overwriting this manually for environments where the subscription information cannot be fetched
83
		if ($this->config->getSystemValueBool('has_valid_subscription')) {
84
			return true;
85
		}
86
87
		if ($this->subscription instanceof ISubscription) {
0 ignored issues
show
introduced by
$this->subscription is always a sub-type of OCP\Support\Subscription\ISubscription.
Loading history...
88
			return $this->subscription->hasValidSubscription();
89
		}
90
		return false;
91
	}
92
93
	/**
94
	 * Indicates if the subscription has extended support
95
	 *
96
	 * @since 17.0.0
97
	 */
98
	public function delegateHasExtendedSupport(): bool {
99
		if ($this->subscription instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
100
			return $this->subscription->hasExtendedSupport();
101
		}
102
		return false;
103
	}
104
}
105