<?php
namespace App\Service;
use App\EntityOtpusk\Cruises;
use App\EntityOtpusk\Operators;
use App\EntityOtpusk\Statistics;
use App\EntityOtpusk\Thotels as Hotels;
use App\EntityOtpusk\Tcities as City;
use App\EntityOtpusk\Tcountries as Country;
use App\Exception\EventsException;
use App\Repository\BlackListRepository;
use App\Repository\StatisticsRepository;
use App\RepositoryOtpusk\HotelsRepository;
use App\RepositoryOtpusk\CruisesRepository;
use App\Repository\ManagerLogsRepository;
use App\Service\Ipv6Service;
use ClickHouseDB\Exception\ClickHouseException;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Class StatisticsService
* @package App\Service
*/
class StatisticsService
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var StatisticsRepository
*/
private $statisticsRepository;
/**
* @var KernelInterface
*/
private $kernelInterface;
/**
* @var CruisesRepository
*/
private $cruisesRepository;
/**
* @var HotelsRepository
*/
private $hotelsRepository;
/**
* @var HttpClientInterface
*/
private $client;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var \App\Repository\BlackListRepository
*/
private BlackListRepository $blackListRepository;
/**
* EventsService constructor.
*
* @param EntityManagerInterface $em
* @param StatisticsRepository $statisticsRepository
* @param HotelsRepository $hotelsRepository
* @param CruisesRepository $cruisesRepository
* @param BlackListRepository $blackListRepository
* @param ManagerLogsRepository $managerLogsRepository
* @param KernelInterface $kernelInterface
* @param LoggerInterface $logger
* @param HttpClientInterface $client
*/
public function __construct(
EntityManagerInterface $em,
StatisticsRepository $statisticsRepository,
HotelsRepository $hotelsRepository,
CruisesRepository $cruisesRepository,
ManagerLogsRepository $managerLogsRepository,
BlackListRepository $blackListRepository,
KernelInterface $kernelInterface,
HttpClientInterface $client,
LoggerInterface $logger
) {
$this->em = $em;
$this->statisticsRepository = $statisticsRepository;
$this->hotelsRepository = $hotelsRepository;
$this->cruisesRepository = $cruisesRepository;
$this->blackListRepository = $blackListRepository;
$this->managerLogsRepository = $managerLogsRepository;
$this->kernelInterface = $kernelInterface;
$this->client = $client;
$this->logger = $logger;
}
public function getHotelCountry(Statistics $statistics)
{
if ($statistics->getEventAction() == 4) {
return false;
}
/** @var $hotel Hotels */
$hotel = $this->em->getRepository(Hotels::class)->findOneBy(['recId' => $statistics->getId()]);
if ($hotel && $hotel->getFcityid()) {
/** @var $city City */
$city = $this->em->getRepository(City::class)
->findOneBy(['recId' => $hotel->getFcityid()]);
if ($city &&$city->getFcountryid()) {
/** @var $country Country */
$country = $this->em->getRepository(Country::class)
->findOneBy(['recId' => $city->getFcountryid()]);
if ($country) return [
'hotel' => $hotel,
'city' => $city,
'country' => $country,
];
}
}
return false;
}
public function getOperator(Statistics $statistics)
{
if ($statistics->getEventAction() == 3) {
return false;
}
return $this->em->getRepository(Operators::class)
->findOneBy(['id' => $statistics->getOperatorId()]);
;
}
public function getCruiseCountry(Statistics $statistics)
{
if ($statistics->getEventAction() != 4) {
return false;
}
return $this->em->getRepository(Cruises::class)
->findOneBy(['id' => $statistics->getId()]);
;
}
public function getStatistics(Statistics $statistics)
{
$this->statisticsRepository->_connect();
}
public function saveStatistics(Statistics $statistics)
{
/*
if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
try {
$ip = $this->blackListRepository->getByIp(intval($statistics->getIp()));
if ($ip) {
// Ip в черном списке
$statistics->setEventPrice(0);
$statistics->setEventPosition(31);
}
} catch (ClickHouseException $e) {
$statistics = 'error: '.$e->getMessage();
}
if (isset($_COOKIE['DM'])) {
header('X-31-blacklist: '.$ip);
}
}
if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
// проверяем был ли уже такое событие сегодня у этого объявления
$option = array(
\Memcached::OPT_PREFIX_KEY => (getenv('MEMCACHED_PREFIX') ? getenv(
'MEMCACHED_PREFIX'
) : ($_ENV['MEMCACHED_PREFIX'] ?? '')),
);
$client = MemcachedAdapter::createConnection(
getenv('MEMCACHED_SERV') ? getenv('MEMCACHED_SERV') : $_ENV['MEMCACHED_SERV'],
$option
);
$cache_key = 'event_'.$statistics->getEventDate()->format('Ymd').'_'.$statistics->getEventAction().'_'.$statistics->getEventSource().'_'.$statistics->getIp();
if ($event_time = $client->get($cache_key)) {
// уникальное событие, если сегодня уже было
$statistics->setEventPrice(0);
$statistics->setEventPosition(26);
} else {
$client->set($cache_key, time(), time() + (24 * 60 * 60));
}
}
if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
try {
$log = $this->managerLogsRepository->findOneBy(['ip' => $statistics->getIp()]);
if ($log && $log->getIp2Long() == $statistics->getIp()) {
// игнорируем если это IP менеджера любого из агентства
$statistics->setEventPrice(0);
$statistics->setEventPosition(27);
}
} catch (Exception $e) {
}
if (isset($_COOKIE['DM'])) {
header('X-27-isManager: '.$statistics->getIp().'='.($log ? $log->getIp2Long() : -1));
}
}
if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
try {
$events = $this->statisticsRepository->getTotalEventsToDayByIp(intval($statistics->getIp()));
if ($events >= 3) {
// Не более 3-х платных взаимодействий в сутки с одного IP в не зависимости от объявлений
$statistics->setEventPrice(0);
$statistics->setEventPosition(29);
}
} catch (ClickHouseException $e) {
$events = 'error: '.$e->getMessage();
}
if (isset($_COOKIE['DM'])) {
header('X-29-events: '.$events.'>='. 3);
}
}
// TODO return if didnt help
if ($statistics->getEventPrice() > 0 || isset($_COOKIE['DM'])) {
if (isset($_COOKIE['DM'])) header('X-30-IP: ' . $statistics->getIp() .' = '. Ipv6Service::long2ip($statistics->getIp()));
try {
$countryIso = $this->statisticsRepository->checkCountryIp($statistics->getIp());
if ($countryIso && $countryIso != 'UA') {
//Если ip не из Украины
$statistics->setEventPrice(0);
$statistics->setEventPosition(30);
}
} catch (ClickHouseException $e) {
$countryIso = 'error: '.$e->getMessage();
}
if (isset($_COOKIE['DM'])) header('X-30-CountryCode: ' . $countryIso);
}
*/
try {
$this->statisticsRepository->save($statistics);
} catch (ClickHouseException $e) {
if (isset($_COOKIE['DM'])) {
header('X-88-error: '.$e->getMessage());
}
}
return $statistics->getEventPrice();
}
public function gelLast($limit = 20)
{
return $this->statisticsRepository->findAll(10);
}
}