src/Controller/Api/PlayerController.php line 848

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.             "goalsConceded" => $request->get('goalsConceded') ?? 0,
  379.             "lostBalls" => $request->get('lostBalls') ?? 0,
  380.             "note" => $request->get('note') ?? 0,
  381.         ];
  382.         /** @var Customer $customer */
  383.         $customer $this->getUser();
  384.         $season $customer->getSeasonActive();
  385.         $response $this->playerManager->updateStats($player$datafalse);
  386.         $this->setStatusCode($response["code"]);
  387.         if ($response['code'] == 200) {
  388.             $response['player'] = $this->getDataPlayer($request$response['player'], $season);
  389.         };
  390.         return $this->response($response);
  391.     }
  392.     protected function getDataFromRequest(Request $request)
  393.     {
  394.         $data = [
  395.             "name" => $request->get('name'),
  396.             "lastname" => $request->get('lastname'),
  397.             "position" => $request->get('position'),
  398.             "secondary_position" => $request->get('secondaryPosition'),
  399.             "age" => (int) $request->get('age'),
  400.             "weight" => (float) $request->get('weight'),
  401.             "height" => (float) $request->get('height'),
  402.             "shoes" => (int) $request->get('shoes'),
  403.             "games" => (int) $request->get('games'),
  404.             "goals" => (int) $request->get('goals'),
  405.             "yellowCard" => (int) $request->get('yellowCard'),
  406.             "redCard" => (int) $request->get('redCard'),
  407.             "dorsal" => (int) $request->get('dorsal'),
  408.             "skillfulLeg" => (int) $request->get('skillfulLeg'),
  409.             "countryId" => (int) $request->get('countryId'),
  410.             "phone" => $request->get('phone') ?? NULL,
  411.             "email" => $request->get('email') ?? NULL,
  412.             "numberDocument" => $request->get('numberDocument') ?? NULL,
  413.             "healthCard" => $request->get('healthCard') ?? NULL,
  414.             "dateOfBirth" => $request->get('dateOfBirth') ? new DateTime($request->get('dateOfBirth')) : NULL,
  415.             "medicalReviewDate" => $request->get('medicalReviewDate') ? new DateTime($request->get('medicalReviewDate')) : NULL,
  416.             "lastClub" => $request->get('lastClub') ?? NULL,
  417.         ];
  418.         $image $request->get('image');
  419.         if (!empty($image))
  420.             $data["image"] = new UploadedBase64File($image$request->get('imageFilename'));
  421.         return $data;
  422.     }
  423.     /**
  424.      * @param Request $request
  425.      * @return JsonResponse
  426.      *
  427.      * @Route("/player/trainingAsist", name="api_player_trainingAsist", methods={"POST"})
  428.      */
  429.     public function trainingAsistPlayer(Request $request)
  430.     {
  431.         $prefixFile $this->getParameter("app.path.player_images");
  432.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  433.         $order $request->get('order''trainingAsist');
  434.         $players = [];
  435.         /** @var PlayerRepository $playerRepository */
  436.         $playerRepository $this->em->getRepository(Player::class);
  437.         /** @var Customer $customer */
  438.         $customer $this->getUser();
  439.         $season $customer->getSeasonActive();
  440.         if (!empty($season)) {
  441.             $filter = [
  442.                 "season" => $season->getId()
  443.             ];
  444.             /** @var SeasonPlayer $seasonPlayer */
  445.             /** @var Player $player */
  446.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  447.                 $player $seasonPlayer->getPlayer();
  448.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  449.                 $isActive $player->getIsActive();
  450.                 if ($isActive && $seasonPlayerActive) {
  451.                     $result $playerRepository->getStatsAsist($player$season);
  452.                     // Retrieve min statistics and league statistics
  453.                     $playerMinutes $this->em->getRepository(GamePlayerStatistics::class)->getPlayerMinutes($seasonPlayer->getId());
  454.                     $totalMinutes = (int) $playerMinutes['totalMinutes'];
  455.                     $leagueMinutes = (int) $playerMinutes['leagueMinutes'];
  456.                     
  457.                     $players[] = [
  458.                         "id" => $player->getId(),
  459.                         "name" => $player->getName(),
  460.                         "lastname" => $player->getLastname(),
  461.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  462.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  463.                         "trainingAsist" => intval($result['trainingAsist'] ?? 0),
  464.                         "trainingAsistTotal" => intval($result['trainingAsistTotal'] ?? 0),
  465.                         "trainingAsistPercent" => intval($result['trainingAsistPercent'] ?? 0),
  466.                         "totalMinutes" => $totalMinutes,
  467.                         "leagueMinutes" => $leagueMinutes,
  468.                     ];
  469.                 }
  470.             }
  471.             usort($players, function ($a$b) use ($order) {
  472.                 if ($a[$order] > $b[$order])
  473.                     return 1;
  474.                 if ($a[$order] < $b[$order])
  475.                     return -1;
  476.                 return 0;
  477.             });
  478.             $players array_reverse($players);
  479.         }
  480.         return $this->response($players);
  481.     }
  482.     /**
  483.      * @param Request $request
  484.      * @return JsonResponse
  485.      *
  486.      * @Route("/player/convocatorias", name="api_player_convocatorias", methods={"POST"})
  487.      */
  488.     public function convocatoriasPlayer(Request $request)
  489.     {
  490.         $prefixFile $this->getParameter("app.path.player_images");
  491.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  492.         $request $this->transformJsonBody($request);
  493.         $players = [];
  494.         /** @var PlayerRepository $playerRepository */
  495.         $playerRepository $this->em->getRepository(Player::class);
  496.         /** @var Customer $customer */
  497.         $customer $this->getUser();
  498.         $season $customer->getSeasonActive();
  499.         if (!empty($season)) {
  500.             $filter = [
  501.                 "season" => $season->getId()
  502.             ];
  503.             /** @var ConvocatoriaRepository $convocatoriaRepository */
  504.             $convocatoriaRepository $this->em->getRepository(Convocatoria::class);
  505.             /** @var SeasonPlayer $seasonPlayer */
  506.             /** @var Player $player */
  507.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  508.                 $player $seasonPlayer->getPlayer();
  509.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  510.                 $isActive $player->getIsActive();
  511.                 if ($isActive && $seasonPlayerActive) {
  512.                     $result $playerRepository->getStatsConvocatoria($player$season);
  513.                     $players[] = array_merge([
  514.                         "id" => $player->getId(),
  515.                         "name" => $player->getName(),
  516.                         "lastname" => $player->getLastname(),
  517.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  518.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  519.                     ],$result);
  520.                 }
  521.             }
  522.             usort($players, function ($a$b) {
  523.                 if ($a["convocatory"] > $b["convocatory"])
  524.                     return 1;
  525.                 if ($a["convocatory"] < $b["convocatory"])
  526.                     return -1;
  527.                 return 0;
  528.             });
  529.             $players array_reverse($players);
  530.         }
  531.         return $this->response($players);
  532.     }
  533.     /**
  534.      * @param Request $request
  535.      * @return JsonResponse
  536.      *
  537.      * @Route("/player/assistances/{id}/{month}/{year}", name="api_player_assistances", methods={"POST"})
  538.      */
  539.     public function assistancesPlayer($id$month$year)
  540.     {
  541.         /** @var Player $player */
  542.         $player $this->em->getRepository(Player::class)->findOneBy(['id' => $id]);
  543.         $lowermonth = \DateTime::createFromFormat("d/m/Y H:i:s""1/" . ($month) . "/" . ($year) . " 00:00:00");
  544.         $uppermonth = \DateTime::createFromFormat("d/m/Y H:i:s""0/" . ($month 1) . "/" . ($year) . " 00:00:00");
  545.         $dates = [];
  546.         $dates['justifications'] = [];
  547.         $justifications $player->getJustification();
  548.         foreach ($justifications as $justification) {
  549.             if ($justification->getDate() >= $lowermonth && $justification->getDate() <= $uppermonth) {
  550.                 $dates['justifications'][] = [
  551.                     "date" => $justification->getDate()->format("Y-m-d"),
  552.                     "type" => $justification->getJustificationType()->getId()
  553.                 ];
  554.             }
  555.         };
  556.         $dates['assistances'] = [];
  557.         $trainingsAssits $player->getTrainingAsists();
  558.         foreach ($trainingsAssits as $trainingAssist) {
  559.             if ($trainingAssist->getDate() >= $lowermonth && $trainingAssist->getDate() <= $uppermonth)
  560.                 $dates['assistances'][] = [
  561.                     "date" => $trainingAssist->getDate()->format("Y-m-d")
  562.                 ];
  563.         };
  564.         $dates['lacks'] = [];
  565.         /** @var Customer $customer */
  566.         $customer $this->getUser();
  567.         $season $customer->getSeasonActive();
  568.         if (!empty($season)) {
  569.             $filter = [
  570.                 [
  571.                     "prop" => "season",
  572.                     "comp" => "=",
  573.                     "val" => $season->getId()
  574.                 ],
  575.                 [
  576.                     "prop" => "date",
  577.                     "comp" => ">=",
  578.                     "val" => $lowermonth
  579.                 ],
  580.                 [
  581.                     "prop" => "date",
  582.                     "comp" => "<=",
  583.                     "val" => $uppermonth
  584.                 ],
  585.             ];
  586.             /** @var TrainingAsist $trainingAsist */
  587.             foreach ($this->em->getRepository(TrainingAsist::class)->findByOwnCriteria($filter) as $trainingAsist) {
  588.                 $date $trainingAsist->getDate()->format("Y-m-d");
  589.                 if (!in_array($datearray_column($dates['justifications'], 'date')) && !in_array($datearray_column($dates['assistances'], 'date'))) {
  590.                     $dates['lacks'][] = [
  591.                         "date" => $date
  592.                     ];
  593.                 }
  594.             }
  595.         }
  596.         return $this->response($dates);
  597.     }
  598.     /**
  599.      * @param Request $request
  600.      * @return JsonResponse
  601.      *
  602.      * @Route("/player/assistance/{id}/{month}", name="api_player_assistance", methods={"POST"})
  603.      */
  604.     public function assistancePlayer($id$month)
  605.     {
  606.         /** @var Player $player */
  607.         $player $this->em->getRepository(Player::class)->findOneBy(['id' => $id]);
  608.         $lowermonth = \DateTime::createFromFormat("d/m/Y H:i:s""1/" . ($month) . "/" date('Y') . " 00:00:00");
  609.         $uppermonth = \DateTime::createFromFormat("d/m/Y H:i:s""0/" . ($month 1) . "/" date('Y') . " 00:00:00");
  610.         $dates = [];
  611.         $dates['justifications'] = [];
  612.         $justifications $player->getJustification();
  613.         foreach ($justifications as $justification) {
  614.             if ($justification->getDate() >= $lowermonth && $justification->getDate() <= $uppermonth) {
  615.                 $dates['justifications'][] = [
  616.                     "date" => $justification->getDate()->format("Y-m-d"),
  617.                     "type" => $justification->getJustificationType()->getId()
  618.                 ];
  619.             }
  620.         };
  621.         $dates['assistances'] = [];
  622.         $trainingsAssits $player->getTrainingAsists();
  623.         foreach ($trainingsAssits as $trainingAssist) {
  624.             if ($trainingAssist->getDate() >= $lowermonth && $trainingAssist->getDate() <= $uppermonth)
  625.                 $dates['assistances'][] = [
  626.                     "date" => $trainingAssist->getDate()->format("Y-m-d")
  627.                 ];
  628.         };
  629.         $dates['lacks'] = [];
  630.         /** @var Customer $customer */
  631.         $customer $this->getUser();
  632.         $season $customer->getSeasonActive();
  633.         if (!empty($season)) {
  634.             $filter = [
  635.                 [
  636.                     "prop" => "season",
  637.                     "comp" => "=",
  638.                     "val" => $season->getId()
  639.                 ],
  640.                 [
  641.                     "prop" => "date",
  642.                     "comp" => ">=",
  643.                     "val" => $lowermonth
  644.                 ],
  645.                 [
  646.                     "prop" => "date",
  647.                     "comp" => "<=",
  648.                     "val" => $uppermonth
  649.                 ],
  650.             ];
  651.             /** @var TrainingAsist $trainingAsist */
  652.             foreach ($this->em->getRepository(TrainingAsist::class)->findByOwnCriteria($filter) as $trainingAsist) {
  653.                 $date $trainingAsist->getDate()->format("Y-m-d");
  654.                 if (!in_array($datearray_column($dates['justifications'], 'date')) && !in_array($datearray_column($dates['assistances'], 'date'))) {
  655.                     $dates['lacks'][] = [
  656.                         "date" => $date
  657.                     ];
  658.                 }
  659.             }
  660.         }
  661.         return $this->response($dates);
  662.     }
  663.     /**
  664.      * @param Request $request
  665.      * @return JsonResponse
  666.      *
  667.      * @Route("/player/delete/{player}", name="api_player_delete", methods={"POST"})
  668.      */
  669.     public function deletePlayer(Player $player)
  670.     {
  671.         $response $this->playerManager->delete($player);
  672.         $this->setStatusCode($response["code"]);
  673.         return $this->response($response);
  674.     }
  675.         /**
  676.      * @param Request $request
  677.      * @return JsonResponse
  678.      *
  679.      * @Route("/player/delete/{player}/permanent", name="api_player_delete_permanent", methods={"POST"})
  680.      */
  681.     public function deletePlayerPermanent(Player $player)
  682.     {
  683.         $response $this->playerManager->deletePermanent($player);
  684.         $this->setStatusCode($response["code"]);
  685.         return $this->response($response);
  686.     }
  687.             /**
  688.      * @param Request $request
  689.      * @return JsonResponse
  690.      *
  691.      * @Route("/player/restore/{player}", name="api_player_restore", methods={"POST"})
  692.      */
  693.     public function restorePlayer(Player $player)
  694.     {
  695.         $response $this->playerManager->restorePlayer($player);
  696.         $this->setStatusCode($response["code"]);
  697.         return $this->response($response);
  698.     }
  699.     /**
  700.      * @param Request $request
  701.      * @return JsonResponse
  702.      *
  703.      * @Route("/player/games/{id}", name="api_player_games", methods={"POST"})
  704.      */
  705.     public function gamesPlayer($idRequest $request)
  706.     {
  707.         $prefixFile $this->getParameter("app.path.game_images");
  708.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  709.         /** @var Customer $customer */
  710.         $customer $this->getUser();
  711.         $season $customer->getSeasonActive();
  712.         /** @var PlayerRepository $playerRepository */
  713.         $playerRepository $this->em->getRepository(Player::class);
  714.         $player $playerRepository->findOneBy([
  715.             'id' => $id
  716.         ]);
  717.         $results $playerRepository->findGamesBySeason($player$season);
  718.         /** @var PlayerRepository $playerRepository */
  719.         $gameRepository $this->em->getRepository(Game::class);
  720.         $output = [];
  721.         foreach ($results as $result) {
  722.             $game $gameRepository->findOneBy([
  723.                 'id' => $result['id']
  724.             ])->__toArray($baseurl $prefixFile "/");
  725.             $game['playerStats'] = $result;
  726.             $output[] = $game;
  727.         }
  728.         return $this->response($output);
  729.     }
  730.     /**
  731.      * @param Request $request
  732.      * @return JsonResponse
  733.      *
  734.      * @Route("/player/justifications", name="api_player_justifications", methods={"POST"})
  735.      */
  736.     public function justifications(Request $request)
  737.     {
  738.         $prefixFile $this->getParameter("app.path.player_images");
  739.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  740.         $order $request->get('order''justificationTotal');
  741.         $players = [];
  742.         /** @var PlayerRepository $playerRepository */
  743.         $playerRepository $this->em->getRepository(Player::class);
  744.         /** @var Customer $customer */
  745.         $customer $this->getUser();
  746.         $season $customer->getSeasonActive();
  747.         if (!empty($season)) {
  748.             $filter = [
  749.                 "season" => $season->getId()
  750.             ];
  751.             /** @var SeasonPlayer $seasonPlayer */
  752.             /** @var Player $player */
  753.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  754.                 $player $seasonPlayer->getPlayer();
  755.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  756.                 $isActive $player->getIsActive();
  757.                 $playerSportInjure = [
  758.                     'season' => $season,
  759.                     'seasonPlayer' => $seasonPlayer
  760.                 ];
  761.                 $playerInjury = [];
  762.                 foreach ($this->em->getRepository(PlayerSportsInjuries::class)->findBy($playerSportInjure) as $key => $injury) {
  763.                     $playerInjury[] = $injury->__toArray();
  764.                 }
  765.                 if ($isActive && $seasonPlayerActive) {
  766.                     $justifications $playerRepository->getStatsJustification($player$season);
  767.                     $justificationsConvocatoria $playerRepository->getStatsJustificationConvocatoria($player$season);
  768.                     $base = [
  769.                         "id" => $player->getId(),
  770.                         "name" => $player->getName(),
  771.                         "lastname" => $player->getLastname(),
  772.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  773.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  774.                         "playerInjuriesGames" => $playerInjury
  775.                     ];
  776.                     $players[] = array_merge($base$justifications$justificationsConvocatoria);
  777.                 }
  778.             }
  779.             usort($players, function ($a$b) use ($order) {
  780.                 if ($a[$order] > $b[$order])
  781.                     return 1;
  782.                 if ($a[$order] < $b[$order])
  783.                     return -1;
  784.                 return 0;
  785.             });
  786.             $players array_reverse($players);
  787.         }
  788.         return $this->response($players);
  789.     }
  790.     /**
  791.      * @param Request $request
  792.      * @return JsonResponse
  793.      *
  794.      * @Route("/player/{id}/seasons", name="api_player_seasons", methods={"POST"})
  795.      */
  796.     public function seasons(Request $request)
  797.     {
  798.         $prefixFile $this->getParameter("app.path.player_images");
  799.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  800.         $order $request->get('order''justificationTotal');
  801.         $players = [];
  802.         /** @var PlayerRepository $playerRepository */
  803.         $playerRepository $this->em->getRepository(Player::class);
  804.         /** @var Customer $customer */
  805.         $customer $this->getUser();
  806.         $season $customer->getSeasonActive();
  807.         if (!empty($season)) {
  808.             $filter = [
  809.                 "season" => $season->getId()
  810.             ];
  811.             /** @var SeasonPlayer $seasonPlayer */
  812.             /** @var Player $player */
  813.             foreach ($this->em->getRepository(SeasonPlayer::class)->findBy($filter) as $seasonPlayer) {
  814.                 $player $seasonPlayer->getPlayer();
  815.                 $seasonPlayerActive = !$seasonPlayer->getDeletedAt();
  816.                 $isActive $player->getIsActive();
  817.                 if ($isActive && $seasonPlayerActive) {
  818.                     $justifications $playerRepository->getStatsJustification($player$season);
  819.                     $justificationsConvocatoria $playerRepository->getStatsJustificationConvocatoria($player$season);
  820.                     $base = [
  821.                         "id" => $player->getId(),
  822.                         "name" => $player->getName(),
  823.                         "lastname" => $player->getLastname(),
  824.                         "position" => !empty($player->getPosition()) ? $player->getPosition()->getSlug() : "",
  825.                         "image" => !empty($player->getImage()) ? $baseurl $prefixFile "/" $player->getImage() : "",
  826.                     ];
  827.                     $players[] = array_merge($base$justifications$justificationsConvocatoria);
  828.                 }
  829.             }
  830.             usort($players, function ($a$b) use ($order) {
  831.                 if ($a[$order] > $b[$order])
  832.                     return 1;
  833.                 if ($a[$order] < $b[$order])
  834.                     return -1;
  835.                 return 0;
  836.             });
  837.             $players array_reverse($players);
  838.         }
  839.         return $this->response($players);
  840.     }
  841.     /**
  842.      * @param Request $request
  843.      * @return JsonResponse
  844.      *
  845.      * @Route("/player/search_customer_shared", name="api_search_customer_shared_", methods={"POST"})
  846.      */
  847.     public function searchCustomerSharedPlayer(Request $request)
  848.     {
  849.         if (!$request->get('email')) {
  850.             return $this->response(['code' => 422'message' => 'email is required']);
  851.         }
  852.         $customerRepository $this->em->getRepository(Customer::class);
  853.         $customer $customerRepository->findOneBy(["email" => $request->get("email")]);
  854.         if ($customer) {
  855.             return $this->response([
  856.                 'code' => 200,
  857.                 'data' => [
  858.                     'id' => $customer->getId(),
  859.                     'name' => $customer->getName(),
  860.                     'teamName' => $customer->getNameTeam(),
  861.                     'email' => $customer->getEmail(),
  862.                 ]
  863.             ]);
  864.         }
  865.         return $this->response([
  866.             'code' => 401,
  867.             'message' => 'user not fount!',
  868.         ]);
  869.     }
  870.     /**
  871.      * @param Request $request
  872.      * @return JsonResponse
  873.      *
  874.      * @Route("/players/season-squad/evaluate", name="api_season_players_evaluate", methods={"POST"})
  875.      */
  876.     public function evalutePlayerBySeason(Request $request)
  877.     {
  878.         $request $this->transformJsonBody($request);
  879.         $players = [];
  880.         /** @var Customer $customer */
  881.         $customer $this->getUser();
  882.         $season $customer->getSeasonActive();
  883.         
  884.         $response $this->playerManager->isOverTheCap($customer$season);
  885.       
  886.         return $this->response($response);
  887.     }
  888.     
  889.     /**
  890.      * @param Request $request
  891.      * @return JsonResponse
  892.      *
  893.      * @Route("/player-league/stats/{player}", name="api_season_player_league_stats", methods={"POST"})
  894.      */
  895.     public function playerLeagueStats(Player $playerRequest $request)
  896.     {
  897.         $request $this->transformJsonBody($request);
  898.         /** @var Customer $customer */
  899.         $customer $this->getUser();
  900.         $season $customer->getSeasonActive();
  901.         
  902.         $seasonPlayer $this->getSeasonPlayer($player$season);
  903.       
  904.         /** @var PlayerRepository $playerRepository */
  905.         $gameRepository $this->em->getRepository(Game::class);
  906.         
  907.         $playerLeagueStats $gameRepository->getSeasonPlayerStats($seasonPlayer->getId());
  908.         return $this->response($playerLeagueStats);
  909.     }
  910.     public function getSeasonPlayer(Player $playerSeason $season)
  911.     {
  912.         $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->findOneBy(['player' => $player"season" => $season]);
  913.         return $seasonPlayer;
  914.     }
  915. }