-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
MyTasks.php
79 lines (63 loc) · 2.31 KB
/
MyTasks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace AppBundle\Action;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use AppBundle\Action\Utils\TokenStorageTrait;
use AppBundle\Entity\Task;
use AppBundle\Entity\TaskList;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class MyTasks
{
use TokenStorageTrait;
public function __construct(
TokenStorageInterface $tokenStorage,
protected EntityManagerInterface $entityManager)
{
$this->tokenStorage = $tokenStorage;
}
public function __invoke(Request $request)
{
$date = new \DateTime($request->get('date'));
$taskList = $this->loadExisting($date);
if (null === $taskList) {
$taskList = new TaskList();
$taskList->setCourier($this->getUser());
$taskList->setDate($date);
try {
$this->entityManager->persist($taskList);
$this->entityManager->flush();
} catch (UniqueConstraintViolationException $e) {
// If 2 requests are received at the very same time,
// we can have a race condition
// @see https://github.com/coopcycle/coopcycle-app/issues/1265
$taskList = $this->loadExisting($date);
}
}
return $taskList;
}
/**
* @param \DateTime $date
* @return TaskList|null
*/
private function loadExisting(\DateTime $date): ?TaskList
{
$taskList = $this->entityManager->getRepository(TaskList::class)
->findOneBy([
'courier' => $this->getUser(),
'date' => $date,
]);
if ($taskList) {
// reset array index to 0 with array_values, otherwise you might get weird stuff in the serializer
$notCancelled = array_values(
array_filter(array_filter($taskList->getTasks(), function (Task $task) {
return !$task->isCancelled();
}))
);
// supports the legacy display of TaskList as tasks for the app courier part
$taskList->setTempLegacyTaskStorage($notCancelled);
return $taskList;
}
return null;
}
}