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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php

namespace Mautic\CoreBundle\Doctrine\Mapping;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder as OrmClassMetadataBuilder;
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Mautic\CategoryBundle\Entity\Category;
use Mautic\CoreBundle\Entity\IpAddress;
use Mautic\LeadBundle\Entity\Lead;

/**
 * Override Doctrine's builder classes to add support to orphanRemoval until the fix is incorporated into Doctrine release
 * See @see https://github.com/doctrine/doctrine2/pull/1326/.
 */
class ClassMetadataBuilder extends OrmClassMetadataBuilder
{
    /**
     * Max length of indexed VARCHAR fields for UTF8MB4 encoding.
     */
    public const MAX_VARCHAR_INDEXED_LENGTH = 191;

    public function __construct(ClassMetadataInfo $cm)
    {
        parent::__construct($cm);

        // Default all Mautic entities to explicit
        $this->setChangeTrackingPolicyDeferredExplicit();
    }

    /**
     * Creates a ManyToOne Association Builder.
     *
     * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
     *
     * @param string $name
     * @param string $targetEntity
     *
     * @return AssociationBuilder
     */
    public function createManyToOne($name, $targetEntity)
    {
        return new AssociationBuilder(
            $this,
            [
                'fieldName'    => $name,
                'targetEntity' => $targetEntity,
            ],
            ClassMetadata::MANY_TO_ONE
        );
    }

    /**
     * Creates a OneToOne Association Builder.
     *
     * @param string $name
     * @param string $targetEntity
     *
     * @return AssociationBuilder
     */
    public function createOneToOne($name, $targetEntity)
    {
        return new AssociationBuilder(
            $this,
            [
                'fieldName'    => $name,
                'targetEntity' => $targetEntity,
            ],
            ClassMetadata::ONE_TO_ONE
        );
    }

    /**
     * Creates a ManyToMany Association Builder.
     *
     * @param string $name
     * @param string $targetEntity
     *
     * @return ManyToManyAssociationBuilder
     */
    public function createManyToMany($name, $targetEntity)
    {
        return new ManyToManyAssociationBuilder(
            $this,
            [
                'fieldName'    => $name,
                'targetEntity' => $targetEntity,
            ],
            ClassMetadata::MANY_TO_MANY
        );
    }

    /**
     * Creates a one to many association builder.
     *
     * @param string $name
     * @param string $targetEntity
     *
     * @return OneToManyAssociationBuilder
     */
    public function createOneToMany($name, $targetEntity)
    {
        return new OneToManyAssociationBuilder(
            $this,
            [
                'fieldName'    => $name,
                'targetEntity' => $targetEntity,
            ],
            ClassMetadata::ONE_TO_MANY
        );
    }

    /**
     * Add Id column.
     *
     * @return $this
     */
    public function addId()
    {
        $this->createField('id', Types::INTEGER)
            ->makePrimaryKey()
            ->generatedValue()
            ->option('unsigned', true)
            ->build();

        return $this;
    }

    /**
     * Adds autogenerated ID field type of BIGINT UNSIGNED.
     *
     * @param string $columnName
     * @param bool   $isPrimary
     * @param bool   $isNullable
     *
     * @return ClassMetadataBuilder
     */
    public function addBigIntIdField($fieldName = 'id', $columnName = 'id', $isPrimary = true, $isNullable = false)
    {
        $cm = $this->getClassMetadata();
        $cm->mapField(
            [
                'fieldName'  => $fieldName,
                'columnName' => $columnName,
                'id'         => $isPrimary,
                'nullable'   => $isNullable,
                'type'       => Types::BIGINT,
                'options'    => [
                    'unsigned' => true,
                ],
            ]
        );

        if ($isPrimary) {
            $cm->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
        }

        return $this;
    }

    /**
     * Add UUID as Id.
     *
     * @return $this
     */
    public function addUuid()
    {
        $this->createField('id', 'guid')
            ->makePrimaryKey()
            ->build();

        return $this;
    }

    /**
     * Add id, name, and description columns.
     *
     * @param string $nameColumn
     * @param string $descriptionColumn
     *
     * @return $this
     */
    public function addIdColumns($nameColumn = 'name', $descriptionColumn = 'description')
    {
        $this->addId();

        if ($nameColumn) {
            $this->createField($nameColumn, Types::STRING)
                ->build();
        }

        if ($descriptionColumn) {
            $this->createField($descriptionColumn, Types::TEXT)
                ->nullable()
                ->build();
        }

        return $this;
    }

    /**
     * Add category to metadata.
     *
     * @return $this
     */
    public function addCategory()
    {
        $this->createManyToOne('category', Category::class)
            ->cascadeMerge()
            ->cascadeDetach()
            ->addJoinColumn('category_id', 'id', true, false, 'SET NULL')
            ->build();

        return $this;
    }

    /**
     * Add publish up and down dates to metadata.
     *
     * @return $this
     */
    public function addPublishDates()
    {
        $this->createField('publishUp', Types::DATETIME_MUTABLE)
            ->columnName('publish_up')
            ->nullable()
            ->build();

        $this->createField('publishDown', Types::DATETIME_MUTABLE)
            ->columnName('publish_down')
            ->nullable()
            ->build();

        return $this;
    }

    /**
     * Added dateAdded column.
     *
     * @param bool|false $nullable
     *
     * @return $this
     */
    public function addDateAdded($nullable = false)
    {
        $dateAdded = $this->createField('dateAdded', Types::DATETIME_MUTABLE)
            ->columnName('date_added');

        if ($nullable) {
            $dateAdded->nullable();
        }

        $dateAdded->build();

        return $this;
    }

    /**
     * Add a contact column.
     *
     * @param bool|false  $nullable
     * @param string      $onDelete
     * @param bool|false  $isPrimaryKey
     * @param string|null $inversedBy
     *
     * @return $this
     */
    public function addContact($nullable = false, $onDelete = 'CASCADE', $isPrimaryKey = false, $inversedBy = null)
    {
        $lead = $this->createManyToOne('contact', Lead::class);

        if ($isPrimaryKey) {
            $lead->makePrimaryKey();
        }

        if ($inversedBy) {
            $lead->inversedBy($inversedBy);
        }

        $lead
            ->addJoinColumn('contact_id', 'id', $nullable, false, $onDelete)
            ->build();

        return $this;
    }

    /**
     * Add a lead column.
     *
     * @param bool|false $nullable
     * @param string     $onDelete
     * @param bool|false $isPrimaryKey
     *
     * @deprecated Use addContact instead; existing implementations will need a migration to rename lead_id to contact_id
     *
     * @return $this
     */
    public function addLead($nullable = false, $onDelete = 'CASCADE', $isPrimaryKey = false, $inversedBy = null)
    {
        $lead = $this->createManyToOne('lead', Lead::class);

        if ($isPrimaryKey) {
            $lead->makePrimaryKey();
        }

        if ($inversedBy) {
            $lead->inversedBy($inversedBy);
        }

        $lead
            ->addJoinColumn('lead_id', 'id', $nullable, false, $onDelete)
            ->build();

        return $this;
    }

    /**
     * Adds IP address.
     *
     * @param bool $nullable
     *
     * @return $this
     */
    public function addIpAddress($nullable = false)
    {
        $this->createManyToOne('ipAddress', IpAddress::class)
            ->cascadePersist()
            ->cascadeMerge()
            ->cascadeDetach()
            ->addJoinColumn('ip_id', 'id', $nullable, false, 'SET NULL')
            ->build();

        return $this;
    }

    /**
     * Add a nullable field.
     *
     * @param string      $name
     * @param string      $type
     * @param string|null $columnName
     *
     * @return $this
     */
    public function addNullableField($name, $type = Types::STRING, $columnName = null)
    {
        $field = $this->createField($name, $type)
            ->nullable();

        if (null !== $columnName) {
            $field->columnName($columnName);
        }

        if ($this->isIndexedVarchar($columnName ?? $name, $type)) {
            $field->length(self::MAX_VARCHAR_INDEXED_LENGTH);
        }

        $field->build();

        return $this;
    }

    /**
     * Add a field with a custom column name.
     *
     * @param bool $nullable
     *
     * @return $this
     */
    public function addNamedField($name, $type, $columnName, $nullable = false)
    {
        $field = $this->createField($name, $type)
            ->columnName($columnName);

        if ($nullable) {
            $field->nullable();
        }

        if ($this->isIndexedVarchar($columnName ?? $name, $type)) {
            $field->length(self::MAX_VARCHAR_INDEXED_LENGTH);
        }

        $field->build();

        return $this;
    }

    /**
     * Adds Field. Overridden for IDE suggestions when stringing methods in entity class.
     *
     * @param string $name
     * @param string $type
     *
     * @return $this
     */
    public function addField($name, $type, array $mapping = [])
    {
        if ($this->isIndexedVarchar($name, $type)) {
            $mapping['length'] = self::MAX_VARCHAR_INDEXED_LENGTH;
        }

        return parent::addField($name, $type, $mapping);
    }

    public function createField($name, $type)
    {
        $mapping = [
            'fieldName' => $name,
            'type'      => $type,
        ];

        if ($this->isIndexedVarchar($name, $type)) {
            $mapping['length'] = self::MAX_VARCHAR_INDEXED_LENGTH;
        }

        return new FieldBuilder($this, $mapping);
    }

    /**
     * @param string  $name
     * @param mixed[] $flags
     * @param mixed[] $options
     */
    public function addIndex(array $columns, $name, array $flags = null, array $options = null): self
    {
        $cm = $this->getClassMetadata();

        if (!isset($cm->table['indexes'])) {
            $cm->table['indexes'] = [];
        }

        $definition = ['columns' => $columns];

        if (null !== $flags) {
            $definition['flags'] = $flags;
        }

        if (null !== $options) {
            $definition['options'] = $options;
        }

        $cm->table['indexes'][$name] = $definition;

        return $this;
    }

    /**
     * @deprecated this method will be removed as MySQL does not support partial indices whatsoever
     *
     * @param string $name
     * @param string $where
     */
    public function addPartialIndex(array $columns, $name, $where): ClassMetadataBuilder
    {
        return $this->addIndex($columns, $name, null, ['where' => $where]);
    }

    /**
     * @param mixed[] $columns
     */
    public function addFulltextIndex(array $columns, string $name): self
    {
        return $this->addIndex($columns, $name, ['fulltext']);
    }

    /**
     * UTF8MB4 encoding needs max length of 191 instead of 255 that UTF8 needed. Doctrine does not take care of it by itself.
     */
    public function isIndexedVarchar(string $name, string $type): bool
    {
        return Types::STRING === $type || isset($this->getClassMetadata()->table['indexes'][$name]);
    }

    /**
     * Adds Index with options.
     *
     * @param list<string>         $columns
     * @param array<string, mixed> $options
     */
    public function addIndexWithOptions(array $columns, string $name, array $options): ClassMetadataBuilder
    {
        $cm = $this->getClassMetadata();

        $cm->table['indexes'][$name] = [
            'columns' => $columns,
            'options' => $options,
        ];

        return $this;
    }
}