Skip to content

Commit

Permalink
Add element movement logic
Browse files Browse the repository at this point in the history
  • Loading branch information
deeravenger committed Oct 17, 2024
1 parent 69771bb commit 146ac08
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\EconumoOneBundle\Domain\Exception;

use App\EconumoOneBundle\Domain\Exception\DomainException;

class InvalidBudgetElementException extends DomainException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function getExcludedAccountIds(Budget $budget, Id $userId): array
return array_map(fn(Account $account) => $account->getId(), $budget->getExcludedAccounts($userId)->toArray());
}

private function getBudgetUserIds(Budget $budget): array
public function getBudgetUserIds(Budget $budget): array
{
$userIds = [$budget->getUser()->getId()];
foreach ($budget->getAccessList() as $entry) {
Expand Down Expand Up @@ -117,7 +117,7 @@ private function getCurrenciesIds(array $userAccounts): array
* @param array $userIds
* @return Category[]
*/
private function getCategories(array $userIds): array
public function getCategories(array $userIds): array
{
$result = [];
foreach ($this->categoryRepository->findByOwnersIds($userIds) as $category) {
Expand All @@ -130,7 +130,7 @@ private function getCategories(array $userIds): array
* @param array $userIds
* @return Tag[]
*/
private function getTags(array $userIds): array
public function getTags(array $userIds): array
{
$result = [];
foreach ($this->tagRepository->findByOwnersIds($userIds) as $tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
namespace App\EconumoOneBundle\Domain\Service\Budget;


use App\EconumoOneBundle\Domain\Entity\Budget;
use App\EconumoOneBundle\Domain\Entity\BudgetEntityOption;
use App\EconumoOneBundle\Domain\Entity\ValueObject\BudgetEntityType;
use App\EconumoOneBundle\Domain\Entity\ValueObject\Id;
use App\EconumoOneBundle\Domain\Exception\DomainException;
use App\EconumoOneBundle\Domain\Exception\InvalidBudgetElementException;
use App\EconumoOneBundle\Domain\Factory\BudgetEntityOptionFactoryInterface;
use App\EconumoOneBundle\Domain\Repository\BudgetEntityOptionRepositoryInterface;
use App\EconumoOneBundle\Domain\Repository\BudgetEnvelopeRepositoryInterface;
use App\EconumoOneBundle\Domain\Repository\BudgetFolderRepositoryInterface;
use App\EconumoOneBundle\Domain\Service\Budget\Assembler\BudgetFiltersDtoAssembler;
use App\EconumoOneBundle\Domain\Service\Budget\Dto\BudgetStructureMoveElementDto;

readonly class BudgetElementsService
Expand All @@ -18,6 +25,8 @@ public function __construct(
private BudgetEntityOptionRepositoryInterface $entityOptionRepository,
private BudgetEntityOptionFactoryInterface $budgetEntityOptionFactory,
private BudgetFolderRepositoryInterface $folderRepository,
private BudgetFiltersDtoAssembler $budgetFiltersDtoAssembler,
private BudgetEnvelopeRepositoryInterface $envelopeRepository,
) {
}

Expand All @@ -26,10 +35,10 @@ public function __construct(
* @param BudgetStructureMoveElementDto[] $elements
* @return void
*/
public function moveElements(Id $budgetId, array $elements): void
public function moveElements(Budget $budget, array $elements): void
{
$seen = [];
$options = $this->entityOptionRepository->getByBudgetId($budgetId);
$options = $this->entityOptionRepository->getByBudgetId($budget->getId());
$updatedOptions = [];
foreach ($options as $option) {
if (!array_key_exists($option->getEntityId()->getValue(), $elements)) {
Expand Down Expand Up @@ -64,18 +73,71 @@ public function moveElements(Id $budgetId, array $elements): void
}
}

/** @var BudgetEntityOption[] $newOptions */
$newOptions = [];
$needTags = false;
$needCategories = false;
$needEnvelopes = false;
foreach ($elements as $element) {
if (array_key_exists($element->id->getValue(), $elements)) {
continue;
}
$updatedOptions[] = $this->budgetEntityOptionFactory->create(
$budgetId,
$newOptions[$element->type->getAlias()][$element->id->getValue()] = $this->budgetEntityOptionFactory->create(
$budget->getId(),
$element->id,
$element->type,
$element->position,
null,
$element->folderId
);
if ($element->type->isCategory()) {
$needCategories = true;
} elseif ($element->type->isTag()) {
$needTags = true;
} elseif ($element->type->isEnvelope()) {
$needEnvelopes = true;
}
}

if ($newOptions !== []) {
if ($needCategories || $needTags) {
$budgetUserIds = $this->budgetFiltersDtoAssembler->getBudgetUserIds($budget);
if (array_key_exists(BudgetEntityType::category()->getAlias(), $newOptions)) {
$budgetCategories = $this->budgetFiltersDtoAssembler->getCategories($budgetUserIds);
foreach ($newOptions[BudgetEntityType::category()->getAlias()] as $option) {
if (!array_key_exists($option->getEntityId()->getValue(), $budgetCategories)) {
throw new InvalidBudgetElementException();
}
$updatedOptions[] = $option;
}
}
if (array_key_exists(BudgetEntityType::tag()->getAlias(), $newOptions)) {
$budgetTags = $this->budgetFiltersDtoAssembler->getTags($budgetUserIds);
foreach ($newOptions[BudgetEntityType::tag()->getAlias()] as $option) {
if (!array_key_exists($option->getEntityId()->getValue(), $budgetTags)) {
throw new InvalidBudgetElementException();
}
$updatedOptions[] = $option;
}
}
}

if ($needEnvelopes) {
$envelopes = $this->envelopeRepository->getByBudgetId($budget->getId());
foreach ($newOptions[BudgetEntityType::envelope()->getAlias()] as $option) {
$found = false;
foreach ($envelopes as $envelope) {
if ($envelope->getId()->isEqual($option->getEntityId())) {
$found = true;
break;
}
}
if (!$found) {
throw new InvalidBudgetElementException();
}
$updatedOptions[] = $option;
}
}
}

if ($updatedOptions !== []) {
Expand Down
3 changes: 2 additions & 1 deletion src/EconumoOneBundle/Domain/Service/Budget/BudgetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function getBudget(Id $userId, Id $budgetId, DateTimeInterface $periodSta

public function moveElements(Id $userId, Id $budgetId, array $affectedElements): void
{
$this->budgetElementsService->moveElements($budgetId, $affectedElements);
$budget = $this->budgetRepository->get($budgetId);
$this->budgetElementsService->moveElements($budget, $affectedElements);
}
}

0 comments on commit 146ac08

Please sign in to comment.