Passed
Pull Request — master (#7)
by Jim
01:46
created

TaskEventManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 72
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createTaskEvent() 0 13 1
A initiateTaskEvent() 0 9 1
A updateTaskEventWithResult() 0 15 2
1
<?php
2
3
namespace Jarobe\TaskRunnerBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
use Jarobe\TaskRunnerBundle\Entity\TaskEvent;
7
use Jarobe\TaskRunnerBundle\Exception\TaskException;
8
use Jarobe\TaskRunnerBundle\Hydrator\Reflector;
9
use Jarobe\TaskRunnerBundle\Model\TaskResult;
10
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface;
11
12
class TaskEventManager
13
{
14
    /**
15
     * @var EntityManager
16
     */
17
    private $entityManager;
18
19
    /**
20
     * @var Reflector
21
     */
22
    private $reflector;
23
24 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...
25
    {
26 6
        $this->entityManager = $entityManager;
27 6
        $this->reflector = $reflector;
28 6
    }
29
30
    /**
31
     * @param TaskTypeInterface $task
32
     * @return TaskEvent
33
     * @throws TaskException
34
     */
35 1
    public function createTaskEvent(TaskTypeInterface $task)
36
    {
37 1
        $taskEvent = new TaskEvent();
38 1
        $name = $this->reflector->getNameForClass($task);
39 1
        $taskEvent->setTaskName($name)
40 1
            ->setTargetTime($task->getTargetTime())
41 1
            ->setPayload($task->getPayload())
42
        ;
43
44 1
        $this->entityManager->persist($taskEvent);
45 1
        $this->entityManager->flush($taskEvent);
46 1
        return $taskEvent;
47
    }
48
49
    /**
50
     * @param TaskEvent $taskEvent
51
     * @return TaskEvent
52
     */
53 1
    public function initiateTaskEvent(TaskEvent $taskEvent)
54
    {
55 1
        $now = new \DateTime('now');
56 1
        $taskEvent->setInitiatedAt($now);
57
58 1
        $this->entityManager->flush($taskEvent);
59
60 1
        return $taskEvent;
61
    }
62
63
    /**
64
     * @param TaskEvent $taskEvent
65
     * @param TaskResult $result
66
     * @return TaskEvent
67
     */
68 4
    public function updateTaskEventWithResult(TaskEvent $taskEvent, TaskResult $result)
69
    {
70 4
        $now = new \DateTime('now');
71 4
        if ($result->isSuccess()) {
72 2
            $taskEvent->setCompletedAt($now)
73 2
                ->setErrors(null)
74
            ;
75
        } else {
76 2
            $taskEvent->setFailedAt($now)
77 2
                ->setErrors($result->getErrors())
78
            ;
79
        }
80 4
        $this->entityManager->flush($taskEvent);
81 4
        return $taskEvent;
82
    }
83
}
84