Passed
Push — master ( 18acb1...10d862 )
by Roeland
11:01 queued 12s
created

Registry::getSubscription()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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\AppFramework\QueryException;
31
use OCP\IConfig;
32
use OCP\IServerContainer;
33
use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
34
use OCP\Support\Subscription\IRegistry;
35
use OCP\Support\Subscription\ISubscription;
36
use OCP\Support\Subscription\ISupportedApps;
37
38
class Registry implements IRegistry {
39
40
	/** @var ISubscription */
41
	private $subscription = null;
42
43
	/** @var string */
44
	private $subscriptionService = null;
45
46
	/** @var IConfig */
47
	private $config;
48
49
	/** @var IServerContainer */
50
	private $container;
51
52
	public function __construct(IConfig $config, IServerContainer $container) {
53
		$this->config = $config;
54
		$this->container = $container;
55
	}
56
57
	private function getSubscription(): ?ISubscription {
58
		if ($this->subscription === null && $this->subscriptionService !== null) {
59
			try {
60
				$this->subscription = $this->container->query($this->subscriptionService);
61
			} catch (QueryException $e) {
62
				// Ignore this
63
			}
64
		}
65
66
		return $this->subscription;
67
	}
68
69
	/**
70
	 * Register a subscription instance. In case it is called multiple times the
71
	 * first one is used.
72
	 *
73
	 * @param ISubscription $subscription
74
	 * @throws AlreadyRegisteredException
75
	 *
76
	 * @since 17.0.0
77
	 */
78
	public function register(ISubscription $subscription): void {
79
		if ($this->subscription !== null || $this->subscriptionService !== null) {
80
			throw new AlreadyRegisteredException();
81
		}
82
		$this->subscription = $subscription;
83
	}
84
85
	public function registerService(string $subscriptionService): void {
86
		if ($this->subscription !== null || $this->subscriptionService !== null) {
87
			throw new AlreadyRegisteredException();
88
		}
89
90
		$this->subscriptionService = $subscriptionService;
91
	}
92
93
94
	/**
95
	 * Fetches the list of app IDs that are supported by the subscription
96
	 *
97
	 * @since 17.0.0
98
	 */
99
	public function delegateGetSupportedApps(): array {
100
		if ($this->getSubscription() instanceof ISupportedApps) {
101
			return $this->getSubscription()->getSupportedApps();
102
		}
103
		return [];
104
	}
105
106
	/**
107
	 * Indicates if a valid subscription is available
108
	 *
109
	 * @since 17.0.0
110
	 */
111
	public function delegateHasValidSubscription(): bool {
112
		// Allow overwriting this manually for environments where the subscription information cannot be fetched
113
		if ($this->config->getSystemValueBool('has_valid_subscription')) {
114
			return true;
115
		}
116
117
		if ($this->getSubscription() instanceof ISubscription) {
0 ignored issues
show
introduced by
$this->getSubscription() is always a sub-type of OCP\Support\Subscription\ISubscription.
Loading history...
118
			return $this->getSubscription()->hasValidSubscription();
119
		}
120
		return false;
121
	}
122
123
	/**
124
	 * Indicates if the subscription has extended support
125
	 *
126
	 * @since 17.0.0
127
	 */
128
	public function delegateHasExtendedSupport(): bool {
129
		if ($this->getSubscription() instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
130
			return $this->getSubscription()->hasExtendedSupport();
131
		}
132
		return false;
133
	}
134
}
135