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 |
|
|
|
|
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
|
|
|
|
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.