Passed
Pull Request — staging (#8238)
by Dennis
18:02 queued 04:45
created

LeadList::getPublicName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\LeadBundle\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Mautic\CoreBundle\Entity\FormEntity;
19
use Mautic\LeadBundle\Form\Validator\Constraints\UniqueUserAlias;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Component\Validator\Mapping\ClassMetadata;
22
23
/**
24
 * Class LeadList.
25
 */
26
class LeadList extends FormEntity
27
{
28
    /**
29
     * @var int
30
     */
31
    private $id;
32
33
    /**
34
     * @var string
35
     */
36
    private $name;
37
38
    /**
39
     * @var string
40
     */
41
    private $publicName;
42
43
    /**
44
     * @var string
45
     */
46
    private $description;
47
48
    /**
49
     * @var string
50
     */
51
    private $alias;
52
53
    /**
54
     * @var array
55
     */
56
    private $filters = [];
57
58
    /**
59
     * @var bool
60
     */
61
    private $isGlobal = true;
62
63
    /**
64
     * @var bool
65
     */
66
    private $isPreferenceCenter = false;
67
68
    /**
69
     * @var ArrayCollection
70
     */
71
    private $leads;
72
73
    /**
74
     * Construct.
75
     */
76
    public function __construct()
77
    {
78
        $this->leads = new ArrayCollection();
79
    }
80
81
    public static function loadMetadata(ORM\ClassMetadata $metadata)
82
    {
83
        $builder = new ClassMetadataBuilder($metadata);
84
85
        $builder->setTable('lead_lists')
86
            ->setCustomRepositoryClass(LeadListRepository::class);
87
88
        $builder->addIdColumns();
89
90
        $builder->addField('alias', 'string');
91
92
        $builder->createField('publicName', 'string')
93
            ->columnName('public_name')
94
            ->build();
95
96
        $builder->addField('filters', 'array');
97
98
        $builder->createField('isGlobal', 'boolean')
99
            ->columnName('is_global')
100
            ->build();
101
102
        $builder->createField('isPreferenceCenter', 'boolean')
103
            ->columnName('is_preference_center')
104
            ->build();
105
106
        $builder->createOneToMany('leads', 'ListLead')
107
            ->setIndexBy('id')
108
            ->mappedBy('list')
109
            ->fetchExtraLazy()
110
            ->build();
111
    }
112
113
    public static function loadValidatorMetadata(ClassMetadata $metadata)
114
    {
115
        $metadata->addPropertyConstraint('name', new Assert\NotBlank(
116
            ['message' => 'mautic.core.name.required']
117
        ));
118
119
        $metadata->addConstraint(new UniqueUserAlias([
120
            'field'   => 'alias',
121
            'message' => 'mautic.lead.list.alias.unique',
122
        ]));
123
    }
124
125
    /**
126
     * Prepares the metadata for API usage.
127
     *
128
     * @param $metadata
129
     */
130
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
131
    {
132
        $metadata->setGroupPrefix('leadList')
133
            ->addListProperties(
134
                [
135
                    'id',
136
                    'name',
137
                    'publicName',
138
                    'alias',
139
                    'description',
140
                ]
141
            )
142
            ->addProperties(
143
                [
144
                    'filters',
145
                    'isGlobal',
146
                    'isPreferenceCenter',
147
                ]
148
            )
149
            ->build();
150
    }
151
152
    /**
153
     * Get id.
154
     *
155
     * @return int
156
     */
157
    public function getId()
158
    {
159
        return $this->id;
160
    }
161
162
    /**
163
     * Set name.
164
     *
165
     * @param int $name
166
     *
167
     * @return LeadList
168
     */
169
    public function setName($name)
170
    {
171
        $this->isChanged('name', $name);
172
        $this->name = $name;
173
174
        return $this;
175
    }
176
177
    /**
178
     * Get name.
179
     *
180
     * @return int
181
     */
182
    public function getName()
183
    {
184
        return $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->name returns the type string which is incompatible with the documented return type integer.
Loading history...
185
    }
186
187
    /**
188
     * Set description.
189
     *
190
     * @param string $description
191
     *
192
     * @return LeadList
193
     */
194
    public function setDescription($description)
195
    {
196
        $this->isChanged('description', $description);
197
        $this->description = $description;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Get description.
204
     *
205
     * @return string
206
     */
207
    public function getDescription()
208
    {
209
        return $this->description;
210
    }
211
212
    /**
213
     * Get publicName.
214
     *
215
     * @return string
216
     */
217
    public function getPublicName()
218
    {
219
        return $this->publicName;
220
    }
221
222
    /**
223
     * Set publicName.
224
     *
225
     * @param string $publicName
226
     *
227
     * @return LeadList
228
     */
229
    public function setPublicName($publicName)
230
    {
231
        $this->isChanged('publicName', $publicName);
232
        $this->publicName = $publicName;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set filters.
239
     *
240
     * @return LeadList
241
     */
242
    public function setFilters(array $filters)
243
    {
244
        $this->isChanged('filters', $filters);
245
        $this->filters = $filters;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Get filters.
252
     *
253
     * @return array
254
     */
255
    public function getFilters()
256
    {
257
        if (is_array($this->filters)) {
0 ignored issues
show
introduced by
The condition is_array($this->filters) is always true.
Loading history...
258
            return $this->addLegacyParams($this->filters);
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\LeadBundle\Entity...List::addLegacyParams() has been deprecated: remove after several of years. ( Ignorable by Annotation )

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

258
            return /** @scrutinizer ignore-deprecated */ $this->addLegacyParams($this->filters);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
259
        }
260
261
        return $this->filters;
262
    }
263
264
    /**
265
     * Set isGlobal.
266
     *
267
     * @param bool $isGlobal
268
     *
269
     * @return LeadList
270
     */
271
    public function setIsGlobal($isGlobal)
272
    {
273
        $this->isChanged('isGlobal', $isGlobal);
274
        $this->isGlobal = $isGlobal;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Get isGlobal.
281
     *
282
     * @return bool
283
     */
284
    public function getIsGlobal()
285
    {
286
        return $this->isGlobal;
287
    }
288
289
    /**
290
     * Proxy function to getIsGlobal().
291
     *
292
     * @return bool
293
     */
294
    public function isGlobal()
295
    {
296
        return $this->getIsGlobal();
297
    }
298
299
    /**
300
     * Set alias.
301
     *
302
     * @param string $alias
303
     *
304
     * @return LeadList
305
     */
306
    public function setAlias($alias)
307
    {
308
        $this->isChanged('alias', $alias);
309
        $this->alias = $alias;
310
311
        return $this;
312
    }
313
314
    /**
315
     * Get alias.
316
     *
317
     * @return string
318
     */
319
    public function getAlias()
320
    {
321
        return $this->alias;
322
    }
323
324
    /**
325
     * Get leads.
326
     *
327
     * @return \Doctrine\Common\Collections\Collection
328
     */
329
    public function getLeads()
330
    {
331
        return $this->leads;
332
    }
333
334
    /**
335
     * Clone entity with empty contact list.
336
     */
337
    public function __clone()
338
    {
339
        parent::__clone();
340
341
        $this->id    = null;
342
        $this->leads = new ArrayCollection();
343
        $this->setIsPublished(false);
344
        $this->setAlias('');
345
    }
346
347
    /**
348
     * @return bool
349
     */
350
    public function getIsPreferenceCenter()
351
    {
352
        return $this->isPreferenceCenter;
353
    }
354
355
    /**
356
     * @param bool $isPreferenceCenter
357
     */
358
    public function setIsPreferenceCenter($isPreferenceCenter)
359
    {
360
        $this->isChanged('isPreferenceCenter', $isPreferenceCenter);
361
        $this->isPreferenceCenter = $isPreferenceCenter;
362
    }
363
364
    /**
365
     * @deprecated remove after several of years.
366
     *
367
     * This is needed go keep BC after we moved 'filter' and 'display' params
368
     * to the 'properties' array.
369
     */
370
    private function addLegacyParams(array $filters): array
371
    {
372
        return array_map(
373
            function (array $filter) {
374
                $filter['filter'] = $filter['properties']['filter'] ?? $filter['filter'] ?? null;
375
                $filter['display'] = $filter['properties']['display'] ?? $filter['display'] ?? null;
376
377
                return $filter;
378
            },
379
            $filters
380
        );
381
    }
382
}
383