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