Fixed coding style.

This commit is contained in:
Jan Böhmer 2020-03-15 13:56:31 +01:00
parent 24939f2342
commit fd61c8d9e2
129 changed files with 962 additions and 1091 deletions

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -20,7 +23,6 @@
namespace App\Services\LogSystem;
class EventCommentHelper
{
protected const MAX_MESSAGE_LENGTH = 255;
@ -36,7 +38,6 @@ class EventCommentHelper
* Set the message that will be saved for all ElementEdited/Created/Deleted messages during the next flush.
* Set to null if no message should be shown.
* After the flush this message is cleared.
* @param string|null $message
*/
public function setMessage(?string $message): void
{
@ -46,6 +47,7 @@ class EventCommentHelper
/**
* Returns the currently set message, or null if no message is set yet.
*
* @return string|null
*/
public function getMessage(): ?string
@ -63,10 +65,11 @@ class EventCommentHelper
/**
* Check if a message is currently set.
*
* @return bool
*/
public function isMessageSet(): bool
{
return is_string($this->message);
}
}
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -20,7 +23,6 @@
namespace App\Services\LogSystem;
use App\Entity\LogSystem\AbstractLogEntry;
class EventUndoHelper
@ -41,7 +43,7 @@ class EventUndoHelper
public function setMode(string $mode): void
{
if (!in_array($mode, self::ALLOWED_MODES)) {
if (! in_array($mode, self::ALLOWED_MODES, true)) {
throw new \InvalidArgumentException('Invalid mode passed!');
}
$this->mode = $mode;
@ -55,7 +57,6 @@ class EventUndoHelper
/**
* Set which event log is currently undone.
* After the flush this message is cleared.
* @param AbstractLogEntry|null $undone_event
*/
public function setUndoneEvent(?AbstractLogEntry $undone_event): void
{
@ -64,6 +65,7 @@ class EventUndoHelper
/**
* Returns event that is currently undone.
*
* @return AbstractLogEntry|null
*/
public function getUndoneEvent(): ?AbstractLogEntry
@ -80,11 +82,12 @@ class EventUndoHelper
}
/**
* Check if a event is undone
* Check if a event is undone.
*
* @return bool
*/
public function isUndo(): bool
{
return ($this->undone_event instanceof AbstractLogEntry);
return $this->undone_event instanceof AbstractLogEntry;
}
}
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -20,7 +23,6 @@
namespace App\Services\LogSystem;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Part;
@ -29,13 +31,12 @@ class HistoryHelper
{
public function __construct()
{
}
/**
* Returns an array containing all elements that are associated with the argument.
* The returned array contains the given element.
* @param AbstractDBElement $element
*
* @return array
*/
public function getAssociatedElements(AbstractDBElement $element): array
@ -58,4 +59,4 @@ class HistoryHelper
return $array;
}
}
}

View file

@ -56,7 +56,6 @@ use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Entity\LogSystem\UserNotAllowedLogEntry;
use App\Services\ElementTypeNameGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
@ -64,12 +63,11 @@ use Symfony\Contracts\Translation\TranslatorInterface;
*/
class LogEntryExtraFormatter
{
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>'];
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>'];
protected $translator;
protected $elementTypeNameGenerator;
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>', ];
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>'];
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator)
{
$this->translator = $translator;
@ -90,15 +88,40 @@ class LogEntryExtraFormatter
foreach ($arr as $key => $value) {
$str = '';
if (is_string($key)) {
$str .= '<error>' . $this->translator->trans($key) . '</error>: ';
$str .= '<error>'.$this->translator->trans($key).'</error>: ';
}
$str .= $value;
if (!empty($str)) {
if (! empty($str)) {
$tmp[] = $str;
}
}
return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode("; ", $tmp));
return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode('; ', $tmp));
}
/**
* Return a HTML formatted string containing a user viewable form of the Extra data.
*
* @return string
*/
public function format(AbstractLogEntry $context): string
{
$arr = $this->getInternalFormat($context);
$tmp = [];
//Make an array with entries in the form "<b>Key:</b> Value"
foreach ($arr as $key => $value) {
$str = '';
if (is_string($key)) {
$str .= '<b>'.$this->translator->trans($key).'</b>: ';
}
$str .= $value;
if (! empty($str)) {
$tmp[] = $str;
}
}
return implode('; ', $tmp);
}
protected function getInternalFormat(AbstractLogEntry $context): array
@ -129,9 +152,9 @@ class LogEntryExtraFormatter
if ($context instanceof LogWithEventUndoInterface) {
if ($context->isUndoEvent()) {
if ($context->getUndoMode() === 'undo') {
if ('undo' === $context->getUndoMode()) {
$array['log.undo_mode.undo'] = (string) $context->getUndoEventID();
} elseif ($context->getUndoMode() === 'revert') {
} elseif ('revert' === $context->getUndoMode()) {
$array['log.undo_mode.revert'] = (string) $context->getUndoEventID();
}
}
@ -146,7 +169,7 @@ class LogEntryExtraFormatter
}
if ($context instanceof ElementDeletedLogEntry) {
if ($context->getOldName() !== null) {
if (null !== $context->getOldName()) {
$array['log.element_deleted.old_name'] = htmlspecialchars($context->getOldName());
} else {
$array['log.element_deleted.old_name'] = $this->translator->trans('log.element_deleted.old_name.unknown');
@ -183,29 +206,4 @@ class LogEntryExtraFormatter
return $array;
}
/**
* Return a HTML formatted string containing a user viewable form of the Extra data.
*
* @return string
*/
public function format(AbstractLogEntry $context): string
{
$arr = $this->getInternalFormat($context);
$tmp = [];
//Make an array with entries in the form "<b>Key:</b> Value"
foreach ($arr as $key => $value) {
$str = '';
if (is_string($key)) {
$str .= '<b>' . $this->translator->trans($key) . '</b>: ';
}
$str .= $value;
if (!empty($str)) {
$tmp[] = $str;
}
}
return implode("; ", $tmp);
}
}

View file

@ -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);
}
}
}