src/Controller/Api/Banner/V1/CpcClickController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Banner\V1;
  3. use App\Controller\Api\ApiInterface;
  4. use App\Repository\CampaignRepository;
  5. use App\Repository\MediaRepository;
  6. use App\Repository\UserRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use PhpParser\Node\Scalar\String_;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Cache\Adapter\RedisAdapter;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use App\Message\CpcClickMessage;
  16. use Symfony\Contracts\Cache\ItemInterface;
  17. class CpcClickController extends AbstractController implements ApiInterface
  18. {
  19.     private $em;
  20.     private $userRepository;
  21.     private $campaignRepository;
  22.     public function __construct(EntityManagerInterface $em,UserRepository $userRepository,CampaignRepository $campaignRepository)
  23.     {
  24.         $this->em $em;
  25.         $this->userRepository $userRepository;
  26.         $this->campaignRepository $campaignRepository;
  27.     }
  28.     /**
  29.      * @param string $uid
  30.      * @param MediaRepository $mediaRepository
  31.      * @param MessageBusInterface $messageBus
  32.      * @param Request $request
  33.      * @return RedirectResponse
  34.      * @Route("api/banner/v1/click/{uid}",name="api.banner.v1.click",methods={"GET"})
  35.      */
  36.     public function index(string $uid,MediaRepository $mediaRepository,MessageBusInterface $messageBus,Request $request):RedirectResponse
  37.     {
  38.         $media $mediaRepository->findOneBy(['uid'=>$uid]);
  39.         $campaign $this->campaignRepository->find($media->getCampaign()->getId());
  40.         $cors = ['Access-Control-Allow-Origin'=>'*','Access-Control-Allow-Methods'=>'GET, POST, PATCH, PUT, DELETE, OPTIONS','Access-Control-Allow-Headers'=>'*','X-Robots-Tag'=>'noindex'];
  41.         if(empty($media)){
  42.             return $this->redirect($_ENV['base_aurl']);
  43.         }else{
  44.             /** Media exits */
  45.             if($_ENV['APP_ENV'] == 'prod') {
  46.                 $ip $request->headers->get('ar-real-ip');
  47.             }else{
  48.                 $ip str_ireplace($request->getClientIp(), ':''.');
  49.             }
  50.             $cacheKey "{$ip}-".$media->getId();
  51.             $cache = new RedisAdapter(
  52.                 RedisAdapter::createConnection('redis://localhost'),
  53.                 'cpc-click',
  54.                 $defaultLifetime 60
  55.             );
  56.             $cache->get($cacheKey,function (ItemInterface $item) use ($media,$campaign,$messageBus,$request){
  57.                 $item->expiresAfter(1800);
  58.                 $messageBus->dispatch(new CpcClickMessage($media->getId()));
  59.             });
  60.             $utmParams http_build_query(['utm_source'=> $campaign->getUtmSource(),'utm_medium'=>$campaign->getUtmMedium(),'utm_campaign'=>$campaign->getUtmCampaign()]);
  61.             if (parse_url($media->getUrl(), PHP_URL_QUERY)){
  62.                 return $this->redirect($media->getUrl().'&'.$utmParams);
  63.             }else{
  64.                 return $this->redirect($media->getUrl().'?'.$utmParams);
  65.             }
  66.         }
  67.     }
  68.     /**
  69.      * @param Request $request
  70.      * @Route("api/get-ip",name="get.ip",methods={"GET"})
  71.      */
  72.     public function getIp(Request $request){
  73.         $cors = ['Access-Control-Allow-Origin'=>'*','Access-Control-Allow-Methods'=>'GET, POST, PATCH, PUT, DELETE, OPTIONS','Access-Control-Allow-Headers'=>'*','X-Robots-Tag'=>'noindex'];
  74.         if($_ENV['APP_ENV'] == 'prod') {
  75.             $ip $request->headers->get('ar-real-ip');
  76.         }else{
  77.             $ip str_ireplace($request->getClientIp(), ':''.');
  78.         }
  79.         return $this->json(['ip'=>$ip],200,$cors);
  80.     }
  81. }