Test Failed
Pull Request — master (#7)
by Jim
03:47
created

TaskEventManager::createTaskEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 12
nc 2
nop 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\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;
0 ignored issues
show
Unused Code introduced by
The property $reflector is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24 6
    public function __construct(EntityManager $entityManager)
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
    }
28
29
    /**
30
     * @param TaskTypeInterface $task
31
     * @return TaskEvent
32
     * @throws TaskException
33
     */
34
    public function createTaskEvent(TaskTypeInterface $task)
35
    {
36
        $taskEvent = new TaskEvent();
37
        $taskEvent->setTaskName($task::getName())
38
            ->setTargetTime($task->getTargetTime())
39
            ->setPayload($task->getPayload())
40
        ;
41
42
        $this->entityManager->persist($taskEvent);
43
        $this->entityManager->flush($taskEvent);
44
        return $taskEvent;
45
    }
46
47
    /**
48
     * @param TaskEvent $taskEvent
49
     * @return TaskEvent
50
     */
51 1
    public function initiateTaskEvent(TaskEvent $taskEvent)
52
    {
53 1
        $now = new \DateTime('now');
54 1
        $taskEvent->setInitiatedAt($now);
55
56 1
        $this->entityManager->flush($taskEvent);
57
58 1
        return $taskEvent;
59
    }
60
61
    /**
62
     * @param TaskEvent $taskEvent
63
     * @param TaskResult $result
64
     * @return TaskEvent
65
     */
66 4
    public function updateTaskEventWithResult(TaskEvent $taskEvent, TaskResult $result)
67
    {
68 4
        $now = new \DateTime('now');
69 4
        if ($result->isSuccess()) {
70 2
            $taskEvent->setCompletedAt($now)
71 2
                ->setErrors(null)
72
            ;
73
        } else {
74 2
            $taskEvent->setFailedAt($now)
75 2
                ->setErrors($result->getErrors())
76
            ;
77
        }
78 4
        $this->entityManager->flush($taskEvent);
79 4
        return $taskEvent;
80
    }
81
}
82