Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

CloneHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 40
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A deepCloneAndSave() 0 12 1
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
7
use Kunstmaan\AdminBundle\Event\Events;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
/**
11
 * This helper will help you to clone Entities
12
 */
13
class CloneHelper
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $eventDispatcher;
24
25
    /**
26
     * @param EntityManager            $em              The EntityManager
27
     * @param EventDispatcherInterface $eventDispatcher The EventDispatchInterface
28
     */
29
    public function __construct(EntityManager $em, EventDispatcherInterface $eventDispatcher)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. 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...
30
    {
31
        $this->em = $em;
32
        $this->eventDispatcher = $eventDispatcher;
33
    }
34
35
    /**
36
     * @param mixed $entity
37
     *
38
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
39
     */
40
    public function deepCloneAndSave($entity)
41
    {
42
        $clonedEntity = clone $entity;
43
        $this->eventDispatcher->dispatch(Events::DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity));
44
45
        $this->em->persist($clonedEntity);
46
        $this->em->flush();
47
48
        $this->eventDispatcher->dispatch(Events::POST_DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity));
49
50
        return $clonedEntity;
51
    }
52
}
53