1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\ActivityContactBundle\Api\Processor\Config; |
4
|
|
|
|
5
|
|
|
use Oro\Component\ChainProcessor\ContextInterface; |
6
|
|
|
use Oro\Component\ChainProcessor\ProcessorInterface; |
7
|
|
|
use Oro\Bundle\ApiBundle\Config\EntityDefinitionConfig; |
8
|
|
|
use Oro\Bundle\ApiBundle\Processor\Config\ConfigContext; |
9
|
|
|
use Oro\Bundle\ApiBundle\Util\DoctrineHelper; |
10
|
|
|
use Oro\Bundle\EntityConfigBundle\Config\ConfigManager; |
11
|
|
|
use OroCRM\Bundle\ActivityContactBundle\EntityConfig\ActivityScope; |
12
|
|
|
use OroCRM\Bundle\ActivityContactBundle\Model\TargetExcludeList; |
13
|
|
|
use OroCRM\Bundle\ActivityContactBundle\Provider\ActivityContactProvider; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Renames "contacting activity" (ac_*) fields to have more readable names. |
17
|
|
|
* Exclude these fields for "update" action because they are calculated automatically |
18
|
|
|
* and should not be updated manually. |
19
|
|
|
*/ |
20
|
|
|
class UpdateActivityContactFields implements ProcessorInterface |
21
|
|
|
{ |
22
|
|
|
/** @var DoctrineHelper */ |
23
|
|
|
protected $doctrineHelper; |
24
|
|
|
|
25
|
|
|
/** @var ConfigManager */ |
26
|
|
|
protected $configManager; |
27
|
|
|
|
28
|
|
|
/** @var ActivityContactProvider */ |
29
|
|
|
protected $activityContactProvider; |
30
|
|
|
|
31
|
|
|
/** @var string[] */ |
32
|
|
|
protected $excludedActions; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param DoctrineHelper $doctrineHelper |
36
|
|
|
* @param ConfigManager $configManager |
37
|
|
|
* @param ActivityContactProvider $activityContactProvider |
38
|
|
|
* @param string $excludedActions |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
DoctrineHelper $doctrineHelper, |
42
|
|
|
ConfigManager $configManager, |
43
|
|
|
ActivityContactProvider $activityContactProvider, |
44
|
|
|
$excludedActions |
45
|
|
|
) { |
46
|
|
|
$this->doctrineHelper = $doctrineHelper; |
47
|
|
|
$this->configManager = $configManager; |
48
|
|
|
$this->activityContactProvider = $activityContactProvider; |
49
|
|
|
$this->excludedActions = $excludedActions; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function process(ContextInterface $context) |
56
|
|
|
{ |
57
|
|
|
/** @var ConfigContext $context */ |
58
|
|
|
|
59
|
|
|
$definition = $context->getResult(); |
60
|
|
|
if (!$definition->isExcludeAll()) { |
61
|
|
|
// expected completed config |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$entityClass = $context->getClassName(); |
66
|
|
|
if (!$this->doctrineHelper->isManageableEntityClass($entityClass)) { |
67
|
|
|
// only manageable entities are supported |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
if (!$this->isSupportedEntity($entityClass)) { |
71
|
|
|
// an entity is not supported |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->updateFields($definition, $context->getTargetAction()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $entityClass |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
protected function isSupportedEntity($entityClass) |
84
|
|
|
{ |
85
|
|
|
if (!$this->configManager->hasConfig($entityClass)) { |
86
|
|
|
// only extended entities are supported |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
if (!$this->configManager->getEntityConfig('extend', $entityClass)->is('is_extend')) { |
90
|
|
|
// only extended entities are supported |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
if (TargetExcludeList::isExcluded($entityClass)) { |
94
|
|
|
// skip excluded entities |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
$activities = $this->configManager->getEntityConfig('activity', $entityClass)->get('activities'); |
98
|
|
|
if (empty($activities)) { |
99
|
|
|
// entity should be associated with at least one activity |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
$contactingActivities = $this->activityContactProvider->getSupportedActivityClasses(); |
103
|
|
|
if (!array_intersect($contactingActivities, $activities)) { |
104
|
|
|
// an entity does not have supported activity |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param EntityDefinitionConfig $definition |
113
|
|
|
* @param string|null $targetAction |
114
|
|
|
*/ |
115
|
|
|
protected function updateFields(EntityDefinitionConfig $definition, $targetAction) |
116
|
|
|
{ |
117
|
|
|
$renameMap = [ |
118
|
|
|
ActivityScope::LAST_CONTACT_DATE => 'lastContactedDate', |
119
|
|
|
ActivityScope::LAST_CONTACT_DATE_IN => 'lastContactedDateIn', |
120
|
|
|
ActivityScope::LAST_CONTACT_DATE_OUT => 'lastContactedDateOut', |
121
|
|
|
ActivityScope::CONTACT_COUNT => 'timesContacted', |
122
|
|
|
ActivityScope::CONTACT_COUNT_IN => 'timesContactedIn', |
123
|
|
|
ActivityScope::CONTACT_COUNT_OUT => 'timesContactedOut', |
124
|
|
|
]; |
125
|
|
|
foreach ($renameMap as $fieldName => $resultFieldName) { |
126
|
|
|
if ($definition->hasField($fieldName) && !$definition->hasField($resultFieldName)) { |
127
|
|
|
$field = $definition->getField($fieldName); |
128
|
|
|
if (!$field->hasPropertyPath()) { |
129
|
|
|
$definition->removeField($fieldName); |
130
|
|
|
$field->setPropertyPath($fieldName); |
131
|
|
|
$definition->addField($resultFieldName, $field); |
132
|
|
|
} |
133
|
|
|
if ('update' === $targetAction && !$field->hasExcluded() && !$field->isExcluded()) { |
134
|
|
|
$field->setExcluded(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..