Completed
Push — master ( 56a418...8a3179 )
by greg
05:17 queued 02:13
created

PostVotePost::setPostElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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="PostVotePostElement", mappedBy="post", cascade={"persist","remove"})
73
     */
74
    private $postElements;
75
76
    /**
77
     * # of shares (FB, Twitter, mails...)
78
     * @ORM\Column(type="integer", nullable=false)
79
     */
80
    protected $shares = 0;
81
82
    /**
83
     * values :
84
     *          0 : draft
85
     *          1 : user confirmed
86
     *          2 : admin accepted
87
     *          8 : user rejected
88
     *          9 : admin rejected
89
     *
90
     * @ORM\Column(type="integer", nullable=false)
91
     */
92
    protected $status = 0;
93
    
94
    /**
95
     * @ORM\Column(type="boolean", nullable=true)
96
     */
97
    protected $pushed = 0;
98
99
    /**
100
     * @ORM\Column(name="created_at", type="datetime")
101
     */
102
    protected $createdAt;
103
104
    /**
105
     * @ORM\Column(name="updated_at", type="datetime")
106
     */
107
    protected $updatedAt;
108
109
    public function __construct()
110
    {
111
        $this->postElements = new ArrayCollection();
112
        $this->votes = new ArrayCollection();
113
        $this->comments = new ArrayCollection();
114
    }
115
116
    /**
117
     * @PrePersist
118
     */
119
    public function createChrono()
120
    {
121
        $this->createdAt = new \DateTime("now");
122
        $this->updatedAt = new \DateTime("now");
123
    }
124
125
    /**
126
     * @PreUpdate
127
     */
128
    public function updateChrono()
129
    {
130
        $this->updatedAt = new \DateTime("now");
131
    }
132
133
    /**
134
     * @return the unknown_type
135
     */
136
    public function getId()
137
    {
138
        return $this->id;
139
    }
140
141
    /**
142
     * @param unknown_type $id
143
     */
144
    public function setId($id)
145
    {
146
        $this->id = $id;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return the unknown_type
153
     */
154
    public function getPostvote()
155
    {
156
        return $this->postvote;
157
    }
158
159
    /**
160
     * @param unknown_type $postvote
161
     */
162
    public function setPostvote($postvote)
163
    {
164
        $postvote->addPost($this);
165
        $this->postvote = $postvote;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @return the unknown_type
172
     */
173
    public function getUser()
174
    {
175
        return $this->user;
176
    }
177
178
    /**
179
     * @param unknown_type $user
180
     */
181
    public function setUser($user)
182
    {
183
        $this->user = $user;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return boolean
190
     */
191
    public function getEntry()
192
    {
193
        return $this->entry;
194
    }
195
196
    /**
197
     * @param boolean $entry
198
     */
199
    public function setEntry($entry)
200
    {
201
        $this->entry = $entry;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Add an entry to the post.
208
     *
209
     * @param PostVotePostEntry $postElement
210
     *
211
     * @return void
212
     */
213
    public function addPostElement($postElement)
214
    {
215
        $postElement->setPost($this);
216
        $this->postElements[] = $postElement;
217
    }
218
219
    /**
220
     * @return ArrayCollection unknown_type
221
     */
222
    public function getPostElements()
223
    {
224
        return $this->postElements;
225
    }
226
227
    /**
228
     */
229
    public function setPostElements($postElements)
230
    {
231
        $this->postElements = $postElements;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Add an entry to the vote.
238
     *
239
     * @param PostVoteVote $vote
240
     *
241
     * @return void
242
     */
243
    public function addVote($vote)
244
    {
245
        $this->votes[] = $vote;
246
    }
247
248
    /**
249
     * @return the unknown_type
250
     */
251
    public function getVotes()
252
    {
253
        return $this->votes;
254
    }
255
256
    /**
257
     * @param unknown_type $votes
258
     */
259
    public function setVotes($votes)
260
    {
261
        $this->votes = $votes;
262
263
        return $this;
264
    }
265
266
    /**
267
     * Add an entry to the comment.
268
     *
269
     * @param PostVoteComment $comment
270
     *
271
     * @return void
272
     */
273
    public function addComment(PostVoteComment $comment)
274
    {
275
        $this->comments[] = $comment;
276
    }
277
278
    /**
279
     * @return the collection of comments. You can filter on it based on one or more categories
280
     */
281
    public function getComments($category = null)
282
    {
283
        if ($category != null) {
284
            if (!is_array($category)) $category = [$category];
285
            $criteria = Criteria::create()->where(Criteria::expr()->in("category", $category));
286
287
            return $this->getComments()->matching($criteria);
288
        }
289
        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...
290
    }
291
292
    /**
293
     * @param unknown_type $comments
294
     */
295
    public function setComments($comments)
296
    {
297
        $this->comments = $comments;
298
299
        return $this;
300
    }
301
302
    public function addComments(ArrayCollection $comments)
303
    {
304
        foreach ($comments as $comment) {
305
            $comment->setPost($this);
306
            $this->comments->add($comment);
307
        }
308
    }
309
310
    public function removeComments(\Doctrine\Common\Collections\Collection $comments)
311
    {
312
        foreach ($comments as $comment) {
313
            $comment->setPost(null);
314
            $this->comments->removeElement($comment);
315
        }
316
    }
317
318
    /**
319
     * @return integer unknown_type
320
     */
321
    public function getShares()
322
    {
323
        return $this->shares;
324
    }
325
326
    /**
327
     * @param unknown_shares $shares
328
     */
329
    public function setShares($shares)
330
    {
331
        $this->shares = $shares;
0 ignored issues
show
Documentation Bug introduced by
It seems like $shares of type object<PlaygroundGame\Entity\unknown_shares> is incompatible with the declared type integer of property $shares.

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...
332
333
        return $this;
334
    }
335
336
    /**
337
     * @return integer unknown_type
338
     */
339
    public function getStatus()
340
    {
341
        return $this->status;
342
    }
343
344
    /**
345
     * @param unknown_status $status
346
     */
347
    public function setStatus($status)
348
    {
349
        $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...
350
351
        return $this;
352
    }
353
    
354
    /**
355
     * @return integer
356
     */
357
    public function isPushed()
358
    {
359
        return $this->pushed;
360
    }
361
        
362
    /**
363
     * @param bool $pushed
364
     * @return PostVotePost
365
     */
366
    public function setPushed($pushed)
367
    {
368
        $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...
369
        return $this;
370
    }
371
372
    /**
373
     * @return the unknown_type
374
     */
375
    public function getCreatedAt()
376
    {
377
        return $this->createdAt;
378
    }
379
380
    /**
381
     * @param unknown_type $createdAt
382
     */
383
    public function setCreatedAt($createdAt)
384
    {
385
        $this->createdAt = $createdAt;
386
387
        return $this;
388
    }
389
390
    /**
391
     * @return the unknown_type
392
     */
393
    public function getUpdatedAt()
394
    {
395
        return $this->updatedAt;
396
    }
397
398
    /**
399
     * @param unknown_type $updatedAt
400
     */
401
    public function setUpdatedAt($updatedAt)
402
    {
403
        $this->updatedAt = $updatedAt;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Convert the object to an array.
410
     *
411
     * @return array
412
     */
413
    public function getArrayCopy()
414
    {
415
        $obj_vars = get_object_vars($this);
416
417
        if (isset($obj_vars['postElements'])) {
418
            $obj_vars['postElements'] = $this->getPostElements()->toArray();
419
        }
420
421
        return $obj_vars;
422
    }
423
424
    /**
425
     * Convert the object to json.
426
     *
427
     * @return array
428
     */
429
    public function jsonSerialize()
430
    {
431
        return $this->getArrayCopy();
432
    }
433
434
    /**
435
     * Populate from an array.
436
     *
437
     * @param array $data
438
     */
439
    public function populate($data = array())
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
440
    {
441
    }
442
443
    public function setInputFilter(InputFilterInterface $inputFilter)
444
    {
445
        throw new \Exception("Not used");
446
    }
447
448
    public function getInputFilter()
449
    {
450
        if (!$this->inputFilter) {
451
            $inputFilter = new InputFilter();
452
            $factory = new InputFactory();
453
454
            $inputFilter->add(
455
                $factory->createInput(
456
                    array(
457
                        'name' => 'id',
458
                        'required' => true,
459
                        'filters' => array(array('name' => 'Int'))
460
                    )
461
                )
462
            );
463
464
            $this->inputFilter = $inputFilter;
465
        }
466
467
        return $this->inputFilter;
468
    }
469
470
    public function setTranslatableLocale($locale)
471
    {
472
        $this->locale = $locale;
473
    }
474
}
475