PostVotePost::getShares()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\ORM\Mapping as ORM;
8
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
9
use Doctrine\ORM\Mapping\PrePersist;
10
use Doctrine\ORM\Mapping\PreUpdate;
11
use Gedmo\Mapping\Annotation as Gedmo;
12
use Gedmo\Translatable\Translatable;
13
use Zend\InputFilter\InputFilter;
14
use Zend\InputFilter\Factory as InputFactory;
15
use Zend\InputFilter\InputFilterAwareInterface;
16
use Zend\InputFilter\InputFilterInterface;
17
18
/**
19
 * @ORM\Entity @HasLifecycleCallbacks
20
 * @ORM\Table(name="game_postvote_post")
21
 * @Gedmo\TranslationEntity(class="PlaygroundGame\Entity\PostVotePostTranslation")
22
 */
23
class PostVotePost implements InputFilterAwareInterface, Translatable, \JsonSerializable
24
{
25
    /**
26
     * @Gedmo\Locale
27
     * Used locale to override Translation listener`s locale
28
     * this is not a mapped field of entity metadata, just a simple property
29
     */
30
    protected $locale;
31
32
    protected $inputFilter;
33
34
    /**
35
     * @ORM\Id
36
     * @ORM\Column(type="integer");
37
     * @ORM\GeneratedValue(strategy="AUTO")
38
     */
39
    protected $id;
40
41
    /**
42
     * @ORM\ManyToOne(targetEntity="PostVote", inversedBy="posts")
43
     * @ORM\JoinColumn(name="postvote_id", referencedColumnName="id", onDelete="CASCADE")
44
     */
45
    protected $postvote;
46
47
    /**
48
     * @ORM\ManyToOne(targetEntity="PlaygroundUser\Entity\User")
49
     * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE")
50
     **/
51
    protected $user;
52
53
    /**
54
     * @ORM\ManyToOne(targetEntity="Entry")
55
     * @ORM\JoinColumn(name="entry_id",referencedColumnName="id",onDelete="CASCADE")
56
     **/
57
    protected $entry;
58
59
    /**
60
     * @ORM\OneToMany(targetEntity="PostVoteVote", mappedBy="post")
61
     * @ORM\OrderBy({"createdAt"="DESC"})
62
     */
63
    private $votes;
64
65
    /**
66
     * @ORM\OneToMany(targetEntity="PostVoteComment", mappedBy="post")
67
     * @ORM\OrderBy({"createdAt"="DESC"})
68
     */
69
    private $comments;
70
71
    /**
72
     * @ORM\OneToMany(targetEntity="PostVoteView", mappedBy="post")
73
     * @ORM\OrderBy({"createdAt"="DESC"})
74
     */
75
    private $views;
76
77
    /**
78
     * @ORM\OneToMany(targetEntity="PostVotePostElement", mappedBy="post", cascade={"persist","remove"})
79
     */
80
    private $postElements;
81
82
    /**
83
     * # of shares (FB, Twitter, mails...)
84
     * @ORM\Column(type="integer", nullable=false)
85
     */
86
87
    /**
88
     * @ORM\OneToMany(targetEntity="PostVoteShare", mappedBy="post")
89
     * @ORM\OrderBy({"createdAt"="DESC"})
90
     */
91
    protected $shares;
92
93
    /**
94
     * values :
95
     *          0 : draft
96
     *          1 : user confirmed
97
     *          2 : admin accepted
98
     *          8 : user rejected
99
     *          9 : admin rejected
100
     *
101
     * @ORM\Column(type="integer", nullable=false)
102
     */
103
    protected $status = 0;
104
    
105
    /**
106
     * @ORM\Column(type="boolean", nullable=true)
107
     */
108
    protected $pushed = 0;
109
110
    /**
111
     * @ORM\Column(name="created_at", type="datetime")
112
     */
113
    protected $createdAt;
114
115
    /**
116
     * @ORM\Column(name="updated_at", type="datetime")
117
     */
118
    protected $updatedAt;
119
120
    public function __construct()
121
    {
122
        $this->postElements = new ArrayCollection();
123
        $this->votes = new ArrayCollection();
124
        $this->comments = new ArrayCollection();
125
        $this->shares = new ArrayCollection();
126
        $this->views = new ArrayCollection();
127
    }
128
129
    /**
130
     * @PrePersist
131
     */
132
    public function createChrono()
133
    {
134
        $this->createdAt = new \DateTime("now");
135
        $this->updatedAt = new \DateTime("now");
136
    }
137
138
    /**
139
     * @PreUpdate
140
     */
141
    public function updateChrono()
142
    {
143
        $this->updatedAt = new \DateTime("now");
144
    }
145
146
    /**
147
     * @return the unknown_type
148
     */
149
    public function getId()
150
    {
151
        return $this->id;
152
    }
153
154
    /**
155
     * @param unknown_type $id
156
     */
157
    public function setId($id)
158
    {
159
        $this->id = $id;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return the unknown_type
166
     */
167
    public function getPostvote()
168
    {
169
        return $this->postvote;
170
    }
171
172
    /**
173
     * @param unknown_type $postvote
174
     */
175
    public function setPostvote($postvote)
176
    {
177
        $postvote->addPost($this);
178
        $this->postvote = $postvote;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return the unknown_type
185
     */
186
    public function getUser()
187
    {
188
        return $this->user;
189
    }
190
191
    /**
192
     * @param unknown_type $user
193
     */
194
    public function setUser($user)
195
    {
196
        $this->user = $user;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return boolean
203
     */
204
    public function getEntry()
205
    {
206
        return $this->entry;
207
    }
208
209
    /**
210
     * @param boolean $entry
211
     */
212
    public function setEntry($entry)
213
    {
214
        $this->entry = $entry;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Add an entry to the post.
221
     *
222
     * @param PostVotePostEntry $postElement
223
     *
224
     * @return void
225
     */
226
    public function addPostElement($postElement)
227
    {
228
        $postElement->setPost($this);
229
        $this->postElements[] = $postElement;
230
    }
231
232
    /**
233
     * @return ArrayCollection unknown_type
234
     */
235
    public function getPostElements()
236
    {
237
        return $this->postElements;
238
    }
239
240
    /**
241
     */
242
    public function setPostElements($postElements)
243
    {
244
        $this->postElements = $postElements;
245
246
        return $this;
247
    }
248
249
    /**
250
     * Add an entry to the vote.
251
     *
252
     * @param PostVoteVote $vote
253
     *
254
     * @return void
255
     */
256
    public function addVote($vote)
257
    {
258
        $this->votes[] = $vote;
259
    }
260
261
    /**
262
     * @return the unknown_type
263
     */
264
    public function getVotes()
265
    {
266
        return $this->votes;
267
    }
268
269
    /**
270
     * @param unknown_type $votes
271
     */
272
    public function setVotes($votes)
273
    {
274
        $this->votes = $votes;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Add an entry to the comment.
281
     *
282
     * @param PostVoteComment $comment
283
     *
284
     * @return void
285
     */
286
    public function addComment(PostVoteComment $comment)
287
    {
288
        $this->comments[] = $comment;
289
    }
290
291
    /**
292
     * @return the collection of comments. You can filter on it based on one or more categories
293
     */
294
    public function getComments($category = null)
295
    {
296
        if ($category != null) {
297
            if (!is_array($category)) {
298
                $category = [$category];
299
            }
300
            $criteria = Criteria::create()->where(Criteria::expr()->in("category", $category));
301
302
            return $this->getComments()->matching($criteria);
303
        }
304
        return $this->comments;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->comments; (Doctrine\Common\Collecti...ame\Entity\unknown_type) is incompatible with the return type documented by PlaygroundGame\Entity\PostVotePost::getComments of type PlaygroundGame\Entity\the.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
305
    }
306
307
    /**
308
     * @param unknown_type $comments
309
     */
310
    public function setComments($comments)
311
    {
312
        $this->comments = $comments;
313
314
        return $this;
315
    }
316
317
    public function addComments(ArrayCollection $comments)
318
    {
319
        foreach ($comments as $comment) {
320
            $comment->setPost($this);
321
            $this->comments->add($comment);
322
        }
323
    }
324
325
    public function removeComments(\Doctrine\Common\Collections\Collection $comments)
326
    {
327
        foreach ($comments as $comment) {
328
            $comment->setPost(null);
329
            $this->comments->removeElement($comment);
330
        }
331
    }
332
333
    /**
334
     * @return integer unknown_type
335
     */
336
    public function getShares()
337
    {
338
        return $this->shares;
339
    }
340
341
    /**
342
     * @param unknown_shares $shares
343
     */
344
    public function setShares($shares)
345
    {
346
        $this->shares = $shares;
347
348
        return $this;
349
    }
350
351
    /**
352
     * Add an entry to the share.
353
     *
354
     * @param PostVoteShare $share
355
     *
356
     * @return void
357
     */
358
    public function addShare(PostVoteShare $share)
359
    {
360
        $this->shares[] = $share;
361
    }
362
363
    public function addShares(ArrayCollection $shares)
364
    {
365
        foreach ($shares as $share) {
366
            $share->setPost($this);
367
            $this->shares->add($share);
368
        }
369
    }
370
371
    public function removeShares(\Doctrine\Common\Collections\Collection $shares)
372
    {
373
        foreach ($shares as $share) {
374
            $share->setPost(null);
375
            $this->shares->removeElement($share);
376
        }
377
    }
378
379
    /**
380
     * Add an entry to the view.
381
     *
382
     * @param PostVoteView $view
383
     *
384
     * @return void
385
     */
386
    public function addView(PostVoteView $view)
387
    {
388
        $this->views[] = $view;
389
    }
390
391
    /**
392
     * @return the collection of views. You can filter on it based on one or more categories
393
     */
394
    public function getViews()
395
    {
396
        return $this->views;
397
    }
398
399
    /**
400
     * @param unknown_type $views
401
     */
402
    public function setViews($views)
403
    {
404
        $this->views = $views;
405
406
        return $this;
407
    }
408
409
    public function addViews(ArrayCollection $views)
410
    {
411
        foreach ($views as $view) {
412
            $view->setPost($this);
413
            $this->views->add($view);
414
        }
415
    }
416
417
    public function removeViews(\Doctrine\Common\Collections\Collection $views)
418
    {
419
        foreach ($views as $view) {
420
            $view->setPost(null);
421
            $this->views->removeElement($view);
422
        }
423
    }
424
    
425
    /**
426
     * @return integer unknown_type
427
     */
428
    public function getStatus()
429
    {
430
        return $this->status;
431
    }
432
433
    /**
434
     * @param unknown_status $status
435
     */
436
    public function setStatus($status)
437
    {
438
        $this->status = $status;
0 ignored issues
show
Documentation Bug introduced by
It seems like $status of type object<PlaygroundGame\Entity\unknown_status> is incompatible with the declared type integer of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
439
440
        return $this;
441
    }
442
    
443
    /**
444
     * @return integer
445
     */
446
    public function isPushed()
447
    {
448
        return $this->pushed;
449
    }
450
        
451
    /**
452
     * @param bool $pushed
453
     * @return PostVotePost
454
     */
455
    public function setPushed($pushed)
456
    {
457
        $this->pushed = (boolean)$pushed;
0 ignored issues
show
Documentation Bug introduced by
The property $pushed was declared of type integer, but (bool) $pushed is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
458
        return $this;
459
    }
460
461
    /**
462
     * @return the unknown_type
463
     */
464
    public function getCreatedAt()
465
    {
466
        return $this->createdAt;
467
    }
468
469
    /**
470
     * @param unknown_type $createdAt
471
     */
472
    public function setCreatedAt($createdAt)
473
    {
474
        $this->createdAt = $createdAt;
475
476
        return $this;
477
    }
478
479
    /**
480
     * @return the unknown_type
481
     */
482
    public function getUpdatedAt()
483
    {
484
        return $this->updatedAt;
485
    }
486
487
    /**
488
     * @param unknown_type $updatedAt
489
     */
490
    public function setUpdatedAt($updatedAt)
491
    {
492
        $this->updatedAt = $updatedAt;
493
494
        return $this;
495
    }
496
497
    /**
498
     * @return ArrayCollection unknown_type
499
     */
500
    public function getPostElementsAsArray()
501
    {
502
        $arElements = [];
503
504
        foreach ($this->postElements as $k => $elt) {
505
            $arElements[$elt->getName()] = $elt->getValue();
506
        }
507
508
        return $arElements;
509
    }
510
511
    /**
512
     * Convert the object to an array.
513
     *
514
     * @return array
515
     */
516
    public function getArrayCopy()
517
    {
518
        $obj_vars = get_object_vars($this);
519
520
        if (isset($obj_vars['postElements'])) {
521
            $obj_vars['postElements'] = $this->getPostElements()->toArray();
522
        }
523
524
        return $obj_vars;
525
    }
526
527
    /**
528
     * Convert the object to json.
529
     *
530
     * @return array
531
     */
532
    public function jsonSerialize()
533
    {
534
        return $this->getArrayCopy();
535
    }
536
537
    /**
538
     * Populate from an array.
539
     *
540
     * @param array $data
541
     */
542
    public function populate($data = array())
543
    {
544
    }
545
546
    public function setInputFilter(InputFilterInterface $inputFilter)
547
    {
548
        throw new \Exception("Not used");
549
    }
550
551
    public function getInputFilter()
552
    {
553
        if (!$this->inputFilter) {
554
            $inputFilter = new InputFilter();
555
            $factory = new InputFactory();
556
557
            $inputFilter->add(
558
                $factory->createInput(
559
                    array(
560
                        'name' => 'id',
561
                        'required' => true,
562
                        'filters' => array(array('name' => 'Int'))
563
                    )
564
                )
565
            );
566
567
            $this->inputFilter = $inputFilter;
568
        }
569
570
        return $this->inputFilter;
571
    }
572
573
    public function setTranslatableLocale($locale)
574
    {
575
        $this->locale = $locale;
576
    }
577
}
578