Skip to content

Instantly share code, notes, and snippets.

@MaximStrutinskiy
Created September 18, 2017 17:10
Show Gist options
  • Save MaximStrutinskiy/7118fce3f95291123b884e927aa6d235 to your computer and use it in GitHub Desktop.
Save MaximStrutinskiy/7118fce3f95291123b884e927aa6d235 to your computer and use it in GitHub Desktop.
Event Subscribers in Silex
$app->extend('dispatcher', function ($dispatcher) use ($app) {
$dispatcher->addSubscriber(new \APP\EventSubscriber\ContributionSubscriber($app['orm.em']));
return $dispatcher;
});
public function createNewContributionAction(Request $request, int $userReviewId, int $review_id)
{
$user = $this->getApp()['user'];
$userReview = $this->getRepository(UserReview::class)->findBy(['id' => $userReviewId]);
$userReview = $userReview[0];
$form = $this->getFormFactory()->createBuilder(CreateContributionType::class)->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
if (isset($_POST['save'])) {
$data = $form->getData();
$data->setUserReview($userReview);
$this->getEntityManager()->persist($data);
$this->getEntityManager()->flush();
$dispatcher = $this->getApp()['dispatcher'];
$dispatcher->dispatch(ContributionEvent::CONTRIBUTION_ADDED, new ContributionEvent($data, $user));
return $this->backToUserReview($review_id);
}
}
return $this->render('create_new_review.html.twig', ['form' => $form->createView()]);
}
class ContributionEvent extends Event
{
const CONTRIBUTION_ADDED = 'contribution_added';
const CONTRIBUTION_UPDATED = 'contribution_updated';
const CONTRIBUTION_DELETED = 'contribution_deleted';
private $contribution;
private $user;
public function __construct(Contribution $contribution, User $user)
{
$this->contribution = $contribution;
$this->user = $user;
}
/**
* @return Contribution
*/
public function getContribution(): Contribution
{
return $this->contribution;
}
/**
* @return User
*/
public function getUser(): User
{
return $this->user;
}
}
class ContributionSubscriber implements EventSubscriberInterface
{
/**
* Default action for logs
*/
const UNKNOWN_ACTION = 'unknown_action';
/**
* @var EntityManager
*/
protected $entityManager;
/**
* AbstractSubscriber constructor.
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @param RecentActivity $recentActivity
*/
protected function logEntity(RecentActivity $recentActivity)
{
$this->entityManager->merge($recentActivity);
$this->entityManager->flush();
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ContributionEvent::CONTRIBUTION_ADDED => 'onContributionAdded',
ContributionEvent::CONTRIBUTION_UPDATED => 'onContributionUpdated',
ContributionEvent::CONTRIBUTION_DELETED => 'onContributionDeleted'
];
}
/**
* @param ContributionEvent $event
*/
public function onContributionAdded(ContributionEvent $event)
{
$entity = new RecentActivity();
$entity->setUser($event->getUser());
$entity->setContribution($event->getContribution());
$this->logEntity($entity);
}
/**
* @param ContributionEvent $event
*/
public function onContributionUpdated(ContributionEvent $event)
{
$this->logEntity(ContributionEvent::CONTRIBUTION_UPDATED, [
'contribution' => $event->getEntity()->getContribution()
]);
}
/**
* @param ContributionEvent $event
*/
public function onContributionDeleted(ContributionEvent $event)
{
$this->logEntity(ContributionEvent::CONTRIBUTION_DELETED, [
'contribution' => $event->getEntity()->getContribution()
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment