Code Duplication    Length = 57-58 lines in 2 locations

src/Service/PostVote.php 2 locations

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