Watcher to track changes in form. preSubmit - has access to object properties & request parameters postSubmit - only access to object (merged)
<?php
namespace AppBundle\Form\EventListener;
use AppBundle\Entity\Notification;
use AppBundle\Entity\Notification\NotificationAware;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
/**
* Class NotificationChangesAdminWatcher
* @package AppBundle\Form\EventListener
*/
class NotificationChangesAdminWatcher implements EventSubscriberInterface
{
/**
* @var Collection
*/
protected $submittedNotifications;
// /**
// * @var Collection
// */
// protected $notificationSnapshot;
// /**
// * @var Collection
// */
// protected $notificationRemoved;
// /**
// * @var Collection
// */
// protected $notificationAdded;
// /**
// * @var Collection
// */
// protected $notificationUpdated;
/**
* Enumerates events this subscriber subscribed on
*/
public static function getSubscribedEvents()
{
return [
// FormEvents::PRE_SUBMIT => 'onPreSubmit',
FormEvents::POST_SUBMIT => 'onPostSubmit',
];
}
// /**
// * @param FormEvent $event
// */
public function onPreSubmit(FormEvent $event)
{
// $notificationSnapshot = $event->getForm()->getData()->getNotifications();
//
// $notificationRequestArray = $event->getData()['notifications'];
//
//
// $qw = 2;
//
}
/**
* @param FormEvent $event
*/
public function onPostSubmit(FormEvent $event)
{
if ($event->getForm()->isValid()) {
$submittedObject = $event->getData();
if ($submittedObject instanceof NotificationAware) {
/** @var Notification $notification */
foreach ($submittedObject->getNotifications() as $notification) {
$notification->sniffStartType();
}
}
}
}
/**
* @return Collection
*/
public function getSubmittedNotifications()
{
return $this->submittedNotifications;
}
// /**
// * @return Collection
// */
// public function getNotificationAdded()
// {
// return $this->notificationAdded;
// }
//
// /**
// * @return Collection
// */
// public function getNotificationRemoved()
// {
// return $this->notificationRemoved;
// }
//
// /**
// * @return Collection
// */
// public function getNotificationUpdated()
// {
// return $this->notificationUpdated;
// }
//
// /**
// * @return Collection
// */
// public function getNotificationSnapshot()
// {
// return $this->notificationSnapshot;
// }
// /**
// * @param Collection $notificationChanges
// */
// protected function mergeRequestChangesToNotifications(Collection $notificationChanges)
// {
// $this->notificationUpdated = new ArrayCollection();
//
// $snapshot = $this->getNotificationSnapshot();
//
// $minElementsLength = min($notificationChanges->count(), $snapshot->count());
// for ($i = 0; $i <= $minElementsLength- 1; $i++) {
// $notification = $snapshot->get($i);
// $changeSet = $notificationChanges->get($i);
//
// foreach ($changeSet as $key => $currentFieldValue) {
// if (! is_callable([$notification, 'get' . ucfirst($key)])) {
// continue;
// }
//
// $oldFieldValue = call_user_func([$notification, 'get' . ucfirst($key)]);
//
// if (! $this->fieldsEqual($oldFieldValue, $currentFieldValue)) {
// if (is_callable([$notification, 'set' . ucfirst($key)])) {
// call_user_func([$notification, 'set' . ucfirst($key)], $currentFieldValue);
// $this->notificationUpdated->add($notification);
// }
//
// }
// }
// }
// }
//
// /**
// * @param Collection $submittedNotifications
// */
// protected function countRemovedNotifications(Collection $submittedNotifications)
// {
// $diffRemoved = array_udiff_assoc(
// $this->notificationSnapshot->toArray(),
// $submittedNotifications->toArray(),
// function($a, $b) { return $a === $b ? 0 : 1; }
// );
//
// $this->notificationRemoved = new ArrayCollection($diffRemoved);
// }
//
// /**
// * @param Collection $submittedNotifications
// */
// protected function countAddedNotifications(Collection $submittedNotifications)
// {
// $diffAdded = array_udiff_assoc(
// $submittedNotifications->toArray(),
// $this->notificationSnapshot->toArray(),
// function($a, $b) { return $a === $b ? 0 : 1; }
// );
//
// $this->notificationAdded = new ArrayCollection($diffAdded);
// }
//
// /**
// * @param $oldFieldValue
// * @param $currentFieldValue
// * @return bool
// */
// private function fieldsEqual($oldFieldValue, $currentFieldValue)
// {
// if ($oldFieldValue instanceof \DateTime || $currentFieldValue instanceof \DateTime) {
// is_string($oldFieldValue)
// ? $oldFieldValue = new \DateTime($oldFieldValue)
// : $currentFieldValue = new \DateTime($currentFieldValue);
// }
//
// return $oldFieldValue == $currentFieldValue;
// }
}