Completed
Push — master ( 584969...3f5763 )
by
unknown
11:30
created

UpdateActivityContactFields   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
c 1
b 0
f 1
lcom 1
cbo 7
dl 0
loc 120
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B process() 0 22 4
B isSupportedEntity() 0 27 6
C updateFields() 0 24 8
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $excludedActions of type string is incompatible with the declared type array<integer,string> of property $excludedActions.

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

Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $definition defined by $context->getResult() on line 59 can be null; however, OroCRM\Bundle\ActivityCo...tFields::updateFields() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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