2020-02-02 21:24:29 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-02-02 21:24:29 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2020-02-02 21:24:29 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-02-02 21:24:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
|
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry;
|
|
|
|
use App\Services\LogSystem\EventLogger;
|
|
|
|
use Doctrine\Common\EventSubscriber;
|
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs;
|
|
|
|
use Doctrine\ORM\Events;
|
2020-02-16 23:48:57 +01:00
|
|
|
use Doctrine\ORM\UnitOfWork;
|
2020-02-02 21:24:29 +01:00
|
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
2020-02-16 23:48:57 +01:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2020-02-02 21:24:29 +01:00
|
|
|
|
|
|
|
class EventLoggerSubscriber implements EventSubscriber
|
|
|
|
{
|
|
|
|
protected $logger;
|
2020-02-16 23:48:57 +01:00
|
|
|
protected $serializer;
|
2020-02-02 21:24:29 +01:00
|
|
|
|
2020-02-16 23:48:57 +01:00
|
|
|
public function __construct(EventLogger $logger, SerializerInterface $serializer)
|
2020-02-02 21:24:29 +01:00
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
2020-02-16 23:48:57 +01:00
|
|
|
$this->serializer = $serializer;
|
2020-02-02 21:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onFlush(OnFlushEventArgs $eventArgs)
|
|
|
|
{
|
|
|
|
$em = $eventArgs->getEntityManager();
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log changes and deletions of entites.
|
|
|
|
* We can not log persist here, because the entities do not have IDs yet...
|
|
|
|
*/
|
|
|
|
|
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) {
|
|
|
|
if ($this->validEntity($entity)) {
|
|
|
|
$log = new ElementEditedLogEntry($entity);
|
2020-02-16 23:48:57 +01:00
|
|
|
$this->saveChangeSet($entity, $log, $uow);
|
2020-02-02 21:24:29 +01:00
|
|
|
$this->logger->log($log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
|
|
|
if ($this->validEntity($entity)) {
|
|
|
|
$log = new ElementDeletedLogEntry($entity);
|
2020-02-16 23:48:57 +01:00
|
|
|
$this->saveChangeSet($entity, $log, $uow);
|
2020-02-02 21:24:29 +01:00
|
|
|
$this->logger->log($log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$uow->computeChangeSets();
|
|
|
|
}
|
|
|
|
|
2020-02-16 23:48:57 +01:00
|
|
|
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, UnitOfWork $uow): void
|
|
|
|
{
|
|
|
|
if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) {
|
|
|
|
throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$changeSet = $uow->getEntityChangeSet($entity);
|
|
|
|
dump($changeSet);
|
|
|
|
$old_data = array_diff(array_combine(array_keys($changeSet), array_column($changeSet, 0)), [null]);
|
|
|
|
$logEntry->setOldData($old_data);
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:24:29 +01:00
|
|
|
public function postPersist(LifecycleEventArgs $args)
|
|
|
|
{
|
|
|
|
//Create an log entry
|
|
|
|
|
|
|
|
/** @var AbstractDBElement $entity */
|
|
|
|
$entity = $args->getObject();
|
|
|
|
if ($this->validEntity($entity)) {
|
|
|
|
$log = new ElementCreatedLogEntry($entity);
|
|
|
|
$this->logger->log($log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postFlush(PostFlushEventArgs $args)
|
|
|
|
{
|
|
|
|
$em = $args->getEntityManager();
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
|
|
// If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here.
|
|
|
|
if ($uow->hasPendingInsertions()) {
|
|
|
|
$em->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given entity can be logged.
|
|
|
|
* @param object $entity
|
|
|
|
* @return bool True, if the given entity can be logged.
|
|
|
|
*/
|
|
|
|
protected function validEntity(object $entity): bool
|
|
|
|
{
|
|
|
|
//Dont log logentries itself!
|
|
|
|
if ($entity instanceof AbstractDBElement && !$entity instanceof AbstractLogEntry) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return[
|
|
|
|
Events::onFlush,
|
|
|
|
Events::postPersist,
|
|
|
|
Events::postFlush
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|