Completed
Push — 1.10 ( c4e320...ea813f )
by
unknown
27:01
created

getEntityLevelContactInfoColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MarketingListBundle\Model;
4
5
use Oro\Component\PhpUtils\ArrayUtil;
6
7
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
8
use Oro\Bundle\EntityBundle\Provider\EntityFieldProvider;
9
use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider;
10
use Oro\Bundle\QueryDesignerBundle\Model\AbstractQueryDesigner;
11
use Oro\Bundle\QueryDesignerBundle\QueryDesigner\JoinIdentifierHelper;
12
13
class ContactInformationFieldHelper
14
{
15
    /** @var ConfigProvider */
16
    protected $configProvider;
17
18
    /** @var array */
19
    protected $entityContactInfoColumns = array();
20
21
    /** @var DoctrineHelper */
22
    protected $doctrineHelper;
23
24
    /** @var EntityFieldProvider */
25
    protected $fieldProvider;
26
27
    /**
28
     * @param ConfigProvider $configProvider
29
     * @param DoctrineHelper $doctrineHelper
30
     * @param EntityFieldProvider $fieldProvider
31
     */
32
    public function __construct(
33
        ConfigProvider $configProvider,
34
        DoctrineHelper $doctrineHelper,
35
        EntityFieldProvider $fieldProvider
36
    ) {
37
        $this->configProvider = $configProvider;
38
        $this->doctrineHelper = $doctrineHelper;
39
        $this->fieldProvider = $fieldProvider;
40
    }
41
42
    /**
43
     * @deprecated since 1.9.9 to be removed in 2.0.0 use getQueryContactInformationFields instead.
44
     *
45
     * @param AbstractQueryDesigner $queryDesigner
46
     *
47
     * @return array
48
     */
49
    public function getQueryContactInformationColumns(AbstractQueryDesigner $queryDesigner)
50
    {
51
        return $this->getQueryContactInformationFields($queryDesigner);
52
    }
53
54
    /**
55
     * @param AbstractQueryDesigner $queryDesigner
56
     *
57
     * @return array
58
     */
59
    public function getQueryContactInformationFields(AbstractQueryDesigner $queryDesigner)
60
    {
61
        $fields = [];
62
63
        // If definition is empty there is no one contact information field
64
        $definition = $queryDesigner->getDefinition();
65
        if (!$definition) {
66
            return $fields;
67
        }
68
69
        $definition = json_decode($definition, JSON_OBJECT_AS_ARRAY);
70
        if (empty($definition['columns'])) {
71
            return $fields;
72
        }
73
74
        $entity = $queryDesigner->getEntity();
75
        foreach ($definition['columns'] as $column) {
76
            $contactInformationType = $this->getContactInformationFieldType($entity, $column['name']);
77
            if (!empty($contactInformationType)) {
78
                $fields[$contactInformationType][] = $column;
79
            }
80
        }
81
82
        return $fields;
83
    }
84
85
    /**
86
     * Get entity contact information fields.
87
     *
88
     * @deprecated since 1.9.9 to be removed in 2.0.0 use getEntityContactInformationFields instead.
89
     *
90
     * @param string|object $entity
91
     * @return array
92
     */
93
    public function getEntityContactInformationColumns($entity)
94
    {
95
        return $this->getEntityContactInformationFields($entity);
96
    }
97
98
    /**
99
     * Get entity contact information fields.
100
     *
101
     * @param string|object $entity
102
     * @return array
103
     */
104
    public function getEntityContactInformationFields($entity)
105
    {
106
        $metadata = $this->doctrineHelper->getEntityMetadata($entity);
107
        $fields = $metadata->getFieldNames();
108
        $contactInformationFields = [];
109
        foreach ($fields as $field) {
110
            if ($type = $this->getContactInformationFieldType($entity, $field)) {
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 104 can also be of type object; however, OroCRM\Bundle\MarketingL...tInformationFieldType() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
111
                $contactInformationFields[$field] = $type;
112
            }
113
        }
114
115
        return array_merge($contactInformationFields, $this->getEntityLevelContactInfoFields($entity));
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 104 can also be of type object; however, OroCRM\Bundle\MarketingL...evelContactInfoFields() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
116
    }
117
118
    /**
119
     * @deprecated since 1.9.9 to be removed in 2.0.0 use getEntityContactInformationFieldsInfo instead.
120
     *
121
     * @param string $entity
122
     * @return array
123
     */
124
    public function getEntityContactInformationColumnsInfo($entity)
125
    {
126
        return $this->getEntityContactInformationFieldsInfo($entity);
127
    }
128
129
    /**
130
     * @param string $entity
131
     * @return array
132
     */
133
    public function getEntityContactInformationFieldsInfo($entity)
134
    {
135
        $fields = [];
136
        $contactInformationFields = $this->getEntityContactInformationFields($entity);
137
        $entityFields = $this->fieldProvider->getFields($entity, false, true);
138
139
        foreach ($entityFields as $entityField) {
140
            if (array_key_exists($entityField['name'], $contactInformationFields)) {
141
                $entityField['contact_information_type'] = $contactInformationFields[$entityField['name']];
142
                $fields[] = $entityField;
143
            }
144
        }
145
146
        return $fields;
147
    }
148
149
    /**
150
     * @param string $entity
151
     * @param string $fieldName
152
     * @return string|null
153
     */
154
    public function getContactInformationFieldType($entity, $fieldName)
155
    {
156
        $contactInformationType = null;
157
        $identifierHelper = new JoinIdentifierHelper($entity);
158
        $className = $identifierHelper->getEntityClassName($fieldName);
159
        $fieldName = $identifierHelper->getFieldName($fieldName);
160
161
        if (!array_key_exists($className, $this->entityContactInfoColumns)) {
162
            $this->entityContactInfoColumns[$className] = $this->getEntityLevelContactInfoFields($className);
163
        }
164
165
        if (!empty($this->entityContactInfoColumns[$className][$fieldName])) {
166
            $contactInformationType = $this->entityContactInfoColumns[$className][$fieldName];
167
        } elseif ($this->configProvider->hasConfig($className, $fieldName)) {
168
            $fieldConfiguration = $this->configProvider->getConfig($className, $fieldName);
169
            $contactInformationType = $fieldConfiguration->get('contact_information');
170
        }
171
172
        return $contactInformationType;
173
    }
174
175
    /**
176
     * @deprecated since 1.9.9 to be removed in 2.0.0 use getEntityLevelContactInfoFields instead.
177
     *
178
     * @param string $entity
179
     * @return array
180
     */
181
    protected function getEntityLevelContactInfoColumns($entity)
182
    {
183
        return $this->getEntityLevelContactInfoFields($entity);
184
    }
185
186
    /**
187
     * @param string $entity
188
     * @return array
189
     */
190
    protected function getEntityLevelContactInfoFields($entity)
191
    {
192
        $result = [];
193
        if ($this->configProvider->hasConfig($entity)) {
194
            $entityContactInformation = $this->configProvider
195
                ->getConfig($entity)
196
                ->get('contact_information');
197
198
            if ($entityContactInformation) {
199
                foreach ($entityContactInformation as $contactInfoType => $contactInfoFields) {
200
                    $entityColumns = ArrayUtil::arrayColumn($contactInfoFields, 'fieldName');
201
                    foreach ($entityColumns as $entityColumn) {
202
                        $result[$entityColumn] = $contactInfoType;
203
                    }
204
                }
205
            }
206
        }
207
208
        return $result;
209
    }
210
}
211