vendor/crehler/mojebambino-catalog/src/Subscriber/CatalogRawGeneratorSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2020 Crehler Sp. z o. o.
  5.  * @link https://crehler.com/
  6.  * @support support@crehler.com
  7.  *
  8.  * @author Mateusz FlasiƄski
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13. namespace Crehler\Catalog\Subscriber;
  14. use Crehler\Catalog\Event\CatalogMediaGeneratedEvent;
  15. use Crehler\Catalog\Service\Indexer\CatalogBookmarkIndexerEvent;
  16. use Crehler\Catalog\Service\Indexer\CatalogIndexerEvent;
  17. use Crehler\Catalog\Service\Indexer\CatalogPageIndexerEvent;
  18. use Doctrine\DBAL\Connection;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use function Symfony\Component\String\u;
  21. class CatalogRawGeneratorSubscriber implements EventSubscriberInterface
  22. {
  23.     private Connection $connection;
  24.     public function __construct(Connection $connection)
  25.     {
  26.         $this->connection $connection;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             CatalogMediaGeneratedEvent::class => 'onMediaGenerated',
  32.             CatalogBookmarkIndexerEvent::class => 'indexBookmarks',
  33.             CatalogPageIndexerEvent::class => 'indexPage',
  34.             CatalogIndexerEvent::class => 'indexCatalog',
  35.         ];
  36.     }
  37.     public function onMediaGenerated(CatalogMediaGeneratedEvent $event)
  38.     {
  39.         $path u($event->getPath());
  40.         if ($path->startsWith('thumbnail')) {
  41.             $path u($event->getPath())->afterLast('/')->beforeLast('_')->toString();
  42.         } else {
  43.             $path u($event->getPath())->afterLast('/')->beforeLast('.')->toString();
  44.         }
  45.         $catalogId $this->findPageByThumbnail($path);
  46.         if (!is_string($catalogId)) {
  47.             return;
  48.         }
  49.     }
  50.     public function indexBookmarks(CatalogBookmarkIndexerEvent $event)
  51.     {
  52.     }
  53.     public function indexCatalog(CatalogIndexerEvent $event)
  54.     {
  55.     }
  56.     public function indexPage(CatalogPageIndexerEvent $event)
  57.     {
  58.     }
  59.     private function findPageByThumbnail(string $path)
  60.     {
  61.         return $this->connection
  62.             ->createQueryBuilder()
  63.             ->select('LOWER(HEX(cp.catalog_id))')
  64.             ->from('catalog_page''cp')
  65.             ->innerJoin('cp''media''m''m.id = cp.media_id')
  66.             ->where('m.file_name LIKE :name')
  67.             ->setParameter('name'"%$path%")
  68.             ->execute()
  69.             ->fetchColumn();
  70.     }
  71. }