Code Duplication    Length = 56-59 lines in 2 locations

src/Service/PostVote.php 2 locations

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