| Conditions | 12 |
| Paths | 48 |
| Total Lines | 207 |
| Code Lines | 135 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 76 | public function __construct(IRequest $request, $baseUri) { |
||
| 77 | $this->request = $request; |
||
| 78 | $this->baseUri = $baseUri; |
||
| 79 | $logger = \OC::$server->getLogger(); |
||
| 80 | $dispatcher = \OC::$server->getEventDispatcher(); |
||
| 81 | |||
| 82 | $root = new RootCollection(); |
||
| 83 | $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
||
| 84 | |||
| 85 | // Add maintenance plugin |
||
| 86 | $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
||
| 87 | |||
| 88 | // Backends |
||
| 89 | $authBackend = new Auth( |
||
| 90 | \OC::$server->getSession(), |
||
| 91 | \OC::$server->getUserSession(), |
||
| 92 | \OC::$server->getRequest(), |
||
| 93 | \OC::$server->getTwoFactorAuthManager(), |
||
| 94 | \OC::$server->getBruteForceThrottler() |
||
| 95 | ); |
||
| 96 | |||
| 97 | // Set URL explicitly due to reverse-proxy situations |
||
| 98 | $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
||
| 99 | $this->server->setBaseUri($this->baseUri); |
||
| 100 | |||
| 101 | $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
||
| 102 | $authPlugin = new Plugin(); |
||
| 103 | $authPlugin->addBackend(new PublicAuth()); |
||
| 104 | $this->server->addPlugin($authPlugin); |
||
| 105 | |||
| 106 | // allow setup of additional auth backends |
||
| 107 | $event = new SabrePluginEvent($this->server); |
||
| 108 | $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
||
| 109 | |||
| 110 | $bearerAuthBackend = new BearerAuth( |
||
| 111 | \OC::$server->getUserSession(), |
||
| 112 | \OC::$server->getSession(), |
||
| 113 | \OC::$server->getRequest() |
||
| 114 | ); |
||
| 115 | $authPlugin->addBackend($bearerAuthBackend); |
||
| 116 | // because we are throwing exceptions this plugin has to be the last one |
||
| 117 | $authPlugin->addBackend($authBackend); |
||
| 118 | |||
| 119 | // debugging |
||
| 120 | if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
||
| 121 | $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
||
| 122 | } else { |
||
| 123 | $this->server->addPlugin(new DummyGetResponsePlugin()); |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
||
| 127 | $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
||
| 128 | $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
||
| 129 | |||
| 130 | // acl |
||
| 131 | $acl = new DavAclPlugin(); |
||
| 132 | $acl->principalCollectionSet = [ |
||
| 133 | 'principals/users', 'principals/groups' |
||
| 134 | ]; |
||
| 135 | $acl->defaultUsernamePath = 'principals/users'; |
||
| 136 | $this->server->addPlugin($acl); |
||
| 137 | |||
| 138 | // calendar plugins |
||
| 139 | if ($this->requestIsForSubtree(['calendars', 'principals'])) { |
||
| 140 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
||
| 141 | $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
||
| 142 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
||
| 143 | if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { |
||
| 144 | $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
||
| 145 | } |
||
| 146 | $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
||
| 147 | $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
||
| 148 | $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
||
| 149 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
||
| 150 | \OC::$server->getConfig(), |
||
| 151 | \OC::$server->getURLGenerator() |
||
| 152 | )); |
||
| 153 | } |
||
| 154 | |||
| 155 | // addressbook plugins |
||
| 156 | if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { |
||
| 157 | $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
||
| 158 | $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
||
| 159 | $this->server->addPlugin(new VCFExportPlugin()); |
||
| 160 | $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
||
| 161 | } |
||
| 162 | |||
| 163 | // system tags plugins |
||
| 164 | $this->server->addPlugin(new SystemTagPlugin( |
||
| 165 | \OC::$server->getSystemTagManager(), |
||
| 166 | \OC::$server->getGroupManager(), |
||
| 167 | \OC::$server->getUserSession() |
||
| 168 | )); |
||
| 169 | |||
| 170 | // comments plugin |
||
| 171 | $this->server->addPlugin(new CommentsPlugin( |
||
| 172 | \OC::$server->getCommentsManager(), |
||
| 173 | \OC::$server->getUserSession() |
||
| 174 | )); |
||
| 175 | |||
| 176 | $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
||
| 177 | $this->server->addPlugin(new ChunkingPlugin()); |
||
| 178 | |||
| 179 | // allow setup of additional plugins |
||
| 180 | $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
||
| 181 | |||
| 182 | // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
||
| 183 | // we do not provide locking we emulate it using a fake locking plugin. |
||
| 184 | if($request->isUserAgent([ |
||
| 185 | '/WebDAVFS/', |
||
| 186 | '/OneNote/', |
||
| 187 | '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
||
| 188 | ])) { |
||
| 189 | $this->server->addPlugin(new FakeLockerPlugin()); |
||
| 190 | } |
||
| 191 | |||
| 192 | if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
||
| 193 | $this->server->addPlugin(new BrowserErrorPagePlugin()); |
||
| 194 | } |
||
| 195 | |||
| 196 | // wait with registering these until auth is handled and the filesystem is setup |
||
| 197 | $this->server->on('beforeMethod', function () use ($root) { |
||
| 198 | // custom properties plugin must be the last one |
||
| 199 | $userSession = \OC::$server->getUserSession(); |
||
| 200 | $user = $userSession->getUser(); |
||
| 201 | if ($user !== null) { |
||
| 202 | $view = \OC\Files\Filesystem::getView(); |
||
| 203 | $this->server->addPlugin( |
||
| 204 | new FilesPlugin( |
||
| 205 | $this->server->tree, |
||
| 206 | \OC::$server->getConfig(), |
||
| 207 | $this->request, |
||
| 208 | \OC::$server->getPreviewManager(), |
||
| 209 | false, |
||
| 210 | !\OC::$server->getConfig()->getSystemValue('debug', false) |
||
| 211 | ) |
||
| 212 | ); |
||
| 213 | |||
| 214 | $this->server->addPlugin( |
||
| 215 | new \Sabre\DAV\PropertyStorage\Plugin( |
||
| 216 | new CustomPropertiesBackend( |
||
| 217 | $this->server->tree, |
||
| 218 | \OC::$server->getDatabaseConnection(), |
||
| 219 | \OC::$server->getUserSession()->getUser() |
||
| 220 | ) |
||
| 221 | ) |
||
| 222 | ); |
||
| 223 | if ($view !== null) { |
||
| 224 | $this->server->addPlugin( |
||
| 225 | new QuotaPlugin($view, false)); |
||
| 226 | } |
||
| 227 | $this->server->addPlugin( |
||
| 228 | new TagsPlugin( |
||
| 229 | $this->server->tree, \OC::$server->getTagManager() |
||
| 230 | ) |
||
| 231 | ); |
||
| 232 | // TODO: switch to LazyUserFolder |
||
| 233 | $userFolder = \OC::$server->getUserFolder(); |
||
| 234 | $this->server->addPlugin(new SharesPlugin( |
||
| 235 | $this->server->tree, |
||
| 236 | $userSession, |
||
| 237 | $userFolder, |
||
| 238 | \OC::$server->getShareManager() |
||
| 239 | )); |
||
| 240 | $this->server->addPlugin(new CommentPropertiesPlugin( |
||
| 241 | \OC::$server->getCommentsManager(), |
||
| 242 | $userSession |
||
| 243 | )); |
||
| 244 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
||
| 245 | if ($view !== null) { |
||
| 246 | $this->server->addPlugin(new FilesReportPlugin( |
||
| 247 | $this->server->tree, |
||
| 248 | $view, |
||
| 249 | \OC::$server->getSystemTagManager(), |
||
| 250 | \OC::$server->getSystemTagObjectMapper(), |
||
| 251 | \OC::$server->getTagManager(), |
||
| 252 | $userSession, |
||
| 253 | \OC::$server->getGroupManager(), |
||
| 254 | $userFolder |
||
| 255 | )); |
||
| 256 | $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( |
||
| 257 | $this->server->tree, |
||
| 258 | $user, |
||
| 259 | \OC::$server->getRootFolder(), |
||
| 260 | \OC::$server->getShareManager(), |
||
| 261 | $view |
||
| 262 | ))); |
||
| 263 | } |
||
| 264 | $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin( |
||
| 265 | \OC::$server->getConfig(), |
||
| 266 | \OC::$server->query(BirthdayService::class) |
||
| 267 | )); |
||
| 268 | } |
||
| 269 | |||
| 270 | // register plugins from apps |
||
| 271 | $pluginManager = new PluginManager( |
||
| 272 | \OC::$server, |
||
| 273 | \OC::$server->getAppManager() |
||
| 274 | ); |
||
| 275 | foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
||
| 276 | $this->server->addPlugin($appPlugin); |
||
| 277 | } |
||
| 278 | foreach ($pluginManager->getAppCollections() as $appCollection) { |
||
| 279 | $root->addChild($appCollection); |
||
| 280 | } |
||
| 281 | }); |
||
| 282 | } |
||
| 283 | |||
| 298 |