Code Duplication    Length = 56-59 lines in 2 locations

src/Service/PostVote.php 2 locations

@@ 398-456 (lines=59) @@
395
        return $arrayPosts;
396
    }
397
398
    public function toggleVote($user, $ipAddress, $post, $comment = null)
399
    {
400
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
401
        $postId = $post->getId();
402
        $commentId = ($comment !== null) ? $comment->getId() : null;
403
        $vote = null;
404
        $game = $post->getPostvote();
405
406
        if ($user) {
407
            if ($comment == null) {
408
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
409
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
410
            } else {
411
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
412
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
413
            }
414
        } else {
415
            if ($comment == null) {
416
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
417
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
418
            } else {
419
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
420
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
421
            }
422
        }
423
424
        if ($entryUser && $entryUser > 0) {
425
            $postvoteVoteMapper->remove($vote);
426
427
            return 0;
428
        } else {
429
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
430
            $vote->setPost($post);
431
            $vote->setIp($ipAddress);
432
            $vote->setNote(1);
433
            // If the vote is for a comment
434
            if ($comment != null) {
435
                $vote->setPostComment($comment);
436
                $vote->setPostvote($post->getPostvote());
437
            // else if the vote is for the post itself
438
            } else {
439
                $vote->setPostvote($post->getPostvote(), true);
440
            }
441
442
            if ($user) {
443
                $vote->setUser($user);
444
            }
445
446
            $postvoteVoteMapper->insert($vote);
447
        }
448
449
        $this->getEventManager()->trigger(
450
            __FUNCTION__ .'.post',
451
            $this,
452
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
453
        );
454
455
        return 1;
456
    }
457
458
    public function removeVote($user, $ipAddress, $post)
459
    {
@@ 493-548 (lines=56) @@
490
        return true;
491
    }
492
493
    public function addVote($user, $ipAddress, $post)
494
    {
495
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
496
        $postId = $post->getId();
497
        $commentId = ($comment !== null) ? $comment->getId() : null;
498
        $vote = null;
499
        $game = $post->getPostvote();
500
501
        if ($user) {
502
            if ($comment == null) {
503
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
504
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
505
            } else {
506
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
507
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
508
            }
509
        } else {
510
            if ($comment == null) {
511
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
512
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
513
            } else {
514
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
515
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
516
            }
517
        }
518
519
        if ($entryUser && $entryUser > 0) {
520
            return false;
521
        } else {
522
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
523
            $vote->setPost($post);
524
            $vote->setIp($ipAddress);
525
            $vote->setNote(1);
526
            // If the vote is for a comment
527
            if ($comment != null) {
528
                $vote->setPostComment($comment);
529
                $vote->setPostvote($post->getPostvote());
530
            // else if the vote is for the post itself
531
            } else {
532
                $vote->setPostvote($post->getPostvote(), true);
533
            }
534
            if ($user) {
535
                $vote->setUser($user);
536
            }
537
538
            $postvoteVoteMapper->insert($vote);
539
            $game = $post->getPostvote();
540
            $this->getEventManager()->trigger(
541
                __FUNCTION__ .'.post',
542
                $this,
543
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
544
            );
545
546
            return true;
547
        }
548
    }
549
550
    public function addComment($user, $ipAddress, $post, $message = '', $category = null)
551
    {