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