<?php

namespace App\EntityListener;

use App\Entity\Blog;
use Doctrine\ORM\Event\LifecycleEventArgs;
use JetBrains\PhpStorm\NoReturn;
use Symfony\Component\String\Slugger\SluggerInterface;

/**
 * Class BlogEntityListener
 * @package App\EntityListener
 */
class BlogEntityListener
{
	private SluggerInterface $slugger;
	
	public function __construct(SluggerInterface $slugger)
	{
		$this->slugger = $slugger;
	}
	
	/**
	 * @param \App\Entity\Blog                       $blog
	 * @param \Doctrine\ORM\Event\LifecycleEventArgs $title
	 */
	#[NoReturn]
	public function prePersist(Blog $blog, LifecycleEventArgs $title)
	{
		$blog->computeSlug($this->slugger);
	}
	
	/**
	 * @param \App\Entity\Blog                       $blog
	 * @param \Doctrine\ORM\Event\LifecycleEventArgs $title
	 */
	#[NoReturn]
	public function preUpdate(Blog $blog, LifecycleEventArgs $title)
	{
		//dd($title);
		$blog->computeSlug($this->slugger);
	}
}