TradingCardModel::populate()   F
last analyzed

Complexity

Conditions 16
Paths 512

Size

Total Lines 38

Duplication

Lines 12
Ratio 31.58 %

Importance

Changes 0
Metric Value
dl 12
loc 38
rs 2.0777
c 0
b 0
f 0
cc 16
nc 512
nop 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace PlaygroundGame\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
8
use Doctrine\ORM\Mapping\PrePersist;
9
use Doctrine\ORM\Mapping\PreUpdate;
10
11
use Gedmo\Mapping\Annotation as Gedmo;
12
use Gedmo\Translatable\Translatable;
13
14
use Zend\InputFilter\Factory as InputFactory;
15
use Zend\InputFilter\InputFilter;
16
use Zend\InputFilter\InputFilterAwareInterface;
17
use Zend\InputFilter\InputFilterInterface;
18
19
/**
20
 * @ORM\Entity @HasLifecycleCallbacks
21
 * @ORM\Table(name="game_tradingcard_model")
22
 * @Gedmo\TranslationEntity(class="PlaygroundGame\Entity\GameTranslation")
23
 */
24
25
class TradingCardModel implements InputFilterAwareInterface, \JsonSerializable
26
{
27
    protected $inputFilter;
28
29
    /**
30
     * @ORM\Id
31
     * @ORM\Column(type="integer");
32
     * @ORM\GeneratedValue(strategy="AUTO")
33
     */
34
    protected $id;
35
36
    /**
37
     * @ORM\ManyToOne(targetEntity="TradingCard", inversedBy="models")
38
     * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE")
39
     */
40
    protected $game;
41
42
    /**
43
     * @ORM\OneToMany(targetEntity="TradingCardCard", mappedBy="model", cascade={"persist","remove"})
44
     */
45
    private $cards;
46
47
    /**
48
     * @Gedmo\Translatable
49
     * @ORM\Column(type="string", length=255, nullable=false)
50
     */
51
    protected $title;
52
53
    /**
54
     * @Gedmo\Translatable
55
     * @ORM\Column(type="text", nullable=true)
56
     */
57
    protected $description;
58
59
    /**
60
     * @Gedmo\Translatable
61
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
62
     */
63
    protected $image;
64
65
    /**
66
     * The type of the card : collector, standard, ...
67
     * @Gedmo\Translatable
68
     * @ORM\Column(type="string", length=255, nullable=true)
69
     */
70
    protected $type;
71
72
    /**
73
     * a card can be part of a set of cards
74
     * @Gedmo\Translatable
75
     * @ORM\Column(type="string", length=255, nullable=true)
76
     */
77
    protected $family;
78
79
    /**
80
     * @ORM\Column(type="integer", nullable=true)
81
     */
82
    protected $points = 0;
83
84
    /**
85
     * @ORM\Column(type="float", nullable=false)
86
     */
87
    protected $distribution = 1;
88
89
    /**
90
     * datetime when this model will be available
91
     * @ORM\Column(type="datetime", nullable=true)
92
     */
93
    protected $availability;
94
95
    /**
96
     * @ORM\Column(name="json_data", type="text", nullable=true)
97
     */
98
    protected $jsonData;
99
100
    /**
101
     * @ORM\Column(name="created_at", type="datetime")
102
     */
103
    protected $createdAt;
104
105
    /**
106
     * @ORM\Column(name="updated_at",type="datetime")
107
     */
108
    protected $updatedAt;
109
110
    public function __construct()
111
    {
112
        $this->cards = new ArrayCollection();
113
    }
114
115
    /**
116
     * @PrePersist
117
     */
118
    public function createChrono()
119
    {
120
        $this->createdAt = new \DateTime("now");
121
        $this->updatedAt = new \DateTime("now");
122
    }
123
124
    /**
125
     * @PreUpdate
126
     */
127
    public function updateChrono()
128
    {
129
        $this->updatedAt = new \DateTime("now");
130
    }
131
132
    /**
133
     * Gets the value of id.
134
     *
135
     * @return mixed
136
     */
137
    public function getId()
138
    {
139
        return $this->id;
140
    }
141
142
    /**
143
     * Sets the value of id.
144
     *
145
     * @param mixed $id the id
146
     *
147
     * @return self
148
     */
149
    public function setId($id)
150
    {
151
        $this->id = $id;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Gets the value of title.
158
     *
159
     * @return mixed
160
     */
161
    public function getTitle()
162
    {
163
        return $this->title;
164
    }
165
166
    /**
167
     * Sets the value of title.
168
     *
169
     * @param mixed $title the title
170
     *
171
     * @return self
172
     */
173
    public function setTitle($title)
174
    {
175
        $this->title = $title;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Gets the value of description.
182
     *
183
     * @return mixed
184
     */
185
    public function getDescription()
186
    {
187
        return $this->description;
188
    }
189
190
    /**
191
     * Sets the value of description.
192
     *
193
     * @param mixed $description the description
194
     *
195
     * @return self
196
     */
197
    public function setDescription($description)
198
    {
199
        $this->description = $description;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Gets the value of image.
206
     *
207
     * @return mixed
208
     */
209
    public function getImage()
210
    {
211
        return $this->image;
212
    }
213
214
    /**
215
     * Sets the value of image.
216
     *
217
     * @param mixed $image the image
218
     *
219
     * @return self
220
     */
221
    public function setImage($image)
222
    {
223
        $this->image = $image;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Gets the value of type.
230
     *
231
     * @return mixed
232
     */
233
    public function getType()
234
    {
235
        return $this->type;
236
    }
237
238
    /**
239
     * Sets the value of type.
240
     *
241
     * @param mixed $type the type
242
     *
243
     * @return self
244
     */
245
    public function setType($type)
246
    {
247
        $this->type = $type;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Gets the value of family.
254
     *
255
     * @return mixed
256
     */
257
    public function getFamily()
258
    {
259
        return $this->family;
260
    }
261
262
    /**
263
     * Sets the value of family.
264
     *
265
     * @param mixed $family the family
266
     *
267
     * @return self
268
     */
269
    public function setFamily($family)
270
    {
271
        $this->family = $family;
272
273
        return $this;
274
    }
275
276
    /**
277
     * Gets the value of points.
278
     *
279
     * @return mixed
280
     */
281
    public function getPoints()
282
    {
283
        return $this->points;
284
    }
285
286
    /**
287
     * Sets the value of points.
288
     *
289
     * @param mixed $points the points
290
     *
291
     * @return self
292
     */
293
    public function setPoints($points)
294
    {
295
        $this->points = $points;
296
297
        return $this;
298
    }
299
300
    /**
301
     * Gets the value of distribution.
302
     *
303
     * @return mixed
304
     */
305
    public function getDistribution()
306
    {
307
        return $this->distribution;
308
    }
309
310
    /**
311
     * Sets the value of distribution.
312
     *
313
     * @param mixed $distribution the distribution
314
     *
315
     * @return self
316
     */
317
    public function setDistribution($distribution)
318
    {
319
        $this->distribution = $distribution;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Gets the value of availability.
326
     *
327
     * @return mixed
328
     */
329
    public function getAvailability()
330
    {
331
        return $this->availability;
332
    }
333
334
    /**
335
     * Sets the value of availability.
336
     *
337
     * @param mixed $availability the availability
338
     *
339
     * @return self
340
     */
341
    public function setAvailability($availability)
342
    {
343
        $this->availability = $availability;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Gets the value of game.
350
     *
351
     * @return mixed
352
     */
353
    public function getGame()
354
    {
355
        return $this->game;
356
    }
357
358
    /**
359
     * Sets the value of game.
360
     *
361
     * @param mixed $game the game
362
     *
363
     * @return self
364
     */
365
    public function setGame($game)
366
    {
367
        $this->game = $game;
368
369
        return $this;
370
    }
371
372
    /**
373
     * Gets the value of jsonData.
374
     *
375
     * @return mixed
376
     */
377
    public function getJsonData()
378
    {
379
        return $this->jsonData;
380
    }
381
382
    /**
383
     * Sets the value of jsonData.
384
     *
385
     * @param mixed $jsonData the json data
386
     *
387
     * @return self
388
     */
389
    public function setJsonData($jsonData)
390
    {
391
        $this->jsonData = $jsonData;
392
393
        return $this;
394
    }
395
396
    /**
397
     * Gets the value of cards.
398
     *
399
     * @return mixed
400
     */
401
    public function getCards()
402
    {
403
        return $this->cards;
404
    }
405
406
    public function addCards(ArrayCollection $cards)
407
    {
408
        foreach ($cards as $card) {
409
            $card->setModel($this);
410
            $this->cards->add($card);
411
        }
412
    }
413
414
    public function removeCards(ArrayCollection $cards)
415
    {
416
        foreach ($cards as $card) {
417
            $card->setModel(null);
418
            $this->cards->removeElement($card);
419
        }
420
    }
421
422
    /**
423
     * Add an card to the Model.
424
     *
425
     * @param TradingCardModel $card
426
     *
427
     * @return void
428
     */
429
    public function addCard($card)
430
    {
431
        $this->cards[] = $card;
432
    }
433
434
    /**
435
     * Gets the value of createdAt.
436
     *
437
     * @return mixed
438
     */
439
    public function getCreatedAt()
440
    {
441
        return $this->createdAt;
442
    }
443
444
    /**
445
     * Sets the value of createdAt.
446
     *
447
     * @param mixed $createdAt the created at
448
     *
449
     * @return self
450
     */
451
    public function setCreatedAt($createdAt)
452
    {
453
        $this->createdAt = $createdAt;
454
455
        return $this;
456
    }
457
458
    /**
459
     * Gets the value of updatedAt.
460
     *
461
     * @return mixed
462
     */
463
    public function getUpdatedAt()
464
    {
465
        return $this->updatedAt;
466
    }
467
468
    /**
469
     * Sets the value of updatedAt.
470
     *
471
     * @param mixed $updatedAt the updated at
472
     *
473
     * @return self
474
     */
475
    public function setUpdatedAt($updatedAt)
476
    {
477
        $this->updatedAt = $updatedAt;
478
479
        return $this;
480
    }
481
482
    /**
483
     * Convert the object to an array.
484
     *
485
     * @return array
486
     */
487
    public function getArrayCopy()
488
    {
489
        $obj_vars = get_object_vars($this);
490
491
        return $obj_vars;
492
    }
493
494
    /**
495
     * Convert the object to json.
496
     *
497
     * @return array
498
     */
499
    public function jsonSerialize()
500
    {
501
        $jsonArray = $this->getArrayCopy();
502
        unset($jsonArray['inputFilter']);
503
        unset($jsonArray['__initializer__']);
504
        unset($jsonArray['__cloner__']);
505
        unset($jsonArray['__isInitialized__']);
506
        unset($jsonArray['game']);
507
        unset($jsonArray['cards']);
508
        unset($jsonArray['distribution']);
509
        unset($jsonArray['createdAt']);
510
        unset($jsonArray['updatedAt']);
511
512
        return $jsonArray;
513
    }
514
515
    /**
516
     * Populate from an array.
517
     *
518
     * @param array $data
519
     */
520
    public function populate($data = array())
521
    {
522 View Code Duplication
        if (isset($data['title']) && $data['title'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
523
            $this->title = $data['title'];
524
        }
525
526 View Code Duplication
        if (isset($data['description']) && $data['description'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
527
            $this->description = $data['description'];
528
        }
529
530 View Code Duplication
        if (isset($data['jsonData']) && $data['jsonData'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
            $this->jsonData = $data['jsonData'];
532
        }
533
534
        if (isset($data['distribution']) && $data['distribution'] !== null) {
535
            $this->distribution = $data['distribution'];
536
        }
537
538 View Code Duplication
        if (isset($data['type']) && $data['type'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
539
            $this->type = $data['type'];
540
        }
541
542
        if (isset($data['family']) && $data['family'] !== null) {
543
            $this->family = $data['family'];
544
        }
545
546
        $this->points = (!empty($data['points']))?
547
        $this->points = $data['points']:
548
        0;
549
550
        if (!empty($data['image'])) {
551
            $this->image = $data['image'];
552
        }
553
554
        $this->availability = (!empty($data['availability']))?
555
        \DateTime::createFromFormat('d/m/Y H:i:s', $data['availability']):
556
        null;
557
    }
558
559
    public function setInputFilter(InputFilterInterface $inputFilter)
560
    {
561
        throw new \Exception("Not used");
562
    }
563
564 View Code Duplication
    public function getInputFilter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
565
    {
566
        if (!$this->inputFilter) {
567
            $inputFilter = new InputFilter();
568
            $factory     = new InputFactory();
569
570
            $inputFilter->add($factory->createInput(array(
571
                        'name'     => 'availability',
572
                        'required' => false,
573
                    )));
574
575
            $this->inputFilter = $inputFilter;
576
        }
577
578
        return $this->inputFilter;
579
    }
580
}
581