|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jarobe\TaskRunnerBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Jarobe\TaskRunnerBundle\Entity\TaskEvent; |
|
7
|
|
|
use Jarobe\TaskRunnerBundle\Entity\TaskEventInterface; |
|
8
|
|
|
use Jarobe\TaskRunnerBundle\Exception\TaskException; |
|
9
|
|
|
use Jarobe\TaskRunnerBundle\Hydrator\Reflector; |
|
10
|
|
|
use Jarobe\TaskRunnerBundle\Model\TaskResult; |
|
11
|
|
|
use Jarobe\TaskRunnerBundle\Provider\TaskEventProviderInterface; |
|
12
|
|
|
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface; |
|
13
|
|
|
|
|
14
|
|
|
class TaskEventManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EntityManager |
|
18
|
|
|
*/ |
|
19
|
|
|
private $entityManager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Reflector |
|
23
|
|
|
*/ |
|
24
|
|
|
private $reflector; |
|
25
|
|
|
|
|
26
|
6 |
|
public function __construct(EntityManager $entityManager, Reflector $reflector) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
6 |
|
$this->entityManager = $entityManager; |
|
29
|
6 |
|
$this->reflector = $reflector; |
|
30
|
6 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param TaskEventInterface $taskEventInterface |
|
34
|
|
|
* @param TaskTypeInterface $task |
|
35
|
|
|
* @return TaskEventInterface |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function createTaskEvent(TaskEventInterface $taskEventInterface, TaskTypeInterface $task) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$name = $this->reflector->getNameForClass($task); |
|
40
|
|
|
|
|
41
|
1 |
|
$taskEventInterface->intialize($name, $task); |
|
42
|
|
|
|
|
43
|
1 |
|
$this->entityManager->persist($taskEventInterface); |
|
44
|
1 |
|
$this->entityManager->flush($taskEventInterface); |
|
45
|
1 |
|
return $taskEventInterface; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param TaskEventInterface $taskEvent |
|
50
|
|
|
* @return TaskEventInterface |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function initiateTaskEvent(TaskEventInterface $taskEvent) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$taskEvent->initiate(); |
|
55
|
|
|
|
|
56
|
1 |
|
$this->entityManager->flush($taskEvent); |
|
57
|
|
|
|
|
58
|
1 |
|
return $taskEvent; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param TaskEventInterface $taskEvent |
|
63
|
|
|
* @param TaskResult $result |
|
64
|
|
|
* @return TaskEventInterface |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function updateTaskEventWithResult(TaskEventInterface $taskEvent, TaskResult $result) |
|
67
|
|
|
{ |
|
68
|
4 |
|
if ($result->isSuccess()) { |
|
69
|
2 |
|
$taskEvent->setAsCompleted(); |
|
70
|
|
|
} else { |
|
71
|
2 |
|
$taskEvent->setAsFailed($result->getErrors()); |
|
72
|
|
|
} |
|
73
|
4 |
|
$this->entityManager->flush($taskEvent); |
|
74
|
4 |
|
return $taskEvent; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.