<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\CoreBundle\EventListener\Frontend;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* @internal
*/
class GoogleSearchConsoleVerificationListener implements EventSubscriberInterface
{
use PimcoreContextAwareTrait;
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 64],
];
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!$event->isMainRequest()) {
return;
}
if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
return;
}
$conf = \Pimcore\Config::getReportConfig();
if (!is_null($conf->get('google_search_console')) && isset($conf->get('google_search_console')->sites)) {
$sites = $conf->get('google_search_console')->sites->toArray();
if (is_array($sites)) {
foreach ($sites as $site) {
if ($site['verification'] && $request->getPathInfo() === '/' . $site['verification']) {
$response = new Response('google-site-verification: ' . $site['verification']);
$event->setResponse($response);
break;
}
}
}
}
}
}