Code Duplication    Length = 56-59 lines in 2 locations

src/Service/PostVote.php 2 locations

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