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