Conditions | 18 |
Paths | 1407 |
Total Lines | 88 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function load(ObjectManager $manager) |
||
63 | { |
||
64 | $fieldGroups['lead'] = FieldModel::$coreFields; |
||
|
|||
65 | $fieldGroups['company'] = FieldModel::$coreCompanyFields; |
||
66 | |||
67 | $translator = $this->container->get('translator'); |
||
68 | $indexesToAdd = []; |
||
69 | foreach ($fieldGroups as $object => $fields) { |
||
70 | if ('company' === $object) { |
||
71 | /** @var ColumnSchemaHelper $schema */ |
||
72 | $schema = $this->container->get('mautic.schema.helper.column')->setName('companies'); |
||
73 | } else { |
||
74 | /** @var ColumnSchemaHelper $schema */ |
||
75 | $schema = $this->container->get('mautic.schema.helper.column')->setName('leads'); |
||
76 | } |
||
77 | |||
78 | $order = 1; |
||
79 | foreach ($fields as $alias => $field) { |
||
80 | $type = isset($field['type']) ? $field['type'] : 'text'; |
||
81 | |||
82 | $entity = new LeadField(); |
||
83 | $entity->setLabel($translator->trans('mautic.lead.field.'.$alias, [], 'fixtures')); |
||
84 | $entity->setGroup(isset($field['group']) ? $field['group'] : 'core'); |
||
85 | $entity->setOrder($order); |
||
86 | $entity->setAlias($alias); |
||
87 | $entity->setIsRequired(isset($field['required']) ? $field['required'] : false); |
||
88 | $entity->setType($type); |
||
89 | $entity->setObject($field['object']); |
||
90 | $entity->setIsUniqueIdentifer(!empty($field['unique'])); |
||
91 | $entity->setProperties(isset($field['properties']) ? $field['properties'] : []); |
||
92 | $entity->setIsFixed(!empty($field['fixed'])); |
||
93 | $entity->setIsListable(!empty($field['listable'])); |
||
94 | $entity->setIsShortVisible(!empty($field['short'])); |
||
95 | |||
96 | if (isset($field['default'])) { |
||
97 | $entity->setDefaultValue($field['default']); |
||
98 | } |
||
99 | |||
100 | $manager->persist($entity); |
||
101 | $manager->flush(); |
||
102 | |||
103 | try { |
||
104 | $schema->addColumn( |
||
105 | FieldModel::getSchemaDefinition($alias, $type, $entity->getIsUniqueIdentifier()) |
||
106 | ); |
||
107 | } catch (SchemaException $e) { |
||
108 | // Schema already has this custom field; likely defined as a property in the entity class itself |
||
109 | } |
||
110 | |||
111 | if ($this->addIndexes) { |
||
112 | $indexesToAdd[$object][$alias] = $field; |
||
113 | } |
||
114 | |||
115 | if (!$this->hasReference('leadfield-'.$alias)) { |
||
116 | $this->addReference('leadfield-'.$alias, $entity); |
||
117 | } |
||
118 | ++$order; |
||
119 | } |
||
120 | |||
121 | $schema->executeChanges(); |
||
122 | } |
||
123 | |||
124 | foreach ($indexesToAdd as $object => $indexes) { |
||
125 | if ('company' === $object) { |
||
126 | /** @var IndexSchemaHelper $indexHelper */ |
||
127 | $indexHelper = $this->container->get('mautic.schema.helper.index')->setName('companies'); |
||
128 | } else { |
||
129 | /** @var IndexSchemaHelper $indexHelper */ |
||
130 | $indexHelper = $this->container->get('mautic.schema.helper.index')->setName('leads'); |
||
131 | } |
||
132 | |||
133 | foreach ($indexes as $name => $field) { |
||
134 | $type = (isset($field['type'])) ? $field['type'] : 'text'; |
||
135 | if ('textarea' !== $type) { |
||
136 | $indexHelper->addIndex([$name], $name.'_search'); |
||
137 | } |
||
138 | } |
||
139 | if ('lead' === $object) { |
||
140 | // Add an attribution index |
||
141 | $indexHelper->addIndex(['attribution', 'attribution_date'], 'contact_attribution'); |
||
142 | //Add date added and country index |
||
143 | $indexHelper->addIndex(['date_added', 'country'], 'date_added_country_index'); |
||
144 | } else { |
||
145 | $indexHelper->addIndex(['companyname', 'companyemail'], 'company_filter'); |
||
146 | $indexHelper->addIndex(['companyname', 'companycity', 'companycountry', 'companystate'], 'company_match'); |
||
147 | } |
||
148 | |||
149 | $indexHelper->executeChanges(); |
||
150 | } |
||
161 |