src/Controller/InternalApiController.php line 94
- <?php
- namespace App\Controller;
- use App\Entity\Club;
- use App\Entity\ClubNameHistory;
- use App\Entity\Event;
- use App\Entity\Game;
- use App\Entity\GameStats;
- use App\Entity\Player;
- use App\Entity\PlayerAchievements;
- use App\Entity\PlayerCareer;
- use App\Entity\Season;
- use App\Entity\Stadium;
- use App\Entity\ChangeList;
- use App\Entity\StadiumNameHistory;
- use App\Entity\Tournament;
- use App\Entity\TournamentClub;
- use App\Entity\TournamentStage;
- use Doctrine\ORM\EntityManagerInterface;
- use GuzzleHttp\Exception\GuzzleException;
- use Symfony\Component\HttpFoundation\Response;
- use GuzzleHttp\Client;
- use Symfony\Component\Serializer\Encoder\JsonEncoder;
- use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
- use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
- use Symfony\Component\Serializer\Serializer;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
- use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
- use DateTimeInterface;
- use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- class InternalApiController extends AbstractController
- {
- protected EntityManagerInterface $em;
- private Serializer $serializer;
- private string $password;
- private string $sendUrl;
- public function __construct(
- EntityManagerInterface $entityManager,
- string $secretKey,
- string $url
- ){
- $this->em = $entityManager;
- $this->password = $secretKey;
- $this->sendUrl = $url;
- $dateCallback = function(
- $innerObject
- ){
- return $innerObject instanceof DateTimeInterface ? $innerObject->format("Y-m-d H:i:s") : '';
- };
- $defaultContext = [
- AbstractNormalizer::CALLBACKS => [
- 'birthdate' => $dateCallback,
- 'team_debut' => $dateCallback,
- 'start' => $dateCallback,
- 'end' => $dateCallback,
- 'startDate' => $dateCallback,
- 'endDate' => $dateCallback,
- 'dateStart' => $dateCallback,
- 'timeStart' => $dateCallback,
- 'createDate' => $dateCallback,
- 'fromDate' => $dateCallback,
- 'dateCreate' => $dateCallback,
- 'dateUpdate' => $dateCallback,
- ],
- ];
- $encoders = [new JsonEncoder()];
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $normalizers = [
- new ObjectNormalizer(
- $classMetadataFactory,
- null,
- null,
- null,
- null,
- null,
- $defaultContext
- ),
- new DateTimeNormalizer(),
- ];
- $this->serializer = new Serializer($normalizers, $encoders);
- }
- public function send(): Response
- {
- $jsonData = [];
- $changes = $this->em->getRepository(ChangeList::class)->findNotSent();
- if(!is_null($changes) and !empty($changes)){
- foreach($changes as $change){
- $json['entity_name'] = $change->getEntityName();
- $json['change_type'] = $change->getChangeType();
- if($change->getChangeType() != 'del'){
- switch($change->getEntityName()){
- case "Club":
- $club = $this->em->getRepository(Club::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($club, 'json', ['groups' => 'groupClub']);
- break;
- case "Event":
- $event = $this->em->getRepository(Event::class)->find($change->getChangeId());
- if(!is_null($event)){
- $jsonData = $this->serializer->serialize($event, 'json', ['groups' => 'groupEvent']);
- }else{
- $jsonData = '';
- $change->setSent(1);
- }
- break;
- case "Game":
- $game = $this->em->getRepository(Game::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($game, 'json', ['groups' => 'groupGame']);
- break;
- case "Player":
- $player = $this->em->getRepository(Player::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($player, 'json', ['groups' => 'groupPlayer']);
- break;
- case "Season":
- $season = $this->em->getRepository(Season::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($season, 'json', ['groups' => 'groupSeason']);
- break;
- case "Stadium":
- $stadium = $this->em->getRepository(Stadium::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($stadium, 'json', ['groups' => 'groupStadium']);
- break;
- case "Tournament":
- $tournament = $this->em->getRepository(Tournament::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($tournament, 'json', ['groups' => 'groupTournament']);
- break;
- case "TournamentStage":
- $tournamentStage = $this->em->getRepository(TournamentStage::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($tournamentStage, 'json', ['groups' => 'groupTournamentStage']);
- break;
- case "TournamentClub":
- $tournamentClub = $this->em->getRepository(TournamentClub::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($tournamentClub, 'json', ['groups' => 'groupTournamentClub']);
- break;
- case "ClubNameHistory":
- $clubHistoryName = $this->em->getRepository(ClubNameHistory::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($clubHistoryName, 'json');
- break;
- case "StadiumNameHistory":
- $stadiumHistoryName = $this->em->getRepository(StadiumNameHistory::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($stadiumHistoryName, 'json');
- break;
- case "GameStats":
- $gameStats = $this->em->getRepository(GameStats::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($gameStats, 'json', ['groups' => 'groupGameStats']);
- break;
- case "PlayerCareer":
- $career = $this->em->getRepository(PlayerCareer::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($career, 'json', ['groups' => 'groupPlayerCareer']);
- break;
- case "PlayerAchievements":
- $achievements = $this->em->getRepository(PlayerAchievements::class)->find($change->getChangeId());
- $jsonData = $this->serializer->serialize($achievements, 'json', ['groups' => 'groupPlayerAchievements']);
- break;
- }
- $jsonData = json_decode($jsonData, true);
- if(isset($jsonData['logo'])){
- if($jsonData['logo'] != ''){
- $jsonData['logo'] = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$jsonData['logo'];
- }
- }
- if(isset($jsonData['logoForList'])){
- if($jsonData['logoForList'] != ''){
- $jsonData['logoForList'] = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$jsonData['logoForList'];
- }
- }
- }else{
- $json['entity_name'] = $change->getEntityName();
- $json['change_type'] = $change->getChangeType();
- $jsonData['id'] = $change->getChangeId();
- }
- $hash = md5($this->password.$change->getChangeId().$json['entity_name'].$json['change_type']);
- $json['hash'] = $hash;
- $json['data'] = $jsonData;
- //print_r($jsonData);
- $client = new Client();
- //file_put_contents("./test.tzt",json_encode($json));
- try{
- $response = $client->request(
- 'POST',
- $this->sendUrl,
- [
- 'auth' => ['admin', '12345'],
- 'json' => $json,
- ]
- );
- $result = json_decode($response->getBody()->getContents());
- //print_r($result);
- if(isset($result->status) and $result->status == 'success'){
- $change->setSent(1);
- }elseif(isset($result->status) and $result->status == 'error'){
- $change->setErrorText($result->description);
- }
- }catch(GuzzleException $e){
- $change->setErrorText("Response code: ".$e->getCode());
- }
- $change->setCount($change->getCount() + 1);
- $this->em->persist($change);
- $this->em->flush();
- }
- }
- return new Response(
- 'Content',
- Response::HTTP_OK,
- ['content-type' => 'text/html']
- );
- }
- }