Completed
Push — master ( 341743...7fbf31 )
by
unknown
09:35
created

ContactEmailApiHandler::beforeProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Handler;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
7
8
use Oro\Bundle\EntityBundle\Form\EntityField\Handler\Processor\AbstractEntityApiHandler;
9
use Oro\Bundle\EntityBundle\ORM\OroEntityManager;
10
use Oro\Bundle\SecurityBundle\SecurityFacade;
11
12
/**
13
 * Class ContactEmailApiHandler
14
 * @package OroCRM\Bundle\ContactBundle\Handler
15
 */
16 View Code Duplication
class ContactEmailApiHandler extends AbstractEntityApiHandler
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
    const ENTITY_CLASS = 'OroCRM\Bundle\ContactBundle\Entity\ContactEmail';
19
20
    /**
21
     * @var OroEntityManager
22
     */
23
    protected $entityManager;
24
25
    /**
26
     * @var SecurityFacade
27
     */
28
    protected $securityFacade;
29
30
    /**
31
     * @param OroEntityManager $entityManager
32
     * @param SecurityFacade $securityFacade
33
     */
34
    public function __construct(OroEntityManager $entityManager, SecurityFacade $securityFacade)
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...
35
    {
36
        $this->entityManager = $entityManager;
37
        $this->securityFacade = $securityFacade;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function beforeProcess($entity)
44
    {
45
        //check owner (Contact) entity with 'edit' permission
46
        if (!$this->securityFacade->isGranted('EDIT', $entity->getOwner())) {
47
            throw new AccessDeniedException();
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function afterProcess($entity)
55
    {
56
        $owner = $entity->getOwner();
57
        $owner->setUpdatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
58
        $changeSet = $this->getChangeSet($owner);
59
        $this->entityManager->persist($owner);
60
        $this->entityManager->flush();
61
62
        return $changeSet;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getClass()
69
    {
70
        return self::ENTITY_CLASS;
71
    }
72
73
    /**
74
     * @param $entity
75
     *
76
     * @return array
77
     */
78
    protected function getChangeSet($entity)
79
    {
80
        $accessor = PropertyAccess::createPropertyAccessor();
81
        $response = [
82
            'fields' => []
83
        ];
84
85
        if ($accessor->isReadable($entity, 'updatedAt')) {
86
            $response['fields']['updatedAt'] = $accessor->getValue($entity, 'updatedAt');
87
        }
88
89
        return $response;
90
    }
91
}
92