Skip to content

Commit

Permalink
clean: use TaskListProvider, remove duplicate method in TaskListManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Atala committed Dec 24, 2024
1 parent 6b21b7a commit 9b8eb6f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/Action/TaskList/SetItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Action\TaskList;

use ApiPlatform\Core\Api\IriConverterInterface;
use AppBundle\Doctrine\EventSubscriber\TaskSubscriber\TaskListProvider;
use AppBundle\Entity\TaskList;
use AppBundle\Entity\TaskList\Item;
use AppBundle\Entity\Tour;
Expand All @@ -20,7 +21,8 @@ public function __construct(
private EntityManagerInterface $entityManager,
private UserManager $userManager,
private TaskListManager $taskListManager,
private TaskListNormalizer $taskListNormalizer
private TaskListNormalizer $taskListNormalizer,
private TaskListProvider $taskListProvider
)
{}

Expand All @@ -29,7 +31,7 @@ public function __invoke(Request $request)
$date = new \DateTime($request->get('date'));
$user = $this->userManager->findUserByUsername($request->get('username'));

$taskList = $this->taskListManager->getTaskListForUser($date, $user);
$taskList = $this->taskListProvider->getTaskListForUserAndDate($date, $user);

// Tasks are sent as JSON payload
$data = json_decode($request->getContent(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public function __construct(EntityManagerInterface $objectManager)

public function getTaskList(Task $task, UserInterface $courier)
{
$taskListRepository = $this->objectManager->getRepository(TaskList::class);

// FIXME
// 1. if task->assignedOn is set, we have explictly set the assignment date -> good, we get the proper TaskList
// 2. if not use the doneBefore date as default, but in this case their might be problems with tasks spanning on multiple days
// @see https://github.com/coopcycle/coopcycle-web/issues/874
$date = null !== $task->getAssignedOn() ? $task->getAssignedOn() : $task->getDoneBefore();

return $this->getTaskListForUserAndDate($date, $courier);
}

public function getTaskListForUserAndDate(\DateTime $date, UserInterface $courier)
{
$taskListRepository = $this->objectManager->getRepository(TaskList::class);
$taskListCacheKey = sprintf('%s-%s', $date->format('Y-m-d'), $courier->getUsername());

if (!isset($this->taskListCache[$taskListCacheKey])) {
Expand Down
19 changes: 1 addition & 18 deletions src/Service/TaskListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Service;

use ApiPlatform\Core\Api\IriConverterInterface;
use AppBundle\Doctrine\EventSubscriber\TaskSubscriber\TaskListProvider;
use AppBundle\Entity\Task;
use AppBundle\Entity\TaskList;
use AppBundle\Entity\TaskList\Item;
Expand Down Expand Up @@ -76,22 +77,4 @@ function (Item $item) use ($newItemIri) {
$task->assignTo($taskList->getCourier(), $taskList->getDate());
}
}

public function getTaskListForUser(\DateTime $date, User $user)
{
$taskList = $this->entityManager
->getRepository(TaskList::class)
->findOneBy(['date' => $date, 'courier' => $user]);

if (null === $taskList) {
$taskList = new TaskList();
$taskList->setDate($date);
$taskList->setCourier($user);
$this->entityManager->persist($taskList);
$this->entityManager->flush();
}

return $taskList;
}

}

0 comments on commit 9b8eb6f

Please sign in to comment.