Completed
Push — master ( 4d85ff...143172 )
by Jan-Christoph
10:37
created

ServerFactory::createServer()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 100
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 68
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 100
rs 6.4589

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\DAV\Connector\Sabre;
31
32
use OC\Files\Node\Folder;
33
use OCA\DAV\Files\BrowserErrorPagePlugin;
34
use OCP\Files\Mount\IMountManager;
35
use OCP\IConfig;
36
use OCP\IDBConnection;
37
use OCP\ILogger;
38
use OCP\IPreview;
39
use OCP\IRequest;
40
use OCP\ITagManager;
41
use OCP\IUserSession;
42
use Sabre\DAV\Auth\Backend\BackendInterface;
43
44
class ServerFactory {
45
	/** @var IConfig */
46
	private $config;
47
	/** @var ILogger */
48
	private $logger;
49
	/** @var IDBConnection */
50
	private $databaseConnection;
51
	/** @var IUserSession */
52
	private $userSession;
53
	/** @var IMountManager */
54
	private $mountManager;
55
	/** @var ITagManager */
56
	private $tagManager;
57
	/** @var IRequest */
58
	private $request;
59
	/** @var IPreview  */
60
	private $previewManager;
61
62
	/**
63
	 * @param IConfig $config
64
	 * @param ILogger $logger
65
	 * @param IDBConnection $databaseConnection
66
	 * @param IUserSession $userSession
67
	 * @param IMountManager $mountManager
68
	 * @param ITagManager $tagManager
69
	 * @param IRequest $request
70
	 * @param IPreview $previewManager
71
	 */
72
	public function __construct(
73
		IConfig $config,
74
		ILogger $logger,
75
		IDBConnection $databaseConnection,
76
		IUserSession $userSession,
77
		IMountManager $mountManager,
78
		ITagManager $tagManager,
79
		IRequest $request,
80
		IPreview $previewManager
81
	) {
82
		$this->config = $config;
83
		$this->logger = $logger;
84
		$this->databaseConnection = $databaseConnection;
85
		$this->userSession = $userSession;
86
		$this->mountManager = $mountManager;
87
		$this->tagManager = $tagManager;
88
		$this->request = $request;
89
		$this->previewManager = $previewManager;
90
	}
91
92
	/**
93
	 * @param string $baseUri
94
	 * @param string $requestUri
95
	 * @param BackendInterface $authBackend
96
	 * @param callable $viewCallBack callback that should return the view for the dav endpoint
97
	 * @return Server
98
	 */
99
	public function createServer($baseUri,
100
								 $requestUri,
101
								 BackendInterface $authBackend,
102
								 callable $viewCallBack) {
103
		// Fire up server
104
		$objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
105
		$server = new \OCA\DAV\Connector\Sabre\Server($objectTree);
106
		// Set URL explicitly due to reverse-proxy situations
107
		$server->httpRequest->setUrl($requestUri);
108
		$server->setBaseUri($baseUri);
109
110
		// Load plugins
111
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config));
112
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
113
		$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
114
		// FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
115
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
116
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
117
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
118
		// Some WebDAV clients do require Class 2 WebDAV support (locking), since
119
		// we do not provide locking we emulate it using a fake locking plugin.
120
		if($this->request->isUserAgent([
121
				'/WebDAVFS/',
122
				'/Microsoft Office OneNote 2013/',
123
				'/Microsoft-WebDAV-MiniRedir/',
124
		])) {
125
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
126
		}
127
128
		if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) {
129
			$server->addPlugin(new BrowserErrorPagePlugin());
130
		}
131
132
		// wait with registering these until auth is handled and the filesystem is setup
133
		$server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
134
			// ensure the skeleton is copied
135
			$userFolder = \OC::$server->getUserFolder();
136
			
137
			/** @var \OC\Files\View $view */
138
			$view = $viewCallBack($server);
139
			if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) {
140
				$rootInfo = $userFolder;
141
			} else {
142
				$rootInfo = $view->getFileInfo('');
143
			}
144
145
			// Create ownCloud Dir
146
			if ($rootInfo->getType() === 'dir') {
147
				$root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
148
			} else {
149
				$root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
150
			}
151
			$objectTree->init($root, $view, $this->mountManager);
152
153
			$server->addPlugin(
154
				new \OCA\DAV\Connector\Sabre\FilesPlugin(
155
					$objectTree,
156
					$view,
157
					$this->config,
158
					$this->request,
159
					$this->previewManager,
160
					false,
161
					!$this->config->getSystemValue('debug', false)
162
				)
163
			);
164
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view));
165
166
			if($this->userSession->isLoggedIn()) {
167
				$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
168
				$server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
169
					$objectTree,
170
					$this->userSession,
171
					$userFolder,
172
					\OC::$server->getShareManager()
173
				));
174
				$server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
175
				$server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
176
					$objectTree,
177
					$view,
178
					\OC::$server->getSystemTagManager(),
179
					\OC::$server->getSystemTagObjectMapper(),
180
					$this->userSession,
181
					\OC::$server->getGroupManager(),
182
					$userFolder
183
				));
184
				// custom properties plugin must be the last one
185
				$server->addPlugin(
186
					new \Sabre\DAV\PropertyStorage\Plugin(
187
						new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
188
							$objectTree,
189
							$this->databaseConnection,
190
							$this->userSession->getUser()
191
						)
192
					)
193
				);
194
			}
195
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
196
		}, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
197
		return $server;
198
	}
199
}
200