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,13 +23,11 @@
namespace App\Repository;
use Doctrine\ORM\EntityRepository;
class AttachmentRepository extends DBElementRepository
{
/**
* Gets the count of all private/secure attachments.
*
* @return int
*/
public function getPrivateAttachmentsCount(): int
@ -36,12 +37,15 @@ class AttachmentRepository extends DBElementRepository
->where('attachment.path LIKE :like');
$qb->setParameter('like', '\\%SECURE\\%%');
$query = $qb->getQuery();
return (int) $query->getSingleScalarResult();
}
/**
* Gets the count of all external attachments (attachments only containing an URL)
* Gets the count of all external attachments (attachments only containing an URL).
*
* @return int
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
@ -54,12 +58,15 @@ class AttachmentRepository extends DBElementRepository
$qb->setParameter('http', 'http://%');
$qb->setParameter('https', 'https://%');
$query = $qb->getQuery();
return (int) $query->getSingleScalarResult();
}
/**
* Gets the count of all attachments where an user uploaded an file.
*
* @return int
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
@ -74,6 +81,7 @@ class AttachmentRepository extends DBElementRepository
$qb->setParameter('base', '\\%BASE\\%%');
$qb->setParameter('media', '\\%MEDIA\\%%');
$query = $qb->getQuery();
return (int) $query->getSingleScalarResult();
}
}
}

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\Repository;
use App\Entity\Base\AbstractDBElement;
use Doctrine\ORM\EntityRepository;
@ -29,8 +31,9 @@ class DBElementRepository extends EntityRepository
/**
* Changes the ID of the given element to a new value.
* You should only use it to undelete former existing elements, everything else is most likely a bad idea!
* @param AbstractDBElement $element The element whose ID should be changed
* @param int $new_id The new ID
*
* @param AbstractDBElement $element The element whose ID should be changed
* @param int $new_id The new ID
*/
public function changeID(AbstractDBElement $element, int $new_id): void
{
@ -46,11 +49,11 @@ class DBElementRepository extends EntityRepository
$this->setField($element, 'id', $new_id);
}
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);
}
}
}

View file

@ -49,8 +49,6 @@ use App\Entity\LogSystem\ElementCreatedLogEntry;
use App\Entity\LogSystem\ElementDeletedLogEntry;
use App\Entity\LogSystem\ElementEditedLogEntry;
use App\Entity\UserSystem\User;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
class LogEntryRepository extends DBElementRepository
{
@ -72,9 +70,9 @@ class LogEntryRepository extends DBElementRepository
* Find log entries associated with the given element (the history of the element).
*
* @param AbstractDBElement $element The element for which the history should be generated
* @param string $order By default newest entries are shown first. Change this to ASC to show oldest entries first.
* @param null $limit
* @param null $offset
* @param string $order By default newest entries are shown first. Change this to ASC to show oldest entries first.
* @param null $limit
* @param null $offset
*
* @return AbstractLogEntry[]
*/
@ -84,9 +82,11 @@ class LogEntryRepository extends DBElementRepository
}
/**
* Try to get a log entry that contains the information to undete a given element
* @param string $class The class of the element that should be undeleted
* @param int $id The ID of the element that should be deleted
* Try to get a log entry that contains the information to undete a given element.
*
* @param string $class The class of the element that should be undeleted
* @param int $id The ID of the element that should be deleted
*
* @return ElementDeletedLogEntry
*/
public function getUndeleteDataForElement(string $class, int $id): ElementDeletedLogEntry
@ -94,31 +94,34 @@ class LogEntryRepository extends DBElementRepository
$qb = $this->createQueryBuilder('log');
$qb->select('log')
//->where('log INSTANCE OF App\Entity\LogSystem\ElementEditedLogEntry')
->where('log INSTANCE OF ' . ElementDeletedLogEntry::class)
->where('log INSTANCE OF '.ElementDeletedLogEntry::class)
->andWhere('log.target_type = :target_type')
->andWhere('log.target_id = :target_id')
->orderBy('log.timestamp', 'DESC')
->setMaxResults(1);
$qb->setParameters([
'target_type' => AbstractLogEntry::targetTypeClassToID($class),
'target_id' => $id,
]);
'target_type' => AbstractLogEntry::targetTypeClassToID($class),
'target_id' => $id,
]);
$query = $qb->getQuery();
$query = $qb->getQuery();
$results = $query->execute();
if (empty($results)) {
throw new \RuntimeException("No undelete data could be found for this element");
throw new \RuntimeException('No undelete data could be found for this element');
}
return $results[0];
}
/**
* Gets all log entries that are related to time travelling
* @param AbstractDBElement $element The element for which the time travel data should be retrieved
* @param \DateTime $until Back to which timestamp should the data be get (including the timestamp)
* Gets all log entries that are related to time travelling.
*
* @param AbstractDBElement $element The element for which the time travel data should be retrieved
* @param \DateTime $until Back to which timestamp should the data be get (including the timestamp)
*
* @return AbstractLogEntry[]
*/
public function getTimetravelDataForElement(AbstractDBElement $element, \DateTime $until): array
@ -126,48 +129,49 @@ class LogEntryRepository extends DBElementRepository
$qb = $this->createQueryBuilder('log');
$qb->select('log')
//->where('log INSTANCE OF App\Entity\LogSystem\ElementEditedLogEntry')
->where('log INSTANCE OF ' . ElementEditedLogEntry::class)
->orWhere('log INSTANCE OF ' . CollectionElementDeleted::class)
->where('log INSTANCE OF '.ElementEditedLogEntry::class)
->orWhere('log INSTANCE OF '.CollectionElementDeleted::class)
->andWhere('log.target_type = :target_type')
->andWhere('log.target_id = :target_id')
->andWhere('log.timestamp >= :until')
->orderBy('log.timestamp', 'DESC');
$qb->setParameters([
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID(),
'until' => $until
]);
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID(),
'until' => $until,
]);
$query = $qb->getQuery();
return $query->execute();
}
/**
* Check if the given element has existed at the given timestamp
* @param AbstractDBElement $element
* @param \DateTime $timestamp
* Check if the given element has existed at the given timestamp.
*
* @return bool True if the element existed at the given timestamp
*/
public function getElementExistedAtTimestamp(AbstractDBElement $element, \DateTime $timestamp): bool
{
$qb = $this->createQueryBuilder('log');
$qb->select('count(log)')
->where('log INSTANCE OF ' . ElementCreatedLogEntry::class)
->where('log INSTANCE OF '.ElementCreatedLogEntry::class)
->andWhere('log.target_type = :target_type')
->andWhere('log.target_id = :target_id')
->andWhere('log.timestamp >= :until')
->orderBy('log.timestamp', 'DESC');
$qb->setParameters([
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID(),
'until' => $timestamp
]);
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID(),
'until' => $timestamp,
]);
$query = $qb->getQuery();
$count = $query->getSingleScalarResult();
return !($count > 0);
return ! ($count > 0);
}
/**
@ -187,9 +191,9 @@ class LogEntryRepository extends DBElementRepository
/**
* Gets the target element associated with the logentry.
*
* @param AbstractLogEntry $logEntry
* @return AbstractDBElement|null Returns the associated DBElement or null if the log either has no target or the element
* was deleted from DB.
* was deleted from DB.
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
@ -206,34 +210,9 @@ class LogEntryRepository extends DBElementRepository
return $this->getEntityManager()->find($class, $id);
}
protected function getLastUser(AbstractDBElement $element, string $class)
{
$qb = $this->createQueryBuilder('log');
$qb->select('log')
//->where('log INSTANCE OF App\Entity\LogSystem\ElementEditedLogEntry')
->where('log INSTANCE OF ' . $class)
->andWhere('log.target_type = :target_type')
->andWhere('log.target_id = :target_id')
->orderBy('log.timestamp', 'DESC');
$qb->setParameters([
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID()
]);
$query = $qb->getQuery();
$query->setMaxResults(1);
/** @var AbstractLogEntry[] $results */
$results = $query->execute();
if (isset($results[0])) {
return $results[0]->getUser();
}
return null;
}
/**
* Returns the last user that has edited the given element.
* @param AbstractDBElement $element
*
* @return User|null A user object, or null if no user could be determined.
*/
public function getLastEditingUser(AbstractDBElement $element): ?User
@ -243,11 +222,37 @@ class LogEntryRepository extends DBElementRepository
/**
* Returns the user that has created the given element.
* @param AbstractDBElement $element
*
* @return User|null A user object, or null if no user could be determined.
*/
public function getCreatingUser(AbstractDBElement $element): ?User
{
return $this->getLastUser($element, ElementCreatedLogEntry::class);
}
protected function getLastUser(AbstractDBElement $element, string $class)
{
$qb = $this->createQueryBuilder('log');
$qb->select('log')
//->where('log INSTANCE OF App\Entity\LogSystem\ElementEditedLogEntry')
->where('log INSTANCE OF '.$class)
->andWhere('log.target_type = :target_type')
->andWhere('log.target_id = :target_id')
->orderBy('log.timestamp', 'DESC');
$qb->setParameters([
'target_type' => AbstractLogEntry::targetTypeClassToID(get_class($element)),
'target_id' => $element->getID(),
]);
$query = $qb->getQuery();
$query->setMaxResults(1);
/** @var AbstractLogEntry[] $results */
$results = $query->execute();
if (isset($results[0])) {
return $results[0]->getUser();
}
return null;
}
}

View file

@ -44,7 +44,6 @@ namespace App\Repository;
use App\Entity\Base\AbstractNamedDBElement;
use App\Helpers\Trees\TreeViewNode;
use Doctrine\ORM\EntityRepository;
class NamedDBElementRepository extends DBElementRepository
{

View file

@ -43,14 +43,15 @@ declare(strict_types=1);
namespace App\Repository;
use App\Entity\Parts\PartLot;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
class PartRepository extends NamedDBElementRepository
{
/**
* Gets the summed up instock of all parts (only parts without an measurent unit)
* Gets the summed up instock of all parts (only parts without an measurent unit).
*
* @return string
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
@ -63,12 +64,15 @@ class PartRepository extends NamedDBElementRepository
->where('part.partUnit IS NULL');
$query = $qb->getQuery();
return $query->getSingleScalarResult();
}
/**
* Gets the number of parts that has price informations.
*
* @return int
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
@ -81,6 +85,7 @@ class PartRepository extends NamedDBElementRepository
->where('pricedetail.price > 0.0');
$query = $qb->getQuery();
return (int) $query->getSingleScalarResult();
}
}