Completed
Push — master ( dccb89...31024b )
by Morris
12:39
created

ProviderFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OC\Share20;
25
26
use OCA\FederatedFileSharing\AddressHandler;
27
use OCA\FederatedFileSharing\DiscoveryManager;
28
use OCA\FederatedFileSharing\FederatedShareProvider;
29
use OCA\FederatedFileSharing\Notifications;
30
use OCA\FederatedFileSharing\TokenHandler;
31
use OCA\ShareByMail\Settings\SettingsManager;
32
use OCA\ShareByMail\ShareByMailProvider;
33
use OCP\Share\IProviderFactory;
34
use OC\Share20\Exception\ProviderException;
35
use OCP\IServerContainer;
36
37
/**
38
 * Class ProviderFactory
39
 *
40
 * @package OC\Share20
41
 */
42
class ProviderFactory implements IProviderFactory {
43
44
	/** @var IServerContainer */
45
	private $serverContainer;
46
	/** @var DefaultShareProvider */
47
	private $defaultProvider = null;
48
	/** @var FederatedShareProvider */
49
	private $federatedProvider = null;
50
	/** @var  ShareByMailProvider */
51
	private $shareByMailProvider;
52
	/** @var  \OCA\Circles\ShareByCircleProvider;
53
	 * ShareByCircleProvider */
54
	private $shareByCircleProvider;
55
56
	/**
57
	 * IProviderFactory constructor.
58
	 *
59
	 * @param IServerContainer $serverContainer
60
	 */
61
	public function __construct(IServerContainer $serverContainer) {
62
		$this->serverContainer = $serverContainer;
63
	}
64
65
	/**
66
	 * Create the default share provider.
67
	 *
68
	 * @return DefaultShareProvider
69
	 */
70
	protected function defaultShareProvider() {
71
		if ($this->defaultProvider === null) {
72
			$this->defaultProvider = new DefaultShareProvider(
73
				$this->serverContainer->getDatabaseConnection(),
74
				$this->serverContainer->getUserManager(),
75
				$this->serverContainer->getGroupManager(),
76
				$this->serverContainer->getLazyRootFolder()
77
			);
78
		}
79
80
		return $this->defaultProvider;
81
	}
82
83
	/**
84
	 * Create the federated share provider
85
	 *
86
	 * @return FederatedShareProvider
87
	 */
88
	protected function federatedShareProvider() {
89
		if ($this->federatedProvider === null) {
90
			/*
91
			 * Check if the app is enabled
92
			 */
93
			$appManager = $this->serverContainer->getAppManager();
94
			if (!$appManager->isEnabledForUser('federatedfilesharing')) {
95
				return null;
96
			}
97
98
			/*
99
			 * TODO: add factory to federated sharing app
100
			 */
101
			$l = $this->serverContainer->getL10N('federatedfilessharing');
102
			$addressHandler = new AddressHandler(
103
				$this->serverContainer->getURLGenerator(),
104
				$l,
105
				$this->serverContainer->getCloudIdManager()
106
			);
107
			$notifications = new Notifications(
108
				$addressHandler,
109
				$this->serverContainer->getHTTPClientService(),
110
				$this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
111
				$this->serverContainer->getJobList()
112
			);
113
			$tokenHandler = new TokenHandler(
114
				$this->serverContainer->getSecureRandom()
115
			);
116
117
			$this->federatedProvider = new FederatedShareProvider(
118
				$this->serverContainer->getDatabaseConnection(),
119
				$addressHandler,
120
				$notifications,
121
				$tokenHandler,
122
				$l,
123
				$this->serverContainer->getLogger(),
124
				$this->serverContainer->getLazyRootFolder(),
125
				$this->serverContainer->getConfig(),
126
				$this->serverContainer->getUserManager(),
127
				$this->serverContainer->getCloudIdManager()
128
			);
129
		}
130
131
		return $this->federatedProvider;
132
	}
133
134
	/**
135
	 * Create the federated share provider
136
	 *
137
	 * @return ShareByMailProvider
138
	 */
139
	protected function getShareByMailProvider() {
140
		if ($this->shareByMailProvider === null) {
141
			/*
142
			 * Check if the app is enabled
143
			 */
144
			$appManager = $this->serverContainer->getAppManager();
145
			if (!$appManager->isEnabledForUser('sharebymail')) {
146
				return null;
147
			}
148
149
			$settingsManager = new SettingsManager($this->serverContainer->getConfig());
150
151
			$this->shareByMailProvider = new ShareByMailProvider(
152
				$this->serverContainer->getDatabaseConnection(),
153
				$this->serverContainer->getSecureRandom(),
154
				$this->serverContainer->getUserManager(),
155
				$this->serverContainer->getLazyRootFolder(),
156
				$this->serverContainer->getL10N('sharebymail'),
157
				$this->serverContainer->getLogger(),
158
				$this->serverContainer->getMailer(),
159
				$this->serverContainer->getURLGenerator(),
160
				$this->serverContainer->getActivityManager(),
161
				$settingsManager
162
			);
163
		}
164
165
		return $this->shareByMailProvider;
166
	}
167
168
169
	/**
170
	 * Create the circle share provider
171
	 *
172
	 * @return FederatedShareProvider
173
	 */
174
	protected function getShareByCircleProvider() {
175
176
		$appManager = $this->serverContainer->getAppManager();
177
		if (!$appManager->isEnabledForUser('circles')) {
178
			return null;
179
		}
180
181
182
		if ($this->shareByCircleProvider === null) {
183
184
			$this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
185
				$this->serverContainer->getDatabaseConnection(),
186
				$this->serverContainer->getSecureRandom(),
187
				$this->serverContainer->getUserManager(),
188
				$this->serverContainer->getLazyRootFolder(),
189
				$this->serverContainer->getL10N('circles'),
190
				$this->serverContainer->getLogger(),
191
				$this->serverContainer->getURLGenerator()
192
			);
193
		}
194
195
		return $this->shareByCircleProvider;
196
	}
197
198
199
	/**
200
	 * @inheritdoc
201
	 */
202
	public function getProvider($id) {
203
		$provider = null;
204
		if ($id === 'ocinternal') {
205
			$provider = $this->defaultShareProvider();
206
		} else if ($id === 'ocFederatedSharing') {
207
			$provider = $this->federatedShareProvider();
208
		} else if ($id === 'ocMailShare') {
209
			$provider = $this->getShareByMailProvider();
210
		} else if ($id === 'ocCircleShare') {
211
			$provider = $this->getShareByCircleProvider();
212
		}
213
214
		if ($provider === null) {
215
			throw new ProviderException('No provider with id .' . $id . ' found.');
216
		}
217
218
		return $provider;
219
	}
220
221
	/**
222
	 * @inheritdoc
223
	 */
224
	public function getProviderForType($shareType) {
225
		$provider = null;
226
227
		if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
228
			$shareType === \OCP\Share::SHARE_TYPE_GROUP ||
229
			$shareType === \OCP\Share::SHARE_TYPE_LINK
230
		) {
231
			$provider = $this->defaultShareProvider();
232
		} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
233
			$provider = $this->federatedShareProvider();
234
		} else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
235
			$provider = $this->getShareByMailProvider();
236
		} else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
237
			$provider = $this->getShareByCircleProvider();
238
		}
239
240
241
		if ($provider === null) {
242
			throw new ProviderException('No share provider for share type ' . $shareType);
243
		}
244
245
		return $provider;
246
	}
247
248
	public function getAllProviders() {
249
		$shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
250
		$shareByMail = $this->getShareByMailProvider();
251
		if ($shareByMail !== null) {
252
			$shares[] = $shareByMail;
253
		}
254
		$shareByCircle = $this->getShareByCircleProvider();
255
		if ($shareByCircle !== null) {
256
			$shares[] = $shareByCircle;
257
		}
258
259
		return $shares;
0 ignored issues
show
Best Practice introduced by
The expression return $shares; seems to be an array, but some of its elements' types (null) are incompatible with the return type declared by the interface OCP\Share\IProviderFactory::getAllProviders of type OCP\Share\IShareProvider[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
260
	}
261
}
262