CloneHelper::deepCloneAndSave()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
10
11
/**
12
 * This helper will help you to clone Entities
13
 */
14
class CloneHelper
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    private $em;
20
21
    /**
22
     * @var EventDispatcherInterface
23
     */
24
    private $eventDispatcher;
25
26
    /**
27
     * @param EntityManager            $em              The EntityManager
28
     * @param EventDispatcherInterface $eventDispatcher The EventDispatchInterface
29
     */
30
    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...
31
    {
32
        $this->em = $em;
33
        $this->eventDispatcher = $eventDispatcher;
34
    }
35
36
    /**
37
     * @param mixed $entity
38
     *
39
     * @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...
40
     */
41
    public function deepCloneAndSave($entity)
42
    {
43
        $clonedEntity = clone $entity;
44
        $this->dispatch(new DeepCloneAndSaveEvent($entity, $clonedEntity), Events::DEEP_CLONE_AND_SAVE);
45
46
        $this->em->persist($clonedEntity);
47
        $this->em->flush();
48
49
        $this->dispatch(new DeepCloneAndSaveEvent($entity, $clonedEntity), Events::POST_DEEP_CLONE_AND_SAVE);
50
51
        return $clonedEntity;
52
    }
53
54
    /**
55
     * @param object $event
56
     * @param string $eventName
57
     *
58
     * @return object
59
     */
60 View Code Duplication
    private function dispatch($event, string $eventName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (class_exists(LegacyEventDispatcherProxy::class)) {
63
            $eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher);
64
65
            return $eventDispatcher->dispatch($event, $eventName);
66
        }
67
68
        return $this->eventDispatcher->dispatch($eventName, $event);
69
    }
70
}
71