Last active
July 10, 2019 11:11
-
-
Save ArchTaqi/e574cc54d8014ba6e17fe19364ab8f98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\EventListener; | |
use App\Event\RepositoryEvent; | |
use App\Services\PostService; | |
use App\Entity\Post; | |
/** | |
* Class PostListener | |
* @package App\EventListener | |
*/ | |
class PostListener | |
{ | |
/** | |
* @var RepositoryEvent | |
*/ | |
protected $event; | |
/** | |
* @var PostService | |
*/ | |
private $postService; | |
/** | |
* PostListener constructor. | |
* @param PostService $postService | |
*/ | |
public function __construct(PostService $postService) | |
{ | |
$this->postService = $postService; | |
} | |
/** | |
* @param RepositoryEvent $event | |
*/ | |
public function preValidate(RepositoryEvent $event) | |
{ | |
if (!$event->getData() instanceof Post) { | |
return ; | |
} | |
/** | |
* @var Post $post | |
*/ | |
$post = $event->getData(); | |
switch ($event->getAction()) { | |
case 'create': | |
$this->postService->newPost($post); | |
break; | |
case 'update': | |
$this->postService->newPost($post); | |
break; | |
case 'delete': | |
break; | |
default: | |
} | |
} | |
/** | |
* @param RepositoryEvent $event | |
*/ | |
public function onPreFlush(RepositoryEvent $event) | |
{ | |
if (!$event->getData() instanceof Post) { | |
return ; | |
} | |
/** | |
* @var Post $post | |
*/ | |
$post = $event->getData(); | |
switch ($event->getAction()) { | |
case 'create': | |
$this->postService->newPost($post); | |
break; | |
case 'update': | |
$this->postService->newPost($post); | |
break; | |
case 'delete': | |
break; | |
default: | |
} | |
} | |
/** | |
* @param RepositoryEvent $event | |
*/ | |
public function onPostFlush(RepositoryEvent $event) | |
{ | |
if (!$event->getData() instanceof Post) { | |
return ; | |
} | |
/** | |
* @var Post $post | |
*/ | |
$post = $event->getData(); | |
switch ($event->getAction()) { | |
case 'create': | |
$this->postService->newPost($post); | |
break; | |
case 'update': | |
$this->postService->newPost($post); | |
break; | |
case 'delete': | |
break; | |
default: | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment