Completed
Pull Request — master (#1)
by Jim
08:57
created

TaskEventManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createTaskEvent() 0 14 1
A initiateTaskEvent() 0 9 1
A updateTaskEventWithResult() 0 15 2
1
<?php
2
3
namespace Jarobe\TaskRunner\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
use Jarobe\TaskRunner\Entity\TaskEvent;
7
use Jarobe\TaskRunner\Hydrator\Reflector;
8
use Jarobe\TaskRunner\Model\TaskResult;
9
use Jarobe\TaskRunner\TaskType\TaskTypeInterface;
10
11
class TaskEventManager
12
{
13
    /**
14
     * @var EntityManager
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var Reflector
20
     */
21
    private $reflector;
22
23
    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...
24
    {
25
        $this->entityManager = $entityManager;
26
        $this->reflector = $reflector;
27
    }
28
29
    /**
30
     * @param TaskTypeInterface $task
31
     * @return TaskEvent
32
     */
33
    public function createTaskEvent(TaskTypeInterface $task)
34
    {
35
        $taskName = $this->reflector->getNameForClass($task);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $taskName is correct as $this->reflector->getNameForClass($task) (which targets Jarobe\TaskRunner\Hydrat...ctor::getNameForClass()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
37
        $taskEvent = new TaskEvent();
38
        $taskEvent->setTaskName($taskName)
39
            ->setTargetTime($task->getTargetTime())
0 ignored issues
show
Bug introduced by
It seems like $task->getTargetTime() can be null; however, setTargetTime() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
            ->setPayload($task->getPayload())
41
        ;
42
43
        $this->entityManager->persist($taskEvent);
44
        $this->entityManager->flush($taskEvent);
45
        return $taskEvent;
46
    }
47
48
    /**
49
     * @param TaskEvent $taskEvent
50
     * @return TaskEvent
51
     */
52
    public function initiateTaskEvent(TaskEvent $taskEvent)
53
    {
54
        $now = new \DateTime('now');
55
        $taskEvent->setInitiatedAt($now);
56
57
        $this->entityManager->flush($taskEvent);
58
59
        return $taskEvent;
60
    }
61
62
    /**
63
     * @param TaskEvent $taskEvent
64
     * @param TaskResult $result
65
     * @return TaskEvent
66
     */
67
    public function updateTaskEventWithResult(TaskEvent $taskEvent, TaskResult $result)
68
    {
69
        $now = new \DateTime('now');
70
        if ($result->isSuccess()) {
71
            $taskEvent->setCompletedAt($now)
72
                ->setErrors(null)
73
            ;
74
        } else {
75
            $taskEvent->setFailedAt($now)
76
                ->setErrors($result->getErrors())
77
            ;
78
        }
79
        $this->entityManager->flush($taskEvent);
80
        return $taskEvent;
81
    }
82
}
83