src/Controller/Api/PlayerController.php line 317

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Convocatoria;
  4. use App\Entity\ConvocatoriaPlayer;
  5. use App\Entity\Customer;
  6. use App\Entity\Player;
  7. use App\Entity\Season;
  8. use App\Entity\Game;
  9. use App\Entity\GameAlignment;
  10. use App\Entity\PlayerSportsInjuries;
  11. use App\Entity\SeasonPlayer;
  12. use App\Entity\GamePlayerStatistics;
  13. use App\Entity\TrainingAsist;
  14. use App\Helper\UploadedBase64File;
  15. use App\Repository\ConvocatoriaRepository;
  16. use App\Repository\PlayerRepository;
  17. use App\Services\Api\PlayerManager;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Psr\Log\LoggerInterface;
  24. use App\Entity\PlayerWeight;
  25. use DateTime;
  26. class PlayerController extends ApiController
  27. {
  28.     /** @var PlayerManager $playerManager */
  29.     protected $playerManager;
  30.     /**
  31.      * @param PlayerManager $playerManager
  32.      */
  33.     public function __construct(EntityManagerInterface $emParameterBagInterface $parameterBagPlayerManager $playerManager)
  34.     {
  35.         parent::__construct($em$parameterBag);
  36.         $this->playerManager $playerManager;
  37.     }
  38.     /**
  39.      * @param Request $request
  40.      * @return JsonResponse
  41.      *
  42.      * @Route("/player/listbygame/{id}", name="api_player_list_by_game", methods={"POST"})
  43.      */
  44.     public function listPlayerByGame($idRequest $request)
  45.     {
  46.         $prefixFile $this->getParameter("app.path.player_images");
  47.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  48.         $request $this->transformJsonBody($request);
  49.         $players = [];
  50.         /** @var PlayerRepository $playerRepository */
  51.         $playerRepository $this->em->getRepository(Player::class);
  52.         /** @var Customer $customer */
  53.         $customer $this->getUser();
  54.         $season $customer->getSeasonActive();
  55.         if (!empty($season)) {
  56.             $filter = [
  57.                 "season" => $season->getId()
  58.             ];
  59.             $sort $request->get('sort');
  60.             if (empty($sort)) $sort 'dorsal';
  61.             $direction $request->get('direction');
  62.             if (empty($direction)) $direction 'ASC';
  63.             /** @var Game $game */
  64.             $game $this->em->getRepository(Game::class)->find($id);
  65.             $convocatoria $game->getConvocatoria();
  66.             $convocatoriaPlayerRepository $this->em->getRepository(ConvocatoriaPlayer::class);
  67.              /** @var GameAlignment $gameAlignment */
  68.              $captain $game->getAlignment() && $game->getAlignment()->getCaptain()? $game->getAlignment()->getCaptain()->getPlayer()->getId() : null;
  69.             /** @var SeasonPlayer $seasonPlayer */
  70.             /** @var Player $player */
  71.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter, [$sort => $direction]) as $seasonPlayer) {
  72.                 $player $seasonPlayer->getPlayer();
  73.                 $playerArr $player->__toArray($baseurl $prefixFile "/");
  74.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  75.                 $isActive $player->getIsActive();
  76.                 if ($isActive && $seasonPlayerActive) {
  77.                     $criteria = [
  78.                         "seasonPlayer" => $seasonPlayer,
  79.                         "game" => $game
  80.                     ];
  81.                     $miscellaneous $playerRepository->getStatsMiscellaneousByGame($player$game);
  82.                     $isTitular $miscellaneous['isTitular'] ? true false;
  83.                     $isCaptain $captain && ($captain == $player->getId());
  84.                     $out array_merge($miscellaneous,[
  85.                         "dorsal" => $seasonPlayer->getDorsal(),
  86.                         "isTitular" => $isTitular,
  87.                         "isCaptain" => $isCaptain,
  88.                         "countryId" => !empty($player->getCountry()) ? $player->getCountry()->getId() : null,
  89.                     ]);
  90.                     if ($convocatoria) {
  91.                         $convocatoriaPlayer $convocatoriaPlayerRepository->findOneBy([
  92.                             "player_id" => $player->getId(),
  93.                             "convocatoria_id" => $convocatoria->getId(),
  94.                         ]);
  95.                         if ($convocatoriaPlayer) {
  96.                             $out array_merge($out, [
  97.                                 "isOn" => $convocatoriaPlayer->getIsActive(),
  98.                             ], $convocatoriaPlayer->__toArray());
  99.                         } else {
  100.                             $out array_merge($out, [
  101.                                 "isOn" => false,
  102.                                 "justification_type" => null,
  103.                                 "is_active" => null,
  104.                             ]);
  105.                         }
  106.                     } else {
  107.                         $out array_merge($out, [
  108.                             "isOn" => false,
  109.                             "justification_type" => null,
  110.                             "is_active" => null,
  111.                         ]);
  112.                     }
  113.                     if ($out['isOn']) {
  114.                         $players[] = array_merge($playerArr$out);
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.         return $this->response($players);
  120.     }
  121.     /**
  122.      * @param Request $request
  123.      * @return JsonResponse
  124.      *
  125.      * @Route("/player/list", name="api_player_list", methods={"POST"})
  126.      */
  127.     public function listPlayer(Request $request)
  128.     {
  129.         $request $this->transformJsonBody($request);
  130.         $players = [];
  131.         /** @var Customer $customer */
  132.         $customer $this->getUser();
  133.         $season $customer->getSeasonActive();
  134.         if (!empty($season)) {
  135.             $filter = [
  136.                 "season" => $season->getId()
  137.             ];
  138.             $sort $request->get('sort');
  139.             if (empty($sort)) $sort 'dorsal';
  140.             $direction $request->get('direction');
  141.             if (empty($direction)) $direction 'ASC';
  142.             /** @var Player $player */
  143.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter, [$sort => $direction]) as $seasonPlayer) {
  144.                 /** @var SeasonPlayer $seasonPlayer */
  145.                 $player $seasonPlayer->getPlayer();
  146.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  147.                 $isActive $player->getIsActive();
  148.                 if ($isActive && !$player->getDeletedAt() && $seasonPlayerActive) {
  149.                     $players[] = $this->getDataPlayer($request$player$season);
  150.                 }
  151.             }
  152.         }
  153.         return $this->response($players);
  154.     }
  155.         /**
  156.      * @param Request $request
  157.      * @return JsonResponse
  158.      *
  159.      * @Route("/player/list/recycling", name="api_player_list_recycling", methods={"POST"})
  160.      */
  161.     public function listPlayerRecycling(Request $request)
  162.     {
  163.         $request $this->transformJsonBody($request);
  164.         $players = [];
  165.         /** @var Customer $customer */
  166.         $customer $this->getUser();
  167.         $season $customer->getSeasonActive();
  168.         if (!empty($season)) {
  169.             $filter = [
  170.                 "season" => $season->getId()
  171.             ];
  172.             $sort $request->get('sort');
  173.             if (empty($sort)) $sort 'dorsal';
  174.             $direction $request->get('direction');
  175.             if (empty($direction)) $direction 'ASC';
  176.             /** @var Player $player */
  177.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter, [$sort => $direction]) as $seasonPlayer) {
  178.                 /** @var SeasonPlayer $seasonPlayer */
  179.                 $player $seasonPlayer->getPlayer();
  180.                 $seasonPlayerActive $seasonPlayer->getIsActive();
  181.                 $seasonPlayerDeletedAt $seasonPlayer->getDeletedAt();
  182.                 if ($player->getDeletedAt() && $player->getIsActive() && $seasonPlayerActive && $seasonPlayerDeletedAt) {
  183.                     $players[] = $this->getDataPlayer($request$player$season);
  184.                 }
  185.             }
  186.         }
  187.         return $this->response($players);
  188.     }
  189.     public function getDataPlayer(Request $requestPlayer $playerSeason $season)
  190.     {
  191.         $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->findOneBy(['player' => $player"season" => $season]);
  192.         $weights = [];
  193.         foreach ($this->em->getRepository(PlayerWeight::class)->findBy([
  194.             'seasonPlayer' => $seasonPlayer
  195.         ], ['date' => 'ASC']) as $key => $weight) {
  196.             $weights[] = $weight->__toArray();
  197.         }
  198.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  199.         $prefixFile $this->getParameter("app.path.player_images");
  200.         /** @var PlayerRepository $playerRepository */
  201.         $playerRepository $this->em->getRepository(Player::class);
  202.         $playerArr $player->__toArray($baseurl $prefixFile "/");
  203.         $playerArr['weights'] = $weights;
  204.         $playerArr['weight'] = count($weights)  ?   $weights[0]['weight'] : $playerArr['weight'];
  205.         // add weight if is empty table PlayerWeight player
  206.         if (!count($weights) && $playerArr['weight'] != 0.00) {
  207.             $playerWeight = new PlayerWeight();
  208.             $playerWeight->setSeasonPlayer($seasonPlayer);
  209.             $playerWeight->setWeight($playerArr['weight']);
  210.             $playerWeight->setDate(new DateTime());
  211.             $playerWeight->setTime(new DateTime());
  212.             $this->em->persist($playerWeight);
  213.             $this->em->flush();
  214.             $playerArr['weights'] = $playerWeight->__toArray();
  215.         }
  216.         $miscellaneous $playerRepository->getStatsMiscellaneousAllPlayers($player$season);
  217.         $trainingAsist $playerRepository->getStatsAsist($player$season);
  218.         $result $playerRepository->getStatsConvocatoria($player$season);
  219.         $newData array_merge($playerArr$miscellaneous);
  220.         $newData array_merge($trainingAsist$newData);
  221.         $newData array_merge($result$newData);
  222.         return array_merge($newData, [
  223.             "dorsal" => $seasonPlayer->getDorsal(),
  224.             "countryId" => !empty($player->getCountry()) ? $player->getCountry()->getId() : null,
  225.             "seasonsPlayer" => $player->getSeasonPlayers()->map(function (SeasonPlayer $seasonPlayer) use ($playerRepository$player$request) {
  226.                 $season $seasonPlayer->getSeason();
  227.                 $miscellaneous $playerRepository->getStatsMiscellaneousAllPlayers($player$season);
  228.                 $imageTeam $season->getTeam()->getImage();
  229.                 $prefixFile $this->getParameter("app.path.customer_images");
  230.                 $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  231.                 $imagePath = !empty($imageTeam) ? $baseurl $prefixFile "/" $imageTeam "";
  232.                 $miscellaneous['imageTeam'] = $imagePath;
  233.                 return array_merge($seasonPlayer->__toArray(), $miscellaneous);
  234.             })->toArray(),
  235.         ]);
  236.     }
  237.     /**
  238.      * @param Request $request
  239.      * @return JsonResponse
  240.      *
  241.      * @Route("/player/add", name="api_player_add", methods={"POST"})
  242.      */
  243.     public function addPlayer(Request $request)
  244.     {
  245.         $request $this->transformJsonBody($request);
  246.         $data $this->getDataFromRequest($request);
  247.         /** @var Customer $customer */
  248.         $customer $this->getUser();
  249.         $season $customer->getSeasonActive();
  250.         $response $this->playerManager->add($data$request);
  251.         $this->setStatusCode($response["code"]);
  252.         if ($response['code'] == 200) {
  253.             $response['player'] = $this->getDataPlayer($request$response['player'], $season);
  254.         };
  255.         return $this->response($response);
  256.     }
  257.     /**
  258.      * @param Request $request
  259.      * @return JsonResponse
  260.      *
  261.      * @Route("/players/all", name="api_players_all", methods={"POST"})
  262.      */
  263.     public function allPlayers(Request $request)
  264.     {
  265.         $customer $this->getUser();
  266.         $response $this->playerManager->allPlayersByUserId($customer);
  267.         return $this->response($response);
  268.     }
  269.     /**
  270.      * @param Request $request
  271.      * @return JsonResponse
  272.      *
  273.      * @Route("/players/migrate/{seasonId}", name="api_players_migrate", methods={"POST"})
  274.      */
  275.     public function migratePlayer($seasonIdRequest $request)
  276.     {
  277.         $customer $this->getUser();
  278.         $season $customer->getSeasonActive();
  279.         $players json_decode($request->get('players'"[]"), true);
  280.         $seasonRepository $this->em->getRepository(Season::class);
  281.         /** @var Season $seasonTarget */
  282.         $seasonTarget $seasonRepository->findOneBy(["id" => $seasonId"customer" => $customer->getId(), "isActive" => true]);
  283.         $response $this->playerManager->addPlayerToSeason($customer$season$players$seasonTarget);
  284.         $this->setStatusCode($response["code"]);
  285.         return $this->response($response);
  286.     }
  287.     /**
  288.      * @param Request $request
  289.      * @return JsonResponse
  290.      *
  291.      * @Route("/players/migrate/undo/{seasonId}", name="api_players_migrate_undo", methods={"POST"})
  292.      */
  293.     public function undoMigratePlayer($seasonIdRequest $request)
  294.     {
  295.         $customer $this->getUser();
  296.         $season $customer->getSeasonActive();
  297.         $players json_decode($request->get('players'"[]"), true);
  298.         $seasonRepository $this->em->getRepository(Season::class);
  299.         /** @var Season $seasonTarget */
  300.         $seasonTarget $seasonRepository->findOneBy(["id" => $seasonId"customer" => $customer->getId(), "isActive" => true]);
  301.         $response $this->playerManager->moveToBinPlayerFromSeason($customer$season$players$seasonTarget);
  302.         $this->setStatusCode($response["code"]);
  303.         return $this->response($response);
  304.     }
  305.     /**
  306.      * @param Request $request
  307.      * @return JsonResponse
  308.      *
  309.      * @Route("/players/migrate/remove/{seasonId}", name="api_players_migrate_remove", methods={"POST"})
  310.      */
  311.     public function removeMigratePlayer($seasonIdRequest $request)
  312.     {
  313.         $customer $this->getUser();
  314.         $season $customer->getSeasonActive();
  315.         $players json_decode($request->get('players'"[]"), true);
  316.         $seasonRepository $this->em->getRepository(Season::class);
  317.         /** @var Season $seasonTarget */
  318.         $seasonTarget $seasonRepository->findOneBy(["id" => $seasonId"customer" => $customer->getId(), "isActive" => true]);
  319.         $response $this->playerManager->removePlayerFromSeason($customer$season$players$seasonTarget);
  320.         $this->setStatusCode($response["code"]);
  321.         return $this->response($response);
  322.     }
  323.     /**
  324.      * @param Request $request
  325.      * @return JsonResponse
  326.      *
  327.      * @Route("/players/migrate/rollback/{seasonId}", name="api_players_migrate_rollback", methods={"POST"})
  328.      */
  329.     public function rollbackMigratePlayer($seasonIdRequest $request)
  330.     {
  331.         $customer $this->getUser();
  332.         $season $customer->getSeasonActive();
  333.         $players json_decode($request->get('players'"[]"), true);
  334.         $seasonRepository $this->em->getRepository(Season::class);
  335.         /** @var Season $seasonTarget */
  336.         $seasonTarget $seasonRepository->findOneBy(["id" => $seasonId"customer" => $customer->getId(), "isActive" => true]);
  337.         $response $this->playerManager->removePlayerFromSeason($customer$season$players$seasonTarget);
  338.         $this->setStatusCode($response["code"]);
  339.         return $this->response($response);
  340.     }
  341.     /**
  342.      * @param Request $request
  343.      * @return JsonResponse
  344.      *
  345.      * @Route("/player/edit/{player}", name="api_player_edit", methods={"POST"})
  346.      */
  347.     public function editPlayer(Player $playerRequest $requestLoggerInterface $logger)
  348.     {
  349.         $request $this->transformJsonBody($request);
  350.         $data $this->getDataFromRequest($request);
  351.         $response $this->playerManager->edit($player$data);
  352.         $logger->info("DATA " json_encode($data));
  353.         $this->setStatusCode($response["code"]);
  354.         return $this->response($response);
  355.     }
  356.     /**
  357.      * @param Request $request
  358.      * @return JsonResponse 
  359.      *
  360.      * @Route("/player/editstats/{player}", name="api_player_editstats", methods={"POST"})
  361.      */
  362.     public function editStats(Player $playerRequest $requestLoggerInterface $logger)
  363.     {
  364.         $data = [
  365.             "gameId" => $request->get('gameId'),
  366.             "minutes" => $request->get('minutes'),
  367.             "yellowCards" => $request->get('yellowCards'),
  368.             "redCards" => $request->get('redCards'),
  369.             "goals" => $request->get('goals'),
  370.             "goalAssistances" => $request->get('goalAssistances'),
  371.             "isTitular" => $request->get('isTitular'),
  372.             "kilometersTraveled" => $request->get('kilometersTraveled'),
  373.             "shotsOnGoal" => $request->get('shotsOnGoal'),
  374.             "stolenBalls" => $request->get('stolenBalls'),
  375.             "penaltiesCommitted" => $request->get('penaltiesCommitted'),
  376.             "forcedPenalties" => $request->get('forcedPenalties'),
  377.             "stopsMade" => $request->get('stopsMade'),
  378.         ];
  379.         /** @var Customer $customer */
  380.         $customer $this->getUser();
  381.         $season $customer->getSeasonActive();
  382.         $response $this->playerManager->updateStats($player$datafalse);
  383.         $this->setStatusCode($response["code"]);
  384.         if ($response['code'] == 200) {
  385.             $response['player'] = $this->getDataPlayer($request$response['player'], $season);
  386.         };
  387.         return $this->response($response);
  388.     }
  389.     protected function getDataFromRequest(Request $request)
  390.     {
  391.         $data = [
  392.             "name" => $request->get('name'),
  393.             "lastname" => $request->get('lastname'),
  394.             "position" => $request->get('position'),
  395.             "secondary_position" => $request->get('secondaryPosition'),
  396.             "age" => (int) $request->get('age'),
  397.             "weight" => (float) $request->get('weight'),
  398.             "height" => (float) $request->get('height'),
  399.             "shoes" => (int) $request->get('shoes'),
  400.             "games" => (int) $request->get('games'),
  401.             "goals" => (int) $request->get('goals'),
  402.             "yellowCard" => (int) $request->get('yellowCard'),
  403.             "redCard" => (int) $request->get('redCard'),
  404.             "dorsal" => (int) $request->get('dorsal'),
  405.             "skillfulLeg" => (int) $request->get('skillfulLeg'),
  406.             "countryId" => (int) $request->get('countryId'),
  407.             "phone" => $request->get('phone') ?? NULL,
  408.             "email" => $request->get('email') ?? NULL,
  409.             "numberDocument" => $request->get('numberDocument') ?? NULL,
  410.             "healthCard" => $request->get('healthCard') ?? NULL,
  411.             "dateOfBirth" => $request->get('dateOfBirth') ? new DateTime($request->get('dateOfBirth')) : NULL,
  412.             "medicalReviewDate" => $request->get('medicalReviewDate') ? new DateTime($request->get('medicalReviewDate')) : NULL,
  413.             "lastClub" => $request->get('lastClub') ?? NULL,
  414.         ];
  415.         $image $request->get('image');
  416.         if (!empty($image))
  417.             $data["image"] = new UploadedBase64File($image$request->get('imageFilename'));
  418.         return $data;
  419.     }
  420.     /**
  421.      * @param Request $request
  422.      * @return JsonResponse
  423.      *
  424.      * @Route("/player/trainingAsist", name="api_player_trainingAsist", methods={"POST"})
  425.      */
  426.     public function trainingAsistPlayer(Request $request)
  427.     {
  428.         $prefixFile $this->getParameter("app.path.player_images");
  429.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  430.         $order $request->get('order''trainingAsist');
  431.         $players = [];
  432.         /** @var PlayerRepository $playerRepository */
  433.         $playerRepository $this->em->getRepository(Player::class);
  434.         /** @var Customer $customer */
  435.         $customer $this->getUser();
  436.         $season $customer->getSeasonActive();
  437.         if (!empty($season)) {
  438.             $filter = [
  439.                 "season" => $season->getId()
  440.             ];
  441.             /** @var SeasonPlayer $seasonPlayer */
  442.             /** @var Player $player */
  443.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  444.                 $player $seasonPlayer->getPlayer();
  445.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  446.                 $isActive $player->getIsActive();
  447.                 if ($isActive && $seasonPlayerActive) {
  448.                     $result $playerRepository->getStatsAsist($player$season);
  449.                     // Retrieve min statistics and league statistics
  450.                     $playerMinutes $this->em->getRepository(GamePlayerStatistics::class)->getPlayerMinutes($seasonPlayer->getId());
  451.                     $totalMinutes = (int) $playerMinutes['totalMinutes'];
  452.                     $leagueMinutes = (int) $playerMinutes['leagueMinutes'];
  453.                     
  454.                     $players[] = [
  455.                         "id" => $player->getId(),
  456.                         "name" => $player->getName(),
  457.                         "lastname" => $player->getLastname(),
  458.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  459.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  460.                         "trainingAsist" => intval($result['trainingAsist'] ?? 0),
  461.                         "trainingAsistTotal" => intval($result['trainingAsistTotal'] ?? 0),
  462.                         "trainingAsistPercent" => intval($result['trainingAsistPercent'] ?? 0),
  463.                         "totalMinutes" => $totalMinutes,
  464.                         "leagueMinutes" => $leagueMinutes,
  465.                     ];
  466.                 }
  467.             }
  468.             usort($players, function ($a$b) use ($order) {
  469.                 if ($a[$order] > $b[$order])
  470.                     return 1;
  471.                 if ($a[$order] < $b[$order])
  472.                     return -1;
  473.                 return 0;
  474.             });
  475.             $players array_reverse($players);
  476.         }
  477.         return $this->response($players);
  478.     }
  479.     /**
  480.      * @param Request $request
  481.      * @return JsonResponse
  482.      *
  483.      * @Route("/player/convocatorias", name="api_player_convocatorias", methods={"POST"})
  484.      */
  485.     public function convocatoriasPlayer(Request $request)
  486.     {
  487.         $prefixFile $this->getParameter("app.path.player_images");
  488.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  489.         $request $this->transformJsonBody($request);
  490.         $players = [];
  491.         /** @var PlayerRepository $playerRepository */
  492.         $playerRepository $this->em->getRepository(Player::class);
  493.         /** @var Customer $customer */
  494.         $customer $this->getUser();
  495.         $season $customer->getSeasonActive();
  496.         if (!empty($season)) {
  497.             $filter = [
  498.                 "season" => $season->getId()
  499.             ];
  500.             /** @var ConvocatoriaRepository $convocatoriaRepository */
  501.             $convocatoriaRepository $this->em->getRepository(Convocatoria::class);
  502.             /** @var SeasonPlayer $seasonPlayer */
  503.             /** @var Player $player */
  504.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  505.                 $player $seasonPlayer->getPlayer();
  506.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  507.                 $isActive $player->getIsActive();
  508.                 if ($isActive && $seasonPlayerActive) {
  509.                     $result $playerRepository->getStatsConvocatoria($player$season);
  510.                     $players[] = array_merge([
  511.                         "id" => $player->getId(),
  512.                         "name" => $player->getName(),
  513.                         "lastname" => $player->getLastname(),
  514.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  515.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  516.                     ],$result);
  517.                 }
  518.             }
  519.             usort($players, function ($a$b) {
  520.                 if ($a["convocatory"] > $b["convocatory"])
  521.                     return 1;
  522.                 if ($a["convocatory"] < $b["convocatory"])
  523.                     return -1;
  524.                 return 0;
  525.             });
  526.             $players array_reverse($players);
  527.         }
  528.         return $this->response($players);
  529.     }
  530.     /**
  531.      * @param Request $request
  532.      * @return JsonResponse
  533.      *
  534.      * @Route("/player/assistances/{id}/{month}/{year}", name="api_player_assistances", methods={"POST"})
  535.      */
  536.     public function assistancesPlayer($id$month$year)
  537.     {
  538.         /** @var Player $player */
  539.         $player $this->em->getRepository(Player::class)->findOneBy(['id' => $id]);
  540.         $lowermonth = \DateTime::createFromFormat("d/m/Y H:i:s""1/" . ($month) . "/" . ($year) . " 00:00:00");
  541.         $uppermonth = \DateTime::createFromFormat("d/m/Y H:i:s""0/" . ($month 1) . "/" . ($year) . " 00:00:00");
  542.         $dates = [];
  543.         $dates['justifications'] = [];
  544.         $justifications $player->getJustification();
  545.         foreach ($justifications as $justification) {
  546.             if ($justification->getDate() >= $lowermonth && $justification->getDate() <= $uppermonth) {
  547.                 $dates['justifications'][] = [
  548.                     "date" => $justification->getDate()->format("Y-m-d"),
  549.                     "type" => $justification->getJustificationType()->getId()
  550.                 ];
  551.             }
  552.         };
  553.         $dates['assistances'] = [];
  554.         $trainingsAssits $player->getTrainingAsists();
  555.         foreach ($trainingsAssits as $trainingAssist) {
  556.             if ($trainingAssist->getDate() >= $lowermonth && $trainingAssist->getDate() <= $uppermonth)
  557.                 $dates['assistances'][] = [
  558.                     "date" => $trainingAssist->getDate()->format("Y-m-d")
  559.                 ];
  560.         };
  561.         $dates['lacks'] = [];
  562.         /** @var Customer $customer */
  563.         $customer $this->getUser();
  564.         $season $customer->getSeasonActive();
  565.         if (!empty($season)) {
  566.             $filter = [
  567.                 [
  568.                     "prop" => "season",
  569.                     "comp" => "=",
  570.                     "val" => $season->getId()
  571.                 ],
  572.                 [
  573.                     "prop" => "date",
  574.                     "comp" => ">=",
  575.                     "val" => $lowermonth
  576.                 ],
  577.                 [
  578.                     "prop" => "date",
  579.                     "comp" => "<=",
  580.                     "val" => $uppermonth
  581.                 ],
  582.             ];
  583.             /** @var TrainingAsist $trainingAsist */
  584.             foreach ($this->em->getRepository(TrainingAsist::class)->findByOwnCriteria($filter) as $trainingAsist) {
  585.                 $date $trainingAsist->getDate()->format("Y-m-d");
  586.                 if (!in_array($datearray_column($dates['justifications'], 'date')) && !in_array($datearray_column($dates['assistances'], 'date'))) {
  587.                     $dates['lacks'][] = [
  588.                         "date" => $date
  589.                     ];
  590.                 }
  591.             }
  592.         }
  593.         return $this->response($dates);
  594.     }
  595.     /**
  596.      * @param Request $request
  597.      * @return JsonResponse
  598.      *
  599.      * @Route("/player/assistance/{id}/{month}", name="api_player_assistance", methods={"POST"})
  600.      */
  601.     public function assistancePlayer($id$month)
  602.     {
  603.         /** @var Player $player */
  604.         $player $this->em->getRepository(Player::class)->findOneBy(['id' => $id]);
  605.         $lowermonth = \DateTime::createFromFormat("d/m/Y H:i:s""1/" . ($month) . "/" date('Y') . " 00:00:00");
  606.         $uppermonth = \DateTime::createFromFormat("d/m/Y H:i:s""0/" . ($month 1) . "/" date('Y') . " 00:00:00");
  607.         $dates = [];
  608.         $dates['justifications'] = [];
  609.         $justifications $player->getJustification();
  610.         foreach ($justifications as $justification) {
  611.             if ($justification->getDate() >= $lowermonth && $justification->getDate() <= $uppermonth) {
  612.                 $dates['justifications'][] = [
  613.                     "date" => $justification->getDate()->format("Y-m-d"),
  614.                     "type" => $justification->getJustificationType()->getId()
  615.                 ];
  616.             }
  617.         };
  618.         $dates['assistances'] = [];
  619.         $trainingsAssits $player->getTrainingAsists();
  620.         foreach ($trainingsAssits as $trainingAssist) {
  621.             if ($trainingAssist->getDate() >= $lowermonth && $trainingAssist->getDate() <= $uppermonth)
  622.                 $dates['assistances'][] = [
  623.                     "date" => $trainingAssist->getDate()->format("Y-m-d")
  624.                 ];
  625.         };
  626.         $dates['lacks'] = [];
  627.         /** @var Customer $customer */
  628.         $customer $this->getUser();
  629.         $season $customer->getSeasonActive();
  630.         if (!empty($season)) {
  631.             $filter = [
  632.                 [
  633.                     "prop" => "season",
  634.                     "comp" => "=",
  635.                     "val" => $season->getId()
  636.                 ],
  637.                 [
  638.                     "prop" => "date",
  639.                     "comp" => ">=",
  640.                     "val" => $lowermonth
  641.                 ],
  642.                 [
  643.                     "prop" => "date",
  644.                     "comp" => "<=",
  645.                     "val" => $uppermonth
  646.                 ],
  647.             ];
  648.             /** @var TrainingAsist $trainingAsist */
  649.             foreach ($this->em->getRepository(TrainingAsist::class)->findByOwnCriteria($filter) as $trainingAsist) {
  650.                 $date $trainingAsist->getDate()->format("Y-m-d");
  651.                 if (!in_array($datearray_column($dates['justifications'], 'date')) && !in_array($datearray_column($dates['assistances'], 'date'))) {
  652.                     $dates['lacks'][] = [
  653.                         "date" => $date
  654.                     ];
  655.                 }
  656.             }
  657.         }
  658.         return $this->response($dates);
  659.     }
  660.     /**
  661.      * @param Request $request
  662.      * @return JsonResponse
  663.      *
  664.      * @Route("/player/delete/{player}", name="api_player_delete", methods={"POST"})
  665.      */
  666.     public function deletePlayer(Player $player)
  667.     {
  668.         $response $this->playerManager->delete($player);
  669.         $this->setStatusCode($response["code"]);
  670.         return $this->response($response);
  671.     }
  672.         /**
  673.      * @param Request $request
  674.      * @return JsonResponse
  675.      *
  676.      * @Route("/player/delete/{player}/permanent", name="api_player_delete_permanent", methods={"POST"})
  677.      */
  678.     public function deletePlayerPermanent(Player $player)
  679.     {
  680.         $response $this->playerManager->deletePermanent($player);
  681.         $this->setStatusCode($response["code"]);
  682.         return $this->response($response);
  683.     }
  684.             /**
  685.      * @param Request $request
  686.      * @return JsonResponse
  687.      *
  688.      * @Route("/player/restore/{player}", name="api_player_restore", methods={"POST"})
  689.      */
  690.     public function restorePlayer(Player $player)
  691.     {
  692.         $response $this->playerManager->restorePlayer($player);
  693.         $this->setStatusCode($response["code"]);
  694.         return $this->response($response);
  695.     }
  696.     /**
  697.      * @param Request $request
  698.      * @return JsonResponse
  699.      *
  700.      * @Route("/player/games/{id}", name="api_player_games", methods={"POST"})
  701.      */
  702.     public function gamesPlayer($idRequest $request)
  703.     {
  704.         $prefixFile $this->getParameter("app.path.game_images");
  705.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  706.         /** @var Customer $customer */
  707.         $customer $this->getUser();
  708.         $season $customer->getSeasonActive();
  709.         /** @var PlayerRepository $playerRepository */
  710.         $playerRepository $this->em->getRepository(Player::class);
  711.         $player $playerRepository->findOneBy([
  712.             'id' => $id
  713.         ]);
  714.         $results $playerRepository->findGamesBySeason($player$season);
  715.         /** @var PlayerRepository $playerRepository */
  716.         $gameRepository $this->em->getRepository(Game::class);
  717.         $output = [];
  718.         foreach ($results as $result) {
  719.             $game $gameRepository->findOneBy([
  720.                 'id' => $result['id']
  721.             ])->__toArray($baseurl $prefixFile "/");
  722.             $game['playerStats'] = $result;
  723.             $output[] = $game;
  724.         }
  725.         return $this->response($output);
  726.     }
  727.     /**
  728.      * @param Request $request
  729.      * @return JsonResponse
  730.      *
  731.      * @Route("/player/justifications", name="api_player_justifications", methods={"POST"})
  732.      */
  733.     public function justifications(Request $request)
  734.     {
  735.         $prefixFile $this->getParameter("app.path.player_images");
  736.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  737.         $order $request->get('order''justificationTotal');
  738.         $players = [];
  739.         /** @var PlayerRepository $playerRepository */
  740.         $playerRepository $this->em->getRepository(Player::class);
  741.         /** @var Customer $customer */
  742.         $customer $this->getUser();
  743.         $season $customer->getSeasonActive();
  744.         if (!empty($season)) {
  745.             $filter = [
  746.                 "season" => $season->getId()
  747.             ];
  748.             /** @var SeasonPlayer $seasonPlayer */
  749.             /** @var Player $player */
  750.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  751.                 $player $seasonPlayer->getPlayer();
  752.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  753.                 $isActive $player->getIsActive();
  754.                 $playerSportInjure = [
  755.                     'season' => $season,
  756.                     'seasonPlayer' => $seasonPlayer
  757.                 ];
  758.                 $playerInjury = [];
  759.                 foreach ($this->em->getRepository(PlayerSportsInjuries::class)->findBy($playerSportInjure) as $key => $injury) {
  760.                     $playerInjury[] = $injury->__toArray();
  761.                 }
  762.                 if ($isActive && $seasonPlayerActive) {
  763.                     $justifications $playerRepository->getStatsJustification($player$season);
  764.                     $justificationsConvocatoria $playerRepository->getStatsJustificationConvocatoria($player$season);
  765.                     $base = [
  766.                         "id" => $player->getId(),
  767.                         "name" => $player->getName(),
  768.                         "lastname" => $player->getLastname(),
  769.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  770.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  771.                         "playerInjuriesGames" => $playerInjury
  772.                     ];
  773.                     $players[] = array_merge($base$justifications$justificationsConvocatoria);
  774.                 }
  775.             }
  776.             usort($players, function ($a$b) use ($order) {
  777.                 if ($a[$order] > $b[$order])
  778.                     return 1;
  779.                 if ($a[$order] < $b[$order])
  780.                     return -1;
  781.                 return 0;
  782.             });
  783.             $players array_reverse($players);
  784.         }
  785.         return $this->response($players);
  786.     }
  787.     /**
  788.      * @param Request $request
  789.      * @return JsonResponse
  790.      *
  791.      * @Route("/player/{id}/seasons", name="api_player_seasons", methods={"POST"})
  792.      */
  793.     public function seasons(Request $request)
  794.     {
  795.         $prefixFile $this->getParameter("app.path.player_images");
  796.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  797.         $order $request->get('order''justificationTotal');
  798.         $players = [];
  799.         /** @var PlayerRepository $playerRepository */
  800.         $playerRepository $this->em->getRepository(Player::class);
  801.         /** @var Customer $customer */
  802.         $customer $this->getUser();
  803.         $season $customer->getSeasonActive();
  804.         if (!empty($season)) {
  805.             $filter = [
  806.                 "season" => $season->getId()
  807.             ];
  808.             /** @var SeasonPlayer $seasonPlayer */
  809.             /** @var Player $player */
  810.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  811.                 $player $seasonPlayer->getPlayer();
  812.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  813.                 $isActive $player->getIsActive();
  814.                 if ($isActive && $seasonPlayerActive) {
  815.                     $justifications $playerRepository->getStatsJustification($player$season);
  816.                     $justificationsConvocatoria $playerRepository->getStatsJustificationConvocatoria($player$season);
  817.                     $base = [
  818.                         "id" => $player->getId(),
  819.                         "name" => $player->getName(),
  820.                         "lastname" => $player->getLastname(),
  821.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  822.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  823.                     ];
  824.                     $players[] = array_merge($base$justifications$justificationsConvocatoria);
  825.                 }
  826.             }
  827.             usort($players, function ($a$b) use ($order) {
  828.                 if ($a[$order] > $b[$order])
  829.                     return 1;
  830.                 if ($a[$order] < $b[$order])
  831.                     return -1;
  832.                 return 0;
  833.             });
  834.             $players array_reverse($players);
  835.         }
  836.         return $this->response($players);
  837.     }
  838.     /**
  839.      * @param Request $request
  840.      * @return JsonResponse
  841.      *
  842.      * @Route("/player/search_customer_shared", name="api_search_customer_shared_", methods={"POST"})
  843.      */
  844.     public function searchCustomerSharedPlayer(Request $request)
  845.     {
  846.         if (!$request->get('email')) {
  847.             return $this->response(['code' => 422'message' => 'email is required']);
  848.         }
  849.         $customerRepository $this->em->getRepository(Customer::class);
  850.         $customer $customerRepository->findOneBy(["email" => $request->get("email")]);
  851.         if ($customer) {
  852.             return $this->response([
  853.                 'code' => 200,
  854.                 'data' => [
  855.                     'id' => $customer->getId(),
  856.                     'name' => $customer->getName(),
  857.                     'teamName' => $customer->getNameTeam(),
  858.                     'email' => $customer->getEmail(),
  859.                 ]
  860.             ]);
  861.         }
  862.         return $this->response([
  863.             'code' => 401,
  864.             'message' => 'user not fount!',
  865.         ]);
  866.     }
  867.     /**
  868.      * @param Request $request
  869.      * @return JsonResponse
  870.      *
  871.      * @Route("/players/season-squad/evaluate", name="api_season_players_evaluate", methods={"POST"})
  872.      */
  873.     public function evalutePlayerBySeason(Request $request)
  874.     {
  875.         $request $this->transformJsonBody($request);
  876.         $players = [];
  877.         /** @var Customer $customer */
  878.         $customer $this->getUser();
  879.         $season $customer->getSeasonActive();
  880.         
  881.         $response $this->playerManager->isOverTheCap($customer$season);
  882.       
  883.         return $this->response($response);
  884.     }
  885.     
  886.     /**
  887.      * @param Request $request
  888.      * @return JsonResponse
  889.      *
  890.      * @Route("/player-league/stats/{player}", name="api_season_player_league_stats", methods={"POST"})
  891.      */
  892.     public function playerLeagueStats(Player $playerRequest $request)
  893.     {
  894.         $request $this->transformJsonBody($request);
  895.         /** @var Customer $customer */
  896.         $customer $this->getUser();
  897.         $season $customer->getSeasonActive();
  898.         
  899.         $seasonPlayer $this->getSeasonPlayer($player$season);
  900.       
  901.         /** @var PlayerRepository $playerRepository */
  902.         $gameRepository $this->em->getRepository(Game::class);
  903.         
  904.         $playerLeagueStats $gameRepository->getSeasonPlayerStats($seasonPlayer->getId());
  905.         return $this->response($playerLeagueStats);
  906.     }
  907.     public function getSeasonPlayer(Player $playerSeason $season)
  908.     {
  909.         $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->findOneBy(['player' => $player"season" => $season]);
  910.         return $seasonPlayer;
  911.     }
  912. }