| Conditions | 28 |
| Paths | 5232 |
| Total Lines | 219 |
| Code Lines | 137 |
| 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 |
||
| 159 | public function createResource(Request $request, $fileType = 'file') |
||
| 160 | { |
||
| 161 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 162 | |||
| 163 | $this->isGrantedOr403($configuration, ResourceActions::CREATE); |
||
| 164 | /** @var CDocument $newResource */ |
||
| 165 | $newResource = $this->newResourceFactory->create($configuration, $this->factory); |
||
| 166 | $form = $this->resourceFormFactory->create($configuration, $newResource); |
||
| 167 | |||
| 168 | $course = $this->getCourse(); |
||
| 169 | $session = $this->getSession(); |
||
| 170 | $newResource->setCourse($course); |
||
| 171 | $newResource->c_id = $course->getId(); |
||
| 172 | $newResource->setFiletype($fileType); |
||
| 173 | $form->setData($newResource); |
||
| 174 | |||
| 175 | $parentId = $request->get('parent'); |
||
| 176 | $parent = null; |
||
| 177 | if (!empty($parentId)) { |
||
| 178 | /** @var CDocument $parent */ |
||
| 179 | $parent = $this->repository->find($parentId); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { |
||
| 183 | /** @var CDocument $newResource */ |
||
| 184 | $newResource = $form->getData(); |
||
| 185 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 186 | |||
| 187 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
| 188 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
| 189 | } |
||
| 190 | if ($event->isStopped()) { |
||
| 191 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
| 192 | |||
| 193 | if ($event->hasResponse()) { |
||
| 194 | return $event->getResponse(); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->redirectHandler->redirectToIndex($configuration, $newResource); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($configuration->hasStateMachine()) { |
||
| 201 | $this->stateMachine->apply($configuration, $newResource); |
||
| 202 | } |
||
| 203 | |||
| 204 | //$sharedType = $form->get('shared')->getData(); |
||
| 205 | $shareList = []; |
||
| 206 | $sharedType = 'this_course'; |
||
| 207 | |||
| 208 | switch ($sharedType) { |
||
| 209 | case 'this_course': |
||
| 210 | if (empty($course)) { |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | // Default Chamilo behaviour: |
||
| 214 | // Teachers can edit and students can see |
||
| 215 | $shareList = [ |
||
| 216 | [ |
||
| 217 | 'sharing' => 'course', |
||
| 218 | 'mask' => ResourceNodeVoter::getReaderMask(), |
||
| 219 | 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT, |
||
| 220 | 'search' => $course->getId(), |
||
| 221 | ], |
||
| 222 | [ |
||
| 223 | 'sharing' => 'course', |
||
| 224 | 'mask' => ResourceNodeVoter::getEditorMask(), |
||
| 225 | 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER, |
||
| 226 | 'search' => $course->getId(), |
||
| 227 | ], |
||
| 228 | ]; |
||
| 229 | break; |
||
| 230 | case 'shared': |
||
| 231 | $shareList = $form->get('rights')->getData(); |
||
| 232 | break; |
||
| 233 | case 'only_me': |
||
| 234 | $shareList = [ |
||
| 235 | [ |
||
| 236 | 'sharing' => 'user', |
||
| 237 | 'only_me' => true, |
||
| 238 | ], |
||
| 239 | ]; |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | |||
| 243 | $resourceNode = $repository->addResourceNode($newResource, $this->getUser(), $parent); |
||
| 244 | |||
| 245 | // Loops all sharing options |
||
| 246 | foreach ($shareList as $share) { |
||
| 247 | $idList = []; |
||
| 248 | if (isset($share['search'])) { |
||
| 249 | $idList = explode(',', $share['search']); |
||
| 250 | } |
||
| 251 | |||
| 252 | $resourceRight = null; |
||
| 253 | if (isset($share['mask'])) { |
||
| 254 | $resourceRight = new ResourceRight(); |
||
| 255 | $resourceRight |
||
| 256 | ->setMask($share['mask']) |
||
| 257 | ->setRole($share['role']) |
||
| 258 | ; |
||
| 259 | } |
||
| 260 | |||
| 261 | // Build links |
||
| 262 | switch ($share['sharing']) { |
||
| 263 | case 'everyone': |
||
| 264 | $repository->addResourceToEveryone( |
||
| 265 | $resourceNode, |
||
| 266 | $resourceRight |
||
| 267 | ); |
||
| 268 | break; |
||
| 269 | case 'course': |
||
| 270 | $repository->addResourceToCourse( |
||
| 271 | $resourceNode, |
||
| 272 | $course, |
||
| 273 | $resourceRight |
||
| 274 | ); |
||
| 275 | break; |
||
| 276 | case 'session': |
||
| 277 | $repository->addResourceToSession( |
||
| 278 | $resourceNode, |
||
| 279 | $course, |
||
| 280 | $session, |
||
| 281 | $resourceRight |
||
| 282 | ); |
||
| 283 | break; |
||
| 284 | case 'user': |
||
| 285 | // Only for me |
||
| 286 | if (isset($share['only_me'])) { |
||
| 287 | $repository->addResourceOnlyToMe($resourceNode); |
||
| 288 | } else { |
||
| 289 | // To other users |
||
| 290 | $repository->addResourceToUserList($resourceNode, $idList); |
||
| 291 | } |
||
| 292 | break; |
||
| 293 | case 'group': |
||
| 294 | // @todo |
||
| 295 | break; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | $newResource |
||
| 300 | ->setCourse($course) |
||
| 301 | ->setFiletype($fileType) |
||
| 302 | ->setSession($session) |
||
| 303 | //->setTitle($title) |
||
| 304 | //->setComment($comment) |
||
| 305 | ->setReadonly(false) |
||
| 306 | ->setResourceNode($resourceNode) |
||
| 307 | ; |
||
| 308 | |||
| 309 | $path = \URLify::filter($newResource->getTitle()); |
||
| 310 | |||
| 311 | switch ($fileType) { |
||
| 312 | case 'folder': |
||
| 313 | $newResource |
||
| 314 | ->setPath($path) |
||
| 315 | ->setSize(0) |
||
| 316 | ; |
||
| 317 | break; |
||
| 318 | case 'file': |
||
| 319 | $newResource |
||
| 320 | ->setPath($path) |
||
| 321 | ->setSize(0) |
||
| 322 | ; |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | |||
| 326 | $this->repository->add($newResource); |
||
| 327 | $postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 328 | |||
| 329 | $newResource->setId($newResource->getIid()); |
||
| 330 | $this->getDoctrine()->getManager()->persist($newResource); |
||
| 331 | $this->getDoctrine()->getManager()->flush(); |
||
| 332 | |||
| 333 | if (!$configuration->isHtmlRequest()) { |
||
| 334 | return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED)); |
||
| 335 | } |
||
| 336 | |||
| 337 | $this->addFlash('success', 'saved'); |
||
| 338 | |||
| 339 | //$this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource); |
||
| 340 | if ($postEvent->hasResponse()) { |
||
| 341 | return $postEvent->getResponse(); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $this->redirectToRoute( |
||
| 345 | 'app_document_show', |
||
| 346 | [ |
||
| 347 | 'id' => $newResource->getIid(), |
||
| 348 | 'course' => $course->getCode(), |
||
| 349 | 'parent_id' => $parentId, |
||
| 350 | ] |
||
| 351 | ); |
||
| 352 | //return $this->redirectHandler->redirectToResource($configuration, $newResource); |
||
| 353 | } |
||
| 354 | |||
| 355 | if (!$configuration->isHtmlRequest()) { |
||
| 356 | return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
||
| 357 | } |
||
| 358 | |||
| 359 | $initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 360 | if ($initializeEvent->hasResponse()) { |
||
| 361 | return $initializeEvent->getResponse(); |
||
| 362 | } |
||
| 363 | |||
| 364 | $view = View::create() |
||
| 365 | ->setData([ |
||
| 366 | 'configuration' => $configuration, |
||
| 367 | 'metadata' => $this->metadata, |
||
| 368 | 'resource' => $newResource, |
||
| 369 | $this->metadata->getName() => $newResource, |
||
| 370 | 'form' => $form->createView(), |
||
| 371 | 'parent_id' => $parentId, |
||
| 372 | 'file_type' => $fileType, |
||
| 373 | ]) |
||
| 374 | ->setTemplate($configuration->getTemplate(ResourceActions::CREATE.'.html')) |
||
| 375 | ; |
||
| 376 | |||
| 377 | return $this->viewHandler->handle($configuration, $view); |
||
| 378 | } |
||
| 619 |