TaskEventManager::createTaskEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
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)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might 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:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
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