src/Service/StatisticsService.php line 126

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\EntityOtpusk\Cruises;
  4. use App\EntityOtpusk\Operators;
  5. use App\EntityOtpusk\Statistics;
  6. use App\EntityOtpusk\Thotels as Hotels;
  7. use App\EntityOtpusk\Tcities as City;
  8. use App\EntityOtpusk\Tcountries as Country;
  9. use App\Exception\EventsException;
  10. use App\Repository\BlackListRepository;
  11. use App\Repository\StatisticsRepository;
  12. use App\RepositoryOtpusk\HotelsRepository;
  13. use App\RepositoryOtpusk\CruisesRepository;
  14. use App\Repository\ManagerLogsRepository;
  15. use App\Service\Ipv6Service;
  16. use ClickHouseDB\Exception\ClickHouseException;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Component\Cache\Adapter\MemcachedAdapter;
  20. use Symfony\Component\HttpClient\HttpClient;
  21. use Symfony\Component\HttpKernel\KernelInterface;
  22. use Symfony\Contracts\HttpClient\HttpClientInterface;
  23. /**
  24.  * Class StatisticsService
  25.  * @package App\Service
  26.  */
  27. class StatisticsService
  28. {
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     private $em;
  33.     /**
  34.      * @var StatisticsRepository
  35.      */
  36.     private $statisticsRepository;
  37.     /**
  38.      * @var KernelInterface
  39.      */
  40.     private $kernelInterface;
  41.     /**
  42.      * @var CruisesRepository
  43.      */
  44.     private $cruisesRepository;
  45.     /**
  46.      * @var HotelsRepository
  47.      */
  48.     private $hotelsRepository;
  49.     /**
  50.      * @var HttpClientInterface
  51.      */
  52.     private $client;
  53.     /**
  54.      * @var LoggerInterface
  55.      */
  56.     private $logger;
  57.     /**
  58.      * @var \App\Repository\BlackListRepository
  59.      */
  60.     private BlackListRepository $blackListRepository;
  61.     /**
  62.      * EventsService constructor.
  63.      *
  64.      * @param EntityManagerInterface $em
  65.      * @param StatisticsRepository $statisticsRepository
  66.      * @param HotelsRepository $hotelsRepository
  67.      * @param CruisesRepository $cruisesRepository
  68.      * @param BlackListRepository $blackListRepository
  69.      * @param ManagerLogsRepository $managerLogsRepository
  70.      * @param KernelInterface $kernelInterface
  71.      * @param LoggerInterface $logger
  72.      * @param HttpClientInterface $client
  73.      */
  74.     public function __construct(
  75.         EntityManagerInterface $em,
  76.         StatisticsRepository $statisticsRepository,
  77.         HotelsRepository $hotelsRepository,
  78.         CruisesRepository $cruisesRepository,
  79.         ManagerLogsRepository $managerLogsRepository,
  80.         BlackListRepository $blackListRepository,
  81.         KernelInterface $kernelInterface,
  82.         HttpClientInterface $client,
  83.         LoggerInterface $logger
  84.     ) {
  85.         $this->em                    = $em;
  86.         $this->statisticsRepository  = $statisticsRepository;
  87.         $this->hotelsRepository      = $hotelsRepository;
  88.         $this->cruisesRepository     = $cruisesRepository;
  89.         $this->blackListRepository   = $blackListRepository;
  90.         $this->managerLogsRepository = $managerLogsRepository;
  91.         $this->kernelInterface       = $kernelInterface;
  92.         $this->client                = $client;
  93.         $this->logger                = $logger;
  94.     }
  95.     public function getHotelCountry(Statistics $statistics)
  96.     {
  97.         if ($statistics->getEventAction() == 4) {
  98.             return false;
  99.         }
  100.         /** @var $hotel Hotels */
  101.         $hotel = $this->em->getRepository(Hotels::class)->findOneBy(['recId' => $statistics->getId()]);
  102.         if ($hotel && $hotel->getFcityid()) {
  103.             /** @var $city City */
  104.             $city = $this->em->getRepository(City::class)
  105.                 ->findOneBy(['recId' => $hotel->getFcityid()]);
  106.             if ($city &&$city->getFcountryid()) {
  107.                 /** @var $country Country */
  108.                 $country = $this->em->getRepository(Country::class)
  109.                     ->findOneBy(['recId' => $city->getFcountryid()]);
  110.                 if ($country) return [
  111.                     'hotel' => $hotel,
  112.                     'city' => $city,
  113.                     'country' => $country,
  114.                 ];
  115.             }
  116.         }
  117.         return false;
  118.     }
  119.     public function getOperator(Statistics $statistics)
  120.     {
  121.         if ($statistics->getEventAction() == 3) {
  122.             return false;
  123.         }
  124.         return $this->em->getRepository(Operators::class)
  125.             ->findOneBy(['id' => $statistics->getOperatorId()]);
  126.         ;
  127.     }
  128.     public function getCruiseCountry(Statistics $statistics)
  129.     {
  130.         if ($statistics->getEventAction() != 4) {
  131.             return false;
  132.         }
  133.         return $this->em->getRepository(Cruises::class)
  134.             ->findOneBy(['id' => $statistics->getId()]);
  135.         ;
  136.     }
  137.     public function getStatistics(Statistics $statistics)
  138.     {
  139.         $this->statisticsRepository->_connect();
  140.     }
  141.     public function saveStatistics(Statistics $statistics)
  142.     {
  143.         /*
  144.         if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
  145.             try {
  146.                 $ip = $this->blackListRepository->getByIp(intval($statistics->getIp()));
  147.                 if ($ip) {
  148.                     // Ip в черном списке
  149.                     $statistics->setEventPrice(0);
  150.                     $statistics->setEventPosition(31);
  151.                 }
  152.             } catch (ClickHouseException $e) {
  153.                 $statistics = 'error: '.$e->getMessage();
  154.             }
  155.             if (isset($_COOKIE['DM'])) {
  156.                 header('X-31-blacklist: '.$ip);
  157.             }
  158.         }
  159.         if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
  160.             // проверяем был ли уже такое событие сегодня у этого объявления
  161.             $option    = array(
  162.                 \Memcached::OPT_PREFIX_KEY => (getenv('MEMCACHED_PREFIX') ? getenv(
  163.                     'MEMCACHED_PREFIX'
  164.                 ) : ($_ENV['MEMCACHED_PREFIX'] ?? '')),
  165.             );
  166.             $client = MemcachedAdapter::createConnection(
  167.                 getenv('MEMCACHED_SERV') ? getenv('MEMCACHED_SERV') : $_ENV['MEMCACHED_SERV'],
  168.                 $option
  169.             );
  170.             $cache_key = 'event_'.$statistics->getEventDate()->format('Ymd').'_'.$statistics->getEventAction().'_'.$statistics->getEventSource().'_'.$statistics->getIp();
  171.             if ($event_time = $client->get($cache_key)) {
  172.                 // уникальное событие, если сегодня уже было
  173.                 $statistics->setEventPrice(0);
  174.                 $statistics->setEventPosition(26);
  175.             } else {
  176.                 $client->set($cache_key, time(), time() + (24 * 60 * 60));
  177.             }
  178.         }
  179.         if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
  180.             try {
  181.                 $log = $this->managerLogsRepository->findOneBy(['ip' => $statistics->getIp()]);
  182.                 if ($log && $log->getIp2Long() == $statistics->getIp()) {
  183.                     // игнорируем если это IP менеджера любого из агентства
  184.                     $statistics->setEventPrice(0);
  185.                     $statistics->setEventPosition(27);
  186.                 }
  187.             } catch (Exception $e) {
  188.             }
  189.             if (isset($_COOKIE['DM'])) {
  190.                 header('X-27-isManager: '.$statistics->getIp().'='.($log ? $log->getIp2Long() : -1));
  191.             }
  192.         }
  193.         if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
  194.             try {
  195.                 $events = $this->statisticsRepository->getTotalEventsToDayByIp(intval($statistics->getIp()));
  196.                 if ($events >= 3) {
  197.                     // Не более 3-х платных взаимодействий в сутки с одного IP в не зависимости от объявлений
  198.                     $statistics->setEventPrice(0);
  199.                     $statistics->setEventPosition(29);
  200.                 }
  201.             } catch (ClickHouseException $e) {
  202.                 $events = 'error: '.$e->getMessage();
  203.             }
  204.             if (isset($_COOKIE['DM'])) {
  205.                 header('X-29-events: '.$events.'>='. 3);
  206.             }
  207.         }
  208.         // TODO return if didnt help
  209.         if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
  210.             if (isset($_COOKIE['DM'])) header('X-30-IP: ' . $statistics->getIp() .' = '. Ipv6Service::long2ip($statistics->getIp()));
  211.             try {
  212.                 $countryIso = $this->statisticsRepository->checkCountryIp($statistics->getIp());
  213.                 if ($countryIso && $countryIso != 'UA') {
  214.                     //Если ip не из Украины
  215.                     $statistics->setEventPrice(0);
  216.                     $statistics->setEventPosition(30);
  217.                 }
  218.             } catch (ClickHouseException $e) {
  219.                 $countryIso = 'error: '.$e->getMessage();
  220.             }
  221.             if (isset($_COOKIE['DM'])) header('X-30-CountryCode: ' . $countryIso);
  222.         }
  223.         */
  224.         try {
  225.             $this->statisticsRepository->save($statistics);
  226.         } catch (ClickHouseException $e) {
  227.             if (isset($_COOKIE['DM'])) {
  228.                 header('X-88-error: '.$e->getMessage());
  229.             }
  230.         }
  231.         return $statistics->getEventPrice();
  232.     }
  233.     public function gelLast($limit = 20)
  234.     {
  235.         return $this->statisticsRepository->findAll(10);
  236.     }
  237. }