<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Subscriber;
use Crehler\Catalog\Event\CatalogMediaGeneratedEvent;
use Crehler\Catalog\Service\Indexer\CatalogBookmarkIndexerEvent;
use Crehler\Catalog\Service\Indexer\CatalogIndexerEvent;
use Crehler\Catalog\Service\Indexer\CatalogPageIndexerEvent;
use Doctrine\DBAL\Connection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function Symfony\Component\String\u;
class CatalogRawGeneratorSubscriber implements EventSubscriberInterface
{
private Connection $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public static function getSubscribedEvents()
{
return [
CatalogMediaGeneratedEvent::class => 'onMediaGenerated',
CatalogBookmarkIndexerEvent::class => 'indexBookmarks',
CatalogPageIndexerEvent::class => 'indexPage',
CatalogIndexerEvent::class => 'indexCatalog',
];
}
public function onMediaGenerated(CatalogMediaGeneratedEvent $event)
{
$path = u($event->getPath());
if ($path->startsWith('thumbnail')) {
$path = u($event->getPath())->afterLast('/')->beforeLast('_')->toString();
} else {
$path = u($event->getPath())->afterLast('/')->beforeLast('.')->toString();
}
$catalogId = $this->findPageByThumbnail($path);
if (!is_string($catalogId)) {
return;
}
}
public function indexBookmarks(CatalogBookmarkIndexerEvent $event)
{
}
public function indexCatalog(CatalogIndexerEvent $event)
{
}
public function indexPage(CatalogPageIndexerEvent $event)
{
}
private function findPageByThumbnail(string $path)
{
return $this->connection
->createQueryBuilder()
->select('LOWER(HEX(cp.catalog_id))')
->from('catalog_page', 'cp')
->innerJoin('cp', 'media', 'm', 'm.id = cp.media_id')
->where('m.file_name LIKE :name')
->setParameter('name', "%$path%")
->execute()
->fetchColumn();
}
}