Code Duplication    Length = 56-59 lines in 2 locations

src/Service/PostVote.php 2 locations

@@ 450-508 (lines=59) @@
447
        return $arrayPosts;
448
    }
449
450
    public function toggleVote($user, $ipAddress, $post, $comment = null)
451
    {
452
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
453
        $postId = $post->getId();
454
        $commentId = ($comment !== null) ? $comment->getId() : null;
455
        $vote = null;
456
        $game = $post->getPostvote();
457
458
        if ($user) {
459
            if ($comment == null) {
460
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
461
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
462
            } else {
463
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
464
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
465
            }
466
        } else {
467
            if ($comment == null) {
468
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
469
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
470
            } else {
471
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
472
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
473
            }
474
        }
475
476
        if ($entryUser && $entryUser > 0) {
477
            $postvoteVoteMapper->remove($vote);
478
479
            return 0;
480
        } else {
481
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
482
            $vote->setPost($post);
483
            $vote->setIp($ipAddress);
484
            $vote->setNote(1);
485
            // If the vote is for a comment
486
            if ($comment != null) {
487
                $vote->setPostComment($comment);
488
                $vote->setPostvote($post->getPostvote());
489
            // else if the vote is for the post itself
490
            } else {
491
                $vote->setPostvote($post->getPostvote(), true);
492
            }
493
494
            if ($user) {
495
                $vote->setUser($user);
496
            }
497
498
            $postvoteVoteMapper->insert($vote);
499
        }
500
501
        $this->getEventManager()->trigger(
502
            __FUNCTION__ .'.post',
503
            $this,
504
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
505
        );
506
507
        return 1;
508
    }
509
510
    public function removeVote($user, $ipAddress, $post)
511
    {
@@ 545-600 (lines=56) @@
542
        return true;
543
    }
544
545
    public function addVote($user, $ipAddress, $post)
546
    {
547
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
548
        $postId = $post->getId();
549
        $commentId = ($comment !== null) ? $comment->getId() : null;
550
        $vote = null;
551
        $game = $post->getPostvote();
552
553
        if ($user) {
554
            if ($comment == null) {
555
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
556
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
557
            } else {
558
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
559
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
560
            }
561
        } else {
562
            if ($comment == null) {
563
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
564
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
565
            } else {
566
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
567
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
568
            }
569
        }
570
571
        if ($entryUser && $entryUser > 0) {
572
            return false;
573
        } else {
574
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
575
            $vote->setPost($post);
576
            $vote->setIp($ipAddress);
577
            $vote->setNote(1);
578
            // If the vote is for a comment
579
            if ($comment != null) {
580
                $vote->setPostComment($comment);
581
                $vote->setPostvote($post->getPostvote());
582
            // else if the vote is for the post itself
583
            } else {
584
                $vote->setPostvote($post->getPostvote(), true);
585
            }
586
            if ($user) {
587
                $vote->setUser($user);
588
            }
589
590
            $postvoteVoteMapper->insert($vote);
591
            $game = $post->getPostvote();
592
            $this->getEventManager()->trigger(
593
                __FUNCTION__ .'.post',
594
                $this,
595
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
596
            );
597
598
            return true;
599
        }
600
    }
601
602
    public function addComment($user, $ipAddress, $post, $message = '', $category = null)
603
    {