Created
September 18, 2017 17:10
-
-
Save MaximStrutinskiy/7118fce3f95291123b884e927aa6d235 to your computer and use it in GitHub Desktop.
Event Subscribers in Silex
This file contains hidden or 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
$app->extend('dispatcher', function ($dispatcher) use ($app) { | |
$dispatcher->addSubscriber(new \APP\EventSubscriber\ContributionSubscriber($app['orm.em'])); | |
return $dispatcher; | |
}); |
This file contains hidden or 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
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()]); | |
} |
This file contains hidden or 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
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; | |
} | |
} |
This file contains hidden or 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
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