Spaces:
No application file
No application file
File size: 4,945 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
<?php
namespace Mautic\PageBundle\Model;
use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Tracker\ContactTracker;
use Mautic\PageBundle\Entity\VideoHit;
use Mautic\PageBundle\Entity\VideoHitRepository;
use Mautic\PageBundle\Event\VideoHitEvent;
use Mautic\PageBundle\PageEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @extends FormModel<VideoHit>
*/
class VideoModel extends FormModel
{
public function __construct(
protected IpLookupHelper $ipLookupHelper,
protected ContactTracker $contactTracker,
EntityManager $em,
CorePermissions $security,
EventDispatcherInterface $dispatcher,
UrlGeneratorInterface $router,
Translator $translator,
UserHelper $userHelper,
LoggerInterface $mauticLogger,
CoreParametersHelper $coreParametersHelper
) {
parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $mauticLogger, $coreParametersHelper);
}
public function getHitRepository(): VideoHitRepository
{
return $this->em->getRepository(VideoHit::class);
}
public function getPermissionBase(): string
{
return 'page:pages';
}
public function getNameGetter(): string
{
return 'getTitle';
}
/**
* @param string $guid
*
* @return VideoHit
*/
public function getHitForLeadByGuid(Lead $lead, $guid)
{
return $this->getHitRepository()->getHitForLeadByGuid($lead, $guid);
}
/**
* @param Request $request
* @param string $code
*
* @throws \Doctrine\ORM\ORMException
* @throws \Exception
*/
public function hitVideo($request, $code = '200'): void
{
// don't skew results with in-house hits
if (!$this->security->isAnonymous()) {
// return;
}
$lead = $this->contactTracker->getContact();
$guid = $request->get('guid');
$hit = ($lead) ? $this->getHitForLeadByGuid($lead, $guid) : new VideoHit();
$hit->setGuid($guid);
$hit->setDateHit(new \DateTime());
$hit->setDuration($request->get('duration'));
$hit->setUrl($request->get('url'));
$hit->setTimeWatched($request->get('total_watched'));
// check for existing IP
$ipAddress = $this->ipLookupHelper->getIpAddress();
$hit->setIpAddress($ipAddress);
// Store query array
$query = $request->query->all();
unset($query['d']);
$hit->setQuery($query);
if ($lead) {
$hit->setLead($lead);
}
// glean info from the IP address
if ($details = $ipAddress->getIpDetails()) {
$hit->setCountry($details['country']);
$hit->setRegion($details['region']);
$hit->setCity($details['city']);
$hit->setIsp($details['isp']);
$hit->setOrganization($details['organization']);
}
$hit->setCode($code);
if (!$hit->getReferer()) {
$hit->setReferer($request->server->get('HTTP_REFERER'));
}
$hit->setUserAgent($request->server->get('HTTP_USER_AGENT'));
$hit->setRemoteHost($request->server->get('REMOTE_HOST'));
// get a list of the languages the user prefers
$browserLanguages = $request->server->get('HTTP_ACCEPT_LANGUAGE');
if (!empty($browserLanguages)) {
$languages = explode(',', $browserLanguages);
foreach ($languages as $k => $l) {
if (($pos = strpos(';q=', $l)) !== false) {
// remove weights
$languages[$k] = substr($l, 0, $pos);
}
}
$hit->setBrowserLanguages($languages);
}
// Wrap in a try/catch to prevent deadlock errors on busy servers
try {
$this->em->persist($hit);
$this->em->flush();
} catch (\Exception $exception) {
if (MAUTIC_ENV === 'dev') {
throw $exception;
} else {
$this->logger->error(
$exception->getMessage(),
['exception' => $exception]
);
}
}
if ($this->dispatcher->hasListeners(PageEvents::VIDEO_ON_HIT)) {
$event = new VideoHitEvent($hit, $request, $code);
$this->dispatcher->dispatch($event, PageEvents::VIDEO_ON_HIT);
}
}
}
|