| Total Complexity | 48 |
| Total Lines | 554 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class AdminController extends Controller |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @Route("/admin/event/{slug}/users/add", name="adminusersadd") |
||
| 28 | * |
||
| 29 | * @Security("has_role('ROLE_ADMIN')") |
||
| 30 | * |
||
| 31 | * @param Event $event |
||
| 32 | * |
||
| 33 | * @return Response |
||
| 34 | */ |
||
| 35 | public function addUsersAction(Event $event) |
||
| 177 | ] |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Widget share contacts. |
||
| 183 | * |
||
| 184 | * @return Response |
||
| 185 | */ |
||
| 186 | public function widgetShareContactsAction() |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Show Statistic. |
||
| 199 | * |
||
| 200 | * @Route("/admin/statistic", name="admin_statistic_all") |
||
| 201 | * |
||
| 202 | * @Method({"GET", "POST"}) |
||
| 203 | * |
||
| 204 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 205 | * |
||
| 206 | * @return Response |
||
| 207 | */ |
||
| 208 | public function showStatisticAction() |
||
| 209 | { |
||
| 210 | // // беру список активних івентів через івент сервіс чи репозиторій (на морді виводиться, значить має бути готовий) |
||
| 211 | // $events = $this->getDoctrine() |
||
| 212 | // ->getRepository('StfalconEventBundle:Event') |
||
| 213 | // ->findBy(['active' => true], ['date' => 'ASC']); |
||
| 214 | |||
| 215 | // $dataForDailyStatistics = $this->ticketRepository |
||
| 216 | // ->getDataForDailyStatisticsOfTicketsSold($dateFrom, $dateTo, $event); |
||
| 217 | // array_unshift($dataForDailyStatistics, ['Date', 'Number of tickets sold']); |
||
| 218 | |||
| 219 | $repo = $this->getDoctrine() |
||
| 220 | ->getManager() |
||
| 221 | ->getRepository('ApplicationUserBundle:User'); |
||
| 222 | |||
| 223 | $totalUsersCount = $repo->getCountBaseQueryBuilder()->getQuery()->getSingleScalarResult(); |
||
| 224 | |||
| 225 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 226 | $qb->where('u.enabled = :enabled') |
||
| 227 | ->setParameter('enabled', 1); |
||
| 228 | $enabledUsersCount = $qb->getQuery()->getSingleScalarResult(); |
||
| 229 | |||
| 230 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 231 | $qb->where('u.subscribe = :subscribed') |
||
| 232 | ->setParameter('subscribed', 1); |
||
| 233 | $subscribedUsersCount = $qb->getQuery()->getSingleScalarResult(); |
||
| 234 | |||
| 235 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 236 | $qb->where('u.subscribe = :subscribed') |
||
| 237 | ->setParameter('subscribed', 0); |
||
| 238 | $unSubscribedUsersCount = $qb->getQuery()->getSingleScalarResult(); |
||
| 239 | |||
| 240 | //Кол-во людей которые не купили билеты никогда |
||
| 241 | //Кол-во людей которые купили билеты на одну \ две \ три \ четыре\ пять \ и так далее любых конференций |
||
| 242 | |||
| 243 | $usersTicketsCount = []; |
||
| 244 | |||
| 245 | $ticketRepository = $this->getDoctrine() |
||
| 246 | ->getRepository('StfalconEventBundle:Ticket'); |
||
| 247 | |||
| 248 | $paidTickets = $ticketRepository->getPaidTicketsCount(); |
||
| 249 | |||
| 250 | foreach ($paidTickets as $paidTicket) { |
||
| 251 | if (isset($usersTicketsCount[$paidTicket[1]])) { |
||
| 252 | ++$usersTicketsCount[$paidTicket[1]]; |
||
| 253 | } else { |
||
| 254 | $usersTicketsCount[$paidTicket[1]] = 1; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | $haveTickets = 0; |
||
| 259 | foreach ($usersTicketsCount as $item) { |
||
| 260 | $haveTickets += $item; |
||
| 261 | } |
||
| 262 | $usersTicketsCount[0] = $totalUsersCount - $haveTickets; |
||
| 263 | ksort($usersTicketsCount); |
||
| 264 | |||
| 265 | $ticketsByEventGroup = $ticketRepository->getTicketsCountByEventGroup(); |
||
| 266 | |||
| 267 | $countsByGroup = []; |
||
| 268 | |||
| 269 | foreach ($ticketsByEventGroup as $key => $item) { |
||
| 270 | if (isset($countsByGroup[$item['name']][$item[1]])) { |
||
| 271 | ++$countsByGroup[$item['name']][$item[1]]; |
||
| 272 | } else { |
||
| 273 | $countsByGroup[$item['name']][$item[1]] = 1; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | foreach ($countsByGroup as $key => $item) { |
||
| 277 | ksort($item); |
||
| 278 | $countsByGroup[$key] = $item; |
||
| 279 | } |
||
| 280 | //сколько людей отказалось предоставлять свои данные партнерам |
||
| 281 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 282 | $qb->where('u.allowShareContacts = :allowShareContacts'); |
||
| 283 | $qb->setParameter('allowShareContacts', 0); |
||
| 284 | $countRefusedProvideData = $qb->getQuery()->getSingleScalarResult(); |
||
| 285 | |||
| 286 | //сколько согласилось |
||
| 287 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 288 | $qb->where('u.allowShareContacts = :allowShareContacts'); |
||
| 289 | $qb->setParameter('allowShareContacts', 1); |
||
| 290 | $countAgreedProvideData = $qb->getQuery()->getSingleScalarResult(); |
||
| 291 | |||
| 292 | //сколько еще не ответило |
||
| 293 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 294 | $qb->where($qb->expr()->isNull('u.allowShareContacts')); |
||
| 295 | $countNotAnswered = $qb->getQuery()->getSingleScalarResult(); |
||
| 296 | |||
| 297 | //сколько было переходов |
||
| 298 | $qb = $repo->getCountBaseQueryBuilder(); |
||
| 299 | $qb->where($qb->expr()->isNotNull('u.userReferral')); |
||
| 300 | $countUseReferralProgram = $qb->getQuery()->getSingleScalarResult(); |
||
| 301 | |||
| 302 | $event = $this |
||
| 303 | ->getDoctrine() |
||
| 304 | ->getRepository('StfalconEventBundle:Event') |
||
| 305 | ->findOneBy([], ['date' => 'DESC']); |
||
|
|
|||
| 306 | |||
| 307 | $eventStatisticSlug = ''; |
||
| 308 | if ($event instanceof Event) { |
||
| 309 | $eventStatisticSlug = $event->getSlug(); |
||
| 310 | } |
||
| 311 | |||
| 312 | return $this->render('@ApplicationDefault/Statistic/statistic.html.twig', [ |
||
| 313 | 'admin_pool' => $this->get('sonata.admin.pool'), |
||
| 314 | 'data' => [ |
||
| 315 | 'countRefusedProvideData' => $countRefusedProvideData, |
||
| 316 | 'countAgreedProvideData' => $countAgreedProvideData, |
||
| 317 | 'countNotAnswered' => $countNotAnswered, |
||
| 318 | 'countUseReferralProgram' => $countUseReferralProgram, |
||
| 319 | 'totalUsersCount' => $totalUsersCount, |
||
| 320 | 'enabledUsersCount' => $enabledUsersCount, |
||
| 321 | 'subscribedUsersCount' => $subscribedUsersCount, |
||
| 322 | 'unSubscribedUsersCount' => $unSubscribedUsersCount, |
||
| 323 | 'haveTicketsCount' => $haveTickets, |
||
| 324 | 'usersTicketsCount' => $usersTicketsCount, |
||
| 325 | 'countsByGroup' => $countsByGroup, |
||
| 326 | 'event_statistic_slug' => $eventStatisticSlug, |
||
| 327 | ], |
||
| 328 | ]); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Start mail action. |
||
| 333 | * |
||
| 334 | * @Route("/mail/{id}/start/{value}", name="admin_start_mail") |
||
| 335 | * |
||
| 336 | * @param Request $request Request |
||
| 337 | * @param Mail $mail Mail |
||
| 338 | * @param int $value Value |
||
| 339 | * |
||
| 340 | * @return JsonResponse |
||
| 341 | */ |
||
| 342 | public function startMailAction(Request $request, Mail $mail, $value) |
||
| 343 | { |
||
| 344 | if (!$request->isXmlHttpRequest()) { |
||
| 345 | throw $this->createNotFoundException(); |
||
| 346 | } |
||
| 347 | |||
| 348 | $em = $this->getDoctrine()->getManager(); |
||
| 349 | |||
| 350 | $mail->setStart((bool) $value); |
||
| 351 | $em->persist($mail); |
||
| 352 | $em->flush(); |
||
| 353 | |||
| 354 | return new JsonResponse([ |
||
| 355 | 'status' => true, |
||
| 356 | 'value' => $value, |
||
| 357 | ]); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @ParamConverter("event", options={"mapping": {"slug": "slug"}}) |
||
| 362 | * |
||
| 363 | * @param Event $event |
||
| 364 | * |
||
| 365 | * @Route("/admin/event_statistic/{slug}", name="admin_event_statistic") |
||
| 366 | * @Route("/admin/event_statistic", name="admin_event_without_slug_statistic") |
||
| 367 | * |
||
| 368 | * @Security("has_role('ROLE_ADMIN')") |
||
| 369 | * |
||
| 370 | * @return Response |
||
| 371 | */ |
||
| 372 | public function showEventStatisticAction(Event $event) |
||
| 373 | { |
||
| 374 | $events = $this |
||
| 375 | ->getDoctrine() |
||
| 376 | ->getRepository('StfalconEventBundle:Event') |
||
| 377 | ->findBy([], ['date' => 'DESC']); |
||
| 378 | |||
| 379 | $eventStatisticHtml = $this->getEventStatistic($event); |
||
| 380 | |||
| 381 | return $this->render('@ApplicationDefault/Statistic/event_statistic_page.html.twig', [ |
||
| 382 | 'admin_pool' => $this->get('sonata.admin.pool'), |
||
| 383 | 'events' => $events, |
||
| 384 | 'event' => $event, |
||
| 385 | 'event_statistic_html' => $eventStatisticHtml, |
||
| 386 | 'current_event_slug' => $event->getSlug(), |
||
| 387 | ]); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Route("/admin/events_statistic/{checkedEvents}", name="admin_events_statistic") |
||
| 392 | * @Route("/admin/events_statistic", name="admin_events_statistic_all") |
||
| 393 | * |
||
| 394 | * @param string $checkedEvents |
||
| 395 | * |
||
| 396 | * @Security("has_role('ROLE_ADMIN')") |
||
| 397 | * |
||
| 398 | * @return Response |
||
| 399 | */ |
||
| 400 | public function showEventsStatisticAction($checkedEvents = '') |
||
| 427 | ]); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @Route("/admin/users_not_buy_tickets", name="admin_user_tickets") |
||
| 432 | * |
||
| 433 | * @param Request $request |
||
| 434 | * |
||
| 435 | * @Security("has_role('ROLE_ADMIN')") |
||
| 436 | * |
||
| 437 | * @return Response|StreamedResponse |
||
| 438 | */ |
||
| 439 | public function usersNotBuyTicketAction(Request $request) |
||
| 463 | ]); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param Event $event |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | private function getEventStatistic(Event $event) |
||
| 472 | { |
||
| 473 | $wannaVisitEvent = $event->getWantsToVisitCount(); |
||
| 474 | $ticketBlocks = $event->getTicketsCost(); |
||
| 475 | $totalTicketCount = 0; |
||
| 476 | $totalSoldTicketCount = 0; |
||
| 477 | /** @var TicketCost $ticketBlock */ |
||
| 478 | foreach ($ticketBlocks as $ticketBlock) { |
||
| 479 | $blockSold = $ticketBlock->recalculateSoldCount(); |
||
| 480 | $totalTicketCount += $ticketBlock->getCount(); |
||
| 481 | $totalSoldTicketCount += $blockSold; |
||
| 482 | } |
||
| 483 | |||
| 484 | $ticketsWithoutCostsCount = (int) $this->getDoctrine()->getRepository('StfalconEventBundle:Ticket')->getEventTicketsWithoutTicketCostCount($event); |
||
| 485 | $totalSoldTicketCount += $ticketsWithoutCostsCount; |
||
| 486 | $totalTicketCount += $ticketsWithoutCostsCount; |
||
| 487 | |||
| 488 | $html = $this->renderView('@ApplicationDefault/Statistic/event_statistic.html.twig', [ |
||
| 489 | 'wannaVisitEvent' => $wannaVisitEvent, |
||
| 490 | 'ticketBlocks' => $ticketBlocks, |
||
| 491 | 'totalTicketCount' => $totalTicketCount, |
||
| 492 | 'totalSoldTicketCount' => $totalSoldTicketCount, |
||
| 493 | 'totalTicketsWithoutCostsCount' => $ticketsWithoutCostsCount, |
||
| 494 | ]); |
||
| 495 | |||
| 496 | return $html; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param array $events |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | private function getEventsTable($events) |
||
| 505 | { |
||
| 506 | $ticketRepository = $this->getDoctrine()->getRepository('StfalconEventBundle:Ticket'); |
||
| 507 | |||
| 508 | $minGreen = 127; |
||
| 509 | $maxGreen = 255; |
||
| 510 | $deltaGreen = $maxGreen - $minGreen; |
||
| 511 | |||
| 512 | foreach ($events as $key => $event) { |
||
| 513 | if (!$event['checked']) { |
||
| 514 | continue; |
||
| 515 | } |
||
| 516 | foreach ($events as $subEvent) { |
||
| 517 | if (!$subEvent['checked']) { |
||
| 518 | continue; |
||
| 519 | } |
||
| 520 | if ($event !== $subEvent) { |
||
| 521 | $result['cnt'] = $ticketRepository->getUserVisitsEventCount($event['id'], $subEvent['id']); |
||
| 522 | if ($subEvent['cnt'] > 0) { |
||
| 523 | $result['percent'] = round($result['cnt'] * 100 / $subEvent['cnt'], 2); |
||
| 524 | } else { |
||
| 525 | $result['percent'] = 0; |
||
| 526 | } |
||
| 527 | $result['text'] = $result['cnt'].' ('.$result['percent'].' %)'; |
||
| 528 | |||
| 529 | $green = $maxGreen - round($deltaGreen * $result['percent'] / 100); |
||
| 530 | $otherColor = (int) round($green / ($maxGreen / $green)); |
||
| 531 | $otherColor = dechex($otherColor); |
||
| 532 | $result['color'] = '#'.$otherColor.dechex((int) $green).$otherColor; |
||
| 533 | } else { |
||
| 534 | $result = [ |
||
| 535 | 'cnt' => 0, |
||
| 536 | 'percent' => 0, |
||
| 537 | 'text' => '', |
||
| 538 | 'color' => '#FFFFFF', |
||
| 539 | ]; |
||
| 540 | } |
||
| 541 | $events[$key]['events'][$subEvent['slug']] = $result; |
||
| 542 | } |
||
| 543 | } |
||
| 544 | |||
| 545 | $html = $this->renderView('@ApplicationDefault/Statistic/events_statistic_table.html.twig', [ |
||
| 546 | 'events' => $events, |
||
| 547 | ]); |
||
| 548 | |||
| 549 | return $html; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param array $users |
||
| 554 | * @param string $filename |
||
| 555 | * |
||
| 556 | * @return Response |
||
| 557 | */ |
||
| 558 | private function getCsvResponse($users, $filename = 'users.csv') |
||
| 578 | } |
||
| 579 | } |
||
| 580 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.