Code Duplication    Length = 76-76 lines in 2 locations

src/OroCRM/Bundle/ContactBundle/Handler/ContactEmailApiHandler.php 1 location

@@ 16-91 (lines=76) @@
13
 * Class ContactEmailApiHandler
14
 * @package OroCRM\Bundle\ContactBundle\Handler
15
 */
16
class ContactEmailApiHandler extends AbstractEntityApiHandler
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)
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

src/OroCRM/Bundle/ContactBundle/Handler/ContactPhoneApiHandler.php 1 location

@@ 16-91 (lines=76) @@
13
 * Class ContactPhoneApiHandler
14
 * @package OroCRM\Bundle\ContactBundle\Handler
15
 */
16
class ContactPhoneApiHandler extends AbstractEntityApiHandler
17
{
18
    const ENTITY_CLASS = 'OroCRM\Bundle\ContactBundle\Entity\ContactPhone';
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)
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