Issues (3627)

InstallFixtures/ORM/LeadFieldData.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\InstallBundle\InstallFixtures\ORM;
13
14
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
15
use Doctrine\Common\DataFixtures\AbstractFixture;
16
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Mautic\CoreBundle\Doctrine\Helper\ColumnSchemaHelper;
19
use Mautic\CoreBundle\Doctrine\Helper\IndexSchemaHelper;
20
use Mautic\CoreBundle\Exception\SchemaException;
21
use Mautic\LeadBundle\Entity\LeadField;
22
use Mautic\LeadBundle\Model\FieldModel;
23
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
26
class LeadFieldData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface, FixtureGroupInterface
27
{
28
    /**
29
     * @var bool
30
     */
31
    private $addIndexes;
32
33
    /**
34
     * @var ContainerInterface
35
     */
36
    private $container;
37
38
    public function __construct(bool $addIndexes = true)
39
    {
40
        $this->addIndexes = $addIndexes;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getGroups(): array
47
    {
48
        return ['group_install', 'group_mautic_install_data'];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function setContainer(ContainerInterface $container = null)
55
    {
56
        $this->container = $container;
57
    }
58
59
    /**
60
     * @throws \Doctrine\DBAL\Schema\SchemaException
61
     */
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'] : []);
0 ignored issues
show
It seems like IssetNode ? $field['properties'] : array() can also be of type array; however, parameter $properties of Mautic\LeadBundle\Entity...dField::setProperties() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                $entity->setProperties(/** @scrutinizer ignore-type */ isset($field['properties']) ? $field['properties'] : []);
Loading history...
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
        }
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getOrder()
157
    {
158
        return 4;
159
    }
160
}
161