2020-05-16 20:53:35 +02:00
|
|
|
<?php
|
2023-06-11 18:59:07 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-16 20:53:35 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-05-16 20:53:35 +02: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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Entity\Base\AbstractPartsContainingDBElement;
|
|
|
|
use App\Entity\Base\PartsContainingRepositoryInterface;
|
|
|
|
use App\Entity\Parts\Part;
|
2022-08-14 19:32:53 +02:00
|
|
|
use InvalidArgumentException;
|
2020-05-16 20:53:35 +02:00
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
/**
|
|
|
|
* @template TEntityClass of AbstractPartsContainingDBElement
|
|
|
|
* @extends StructuralDBElementRepository<TEntityClass>
|
|
|
|
*/
|
2020-08-21 21:36:22 +02:00
|
|
|
abstract class AbstractPartsContainingRepository extends StructuralDBElementRepository implements PartsContainingRepositoryInterface
|
2020-05-16 20:53:35 +02:00
|
|
|
{
|
2023-04-17 23:59:44 +02:00
|
|
|
/** @var int The maximum number of levels for which we can recurse before throwing an error */
|
2023-03-02 22:55:22 +01:00
|
|
|
private const RECURSION_LIMIT = 50;
|
|
|
|
|
2020-05-16 20:53:35 +02:00
|
|
|
/**
|
|
|
|
* Returns all parts associated with this element.
|
2020-08-21 21:36:22 +02:00
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param object $element the element for which the parts should be determined
|
|
|
|
* @param array $order_by The order of the parts. Format ['name' => 'ASC']
|
2020-08-21 21:36:22 +02:00
|
|
|
*
|
2022-09-11 19:14:16 +02:00
|
|
|
* @return Part[]
|
2020-05-16 20:53:35 +02:00
|
|
|
*/
|
|
|
|
abstract public function getParts(object $element, array $order_by = ['name' => 'ASC']): array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the count of the parts associated with this element.
|
2020-08-21 21:36:22 +02:00
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param object $element the element for which the parts should be determined
|
|
|
|
* @return int
|
2020-05-16 20:53:35 +02:00
|
|
|
*/
|
|
|
|
abstract public function getPartsCount(object $element): int;
|
|
|
|
|
2022-09-21 13:20:57 +02:00
|
|
|
/**
|
|
|
|
* Returns the count of the parts associated with this element and all its children.
|
|
|
|
* Please be aware that this function is pretty slow on large trees!
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPartsCountRecursive(AbstractPartsContainingDBElement $element): int
|
|
|
|
{
|
2023-04-17 23:59:44 +02:00
|
|
|
return $this->getPartsCountRecursiveWithDepthN($element, self::RECURSION_LIMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The implementation of the recursive function to get the parts count.
|
|
|
|
* This function is used to limit the recursion depth (remaining_depth is decreased on each call).
|
|
|
|
* If the recursion limit is reached (remaining_depth <= 0), a RuntimeException is thrown.
|
|
|
|
* @internal This function is not intended to be called directly, use getPartsCountRecursive() instead.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getPartsCountRecursiveWithDepthN(AbstractPartsContainingDBElement $element, int $remaining_depth): int
|
|
|
|
{
|
|
|
|
if ($remaining_depth <= 0) {
|
|
|
|
throw new \RuntimeException('Recursion limit reached!');
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:20:57 +02:00
|
|
|
$count = $this->getPartsCount($element);
|
|
|
|
|
2023-03-02 22:55:22 +01:00
|
|
|
//If the element is its own parent, we have a loop in the tree, so we stop here.
|
|
|
|
if ($element->getParent() === $element) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:20:57 +02:00
|
|
|
foreach ($element->getChildren() as $child) {
|
2023-04-17 23:59:44 +02:00
|
|
|
$count += $this->getPartsCountRecursiveWithDepthN($child, $remaining_depth - 1);
|
2022-09-21 13:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:53:35 +02:00
|
|
|
protected function getPartsByField(object $element, array $order_by, string $field_name): array
|
|
|
|
{
|
|
|
|
if (!$element instanceof AbstractPartsContainingDBElement) {
|
2022-08-14 19:32:53 +02:00
|
|
|
throw new InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!');
|
2020-05-16 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$repo = $this->getEntityManager()->getRepository(Part::class);
|
|
|
|
|
|
|
|
return $repo->findBy([$field_name => $element], $order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getPartsCountByField(object $element, string $field_name): int
|
|
|
|
{
|
|
|
|
if (!$element instanceof AbstractPartsContainingDBElement) {
|
2022-08-14 19:32:53 +02:00
|
|
|
throw new InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!');
|
2020-05-16 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$repo = $this->getEntityManager()->getRepository(Part::class);
|
|
|
|
|
|
|
|
return $repo->count([$field_name => $element]);
|
|
|
|
}
|
2020-08-21 21:36:22 +02:00
|
|
|
}
|