mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-12 11:24:31 +02:00
Fixed coding style.
This commit is contained in:
parent
24939f2342
commit
fd61c8d9e2
129 changed files with 962 additions and 1091 deletions
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
|
@ -21,7 +24,6 @@
|
|||
|
||||
namespace App\Services\LogSystem;
|
||||
|
||||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
|
@ -47,8 +49,10 @@ class TimeTravel
|
|||
|
||||
/**
|
||||
* Undeletes the element with the given ID.
|
||||
* @param string $class The class name of the element that should be undeleted
|
||||
* @param int $id The ID of the element that should be undeleted.
|
||||
*
|
||||
* @param string $class The class name of the element that should be undeleted
|
||||
* @param int $id The ID of the element that should be undeleted.
|
||||
*
|
||||
* @return AbstractDBElement
|
||||
*/
|
||||
public function undeleteEntity(string $class, int $id): AbstractDBElement
|
||||
|
@ -61,21 +65,21 @@ class TimeTravel
|
|||
$this->setField($element, 'id', $id);
|
||||
|
||||
//Let database determine when it will be created
|
||||
$this->setField($element,'addedDate', null);
|
||||
$this->setField($element, 'addedDate', null);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert the given element to the state it has on the given timestamp
|
||||
* @param AbstractDBElement $element
|
||||
* @param \DateTime $timestamp
|
||||
* @param AbstractLogEntry[] $reverted_elements
|
||||
* Revert the given element to the state it has on the given timestamp.
|
||||
*
|
||||
* @param AbstractLogEntry[] $reverted_elements
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function revertEntityToTimestamp(AbstractDBElement $element, \DateTime $timestamp, array $reverted_elements = [])
|
||||
public function revertEntityToTimestamp(AbstractDBElement $element, \DateTime $timestamp, array $reverted_elements = []): void
|
||||
{
|
||||
if (!$element instanceof TimeStampableInterface) {
|
||||
if (! $element instanceof TimeStampableInterface) {
|
||||
throw new \InvalidArgumentException('$element must have a Timestamp!');
|
||||
}
|
||||
|
||||
|
@ -84,7 +88,7 @@ class TimeTravel
|
|||
}
|
||||
|
||||
//Skip this process if already were reverted...
|
||||
if (in_array($element, $reverted_elements)) {
|
||||
if (in_array($element, $reverted_elements, true)) {
|
||||
return;
|
||||
}
|
||||
$reverted_elements[] = $element;
|
||||
|
@ -122,35 +126,34 @@ class TimeTravel
|
|||
$associations = $metadata->getAssociationMappings();
|
||||
foreach ($associations as $field => $mapping) {
|
||||
if (
|
||||
($element instanceof AbstractStructuralDBElement && ($field === 'parts' || $field === 'children'))
|
||||
|| ($element instanceof AttachmentType && $field === 'attachments')
|
||||
($element instanceof AbstractStructuralDBElement && ('parts' === $field || 'children' === $field))
|
||||
|| ($element instanceof AttachmentType && 'attachments' === $field)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
//Revert many to one association (one element in property)
|
||||
if (
|
||||
$mapping['type'] === ClassMetadata::MANY_TO_ONE
|
||||
|| $mapping['type'] === ClassMetadata::ONE_TO_ONE
|
||||
ClassMetadata::MANY_TO_ONE === $mapping['type']
|
||||
|| ClassMetadata::ONE_TO_ONE === $mapping['type']
|
||||
) {
|
||||
$target_element = $this->getField($element, $field);
|
||||
if ($target_element !== null && $element->getLastModified() > $timestamp) {
|
||||
if (null !== $target_element && $element->getLastModified() > $timestamp) {
|
||||
$this->revertEntityToTimestamp($target_element, $timestamp, $reverted_elements);
|
||||
}
|
||||
} elseif ( //Revert *_TO_MANY associations (collection properties)
|
||||
($mapping['type'] === ClassMetadata::MANY_TO_MANY
|
||||
|| $mapping['type'] === ClassMetadata::ONE_TO_MANY)
|
||||
&& $mapping['isOwningSide'] === false
|
||||
(ClassMetadata::MANY_TO_MANY === $mapping['type']
|
||||
|| ClassMetadata::ONE_TO_MANY === $mapping['type'])
|
||||
&& false === $mapping['isOwningSide']
|
||||
) {
|
||||
$target_elements = $this->getField($element, $field);
|
||||
if ($target_elements === null || count($target_elements) > 10) {
|
||||
if (null === $target_elements || count($target_elements) > 10) {
|
||||
continue;
|
||||
}
|
||||
foreach ($target_elements as $target_element) {
|
||||
if ($target_element !== null && $element->getLastModified() >= $timestamp) {
|
||||
if (null !== $target_element && $element->getLastModified() >= $timestamp) {
|
||||
//Remove the element from collection, if it did not existed at $timestamp
|
||||
if (!$this->repo->getElementExistedAtTimestamp($target_element, $timestamp)) {
|
||||
if (! $this->repo->getElementExistedAtTimestamp($target_element, $timestamp)) {
|
||||
if ($target_elements instanceof Collection) {
|
||||
$target_elements->removeElement($target_element);
|
||||
}
|
||||
|
@ -159,23 +162,21 @@ class TimeTravel
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the changeset in the given LogEntry to the element
|
||||
* @param AbstractDBElement $element
|
||||
* @param TimeTravelInterface $logEntry
|
||||
* Apply the changeset in the given LogEntry to the element.
|
||||
*
|
||||
* @throws \Doctrine\ORM\Mapping\MappingException
|
||||
*/
|
||||
public function applyEntry(AbstractDBElement $element, TimeTravelInterface $logEntry): void
|
||||
{
|
||||
//Skip if this does not provide any info...
|
||||
if (!$logEntry->hasOldDataInformations()) {
|
||||
if (! $logEntry->hasOldDataInformations()) {
|
||||
return;
|
||||
}
|
||||
if (!$element instanceof TimeStampableInterface) {
|
||||
if (! $element instanceof TimeStampableInterface) {
|
||||
return;
|
||||
}
|
||||
$metadata = $this->em->getClassMetadata(get_class($element));
|
||||
|
@ -204,14 +205,15 @@ class TimeTravel
|
|||
$reflection = new \ReflectionClass(get_class($element));
|
||||
$property = $reflection->getProperty($field);
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property->getValue($element);
|
||||
}
|
||||
|
||||
protected function setField(AbstractDBElement $element, string $field, $new_value)
|
||||
protected function setField(AbstractDBElement $element, string $field, $new_value): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(get_class($element));
|
||||
$property = $reflection->getProperty($field);
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($element, $new_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue