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