Completed
Push — master ( 1dbefb...c77374 )
by Jeroen
32s queued 11s
created

src/Kunstmaan/AdminBundle/Helper/CloneHelper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
31
    {
32
        $this->em = $em;
33
        $this->eventDispatcher = $eventDispatcher;
34
    }
35
36
    /**
37
     * @param mixed $entity
38
     *
39
     * @return mixed
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
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