<?php
namespace App\Controller\Api\Banner\V1;
use App\Controller\Api\ApiInterface;
use App\Repository\CampaignRepository;
use App\Repository\MediaRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use PhpParser\Node\Scalar\String_;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Message\CpcClickMessage;
use Symfony\Contracts\Cache\ItemInterface;
class CpcClickController extends AbstractController implements ApiInterface
{
private $em;
private $userRepository;
private $campaignRepository;
public function __construct(EntityManagerInterface $em,UserRepository $userRepository,CampaignRepository $campaignRepository)
{
$this->em = $em;
$this->userRepository = $userRepository;
$this->campaignRepository = $campaignRepository;
}
/**
* @param string $uid
* @param MediaRepository $mediaRepository
* @param MessageBusInterface $messageBus
* @param Request $request
* @return RedirectResponse
* @Route("api/banner/v1/click/{uid}",name="api.banner.v1.click",methods={"GET"})
*/
public function index(string $uid,MediaRepository $mediaRepository,MessageBusInterface $messageBus,Request $request):RedirectResponse
{
$media = $mediaRepository->findOneBy(['uid'=>$uid]);
$campaign = $this->campaignRepository->find($media->getCampaign()->getId());
$cors = ['Access-Control-Allow-Origin'=>'*','Access-Control-Allow-Methods'=>'GET, POST, PATCH, PUT, DELETE, OPTIONS','Access-Control-Allow-Headers'=>'*','X-Robots-Tag'=>'noindex'];
if(empty($media)){
return $this->redirect($_ENV['base_aurl']);
}else{
/** Media exits */
if($_ENV['APP_ENV'] == 'prod') {
$ip = $request->headers->get('ar-real-ip');
}else{
$ip = str_ireplace($request->getClientIp(), ':', '.');
}
$cacheKey = "{$ip}-".$media->getId();
$cache = new RedisAdapter(
RedisAdapter::createConnection('redis://localhost'),
'cpc-click',
$defaultLifetime = 60
);
$cache->get($cacheKey,function (ItemInterface $item) use ($media,$campaign,$messageBus,$request){
$item->expiresAfter(1800);
$messageBus->dispatch(new CpcClickMessage($media->getId()));
});
$utmParams = http_build_query(['utm_source'=> $campaign->getUtmSource(),'utm_medium'=>$campaign->getUtmMedium(),'utm_campaign'=>$campaign->getUtmCampaign()]);
if (parse_url($media->getUrl(), PHP_URL_QUERY)){
return $this->redirect($media->getUrl().'&'.$utmParams);
}else{
return $this->redirect($media->getUrl().'?'.$utmParams);
}
}
}
/**
* @param Request $request
* @Route("api/get-ip",name="get.ip",methods={"GET"})
*/
public function getIp(Request $request){
$cors = ['Access-Control-Allow-Origin'=>'*','Access-Control-Allow-Methods'=>'GET, POST, PATCH, PUT, DELETE, OPTIONS','Access-Control-Allow-Headers'=>'*','X-Robots-Tag'=>'noindex'];
if($_ENV['APP_ENV'] == 'prod') {
$ip = $request->headers->get('ar-real-ip');
}else{
$ip = str_ireplace($request->getClientIp(), ':', '.');
}
return $this->json(['ip'=>$ip],200,$cors);
}
}