Completed
Pull Request — master (#139)
by olivier
02:16
created

ClaimedBadgeController::acceptAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 56
Code Lines 35

Duplication

Lines 8
Ratio 14.29 %

Importance

Changes 0
Metric Value
dl 8
loc 56
rs 9.0544
c 0
b 0
f 0
cc 4
eloc 35
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Badger\Bundle\GameBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * @author  Adrien Pétremann <[email protected]>
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
class ClaimedBadgeController extends Controller
14
{
15
    /**
16
     * Lists all ClaimedBadge entities.
17
     *
18
     * @return Response
19
     */
20
    public function indexAction()
21
    {
22
        $claimedBadges = $this->get('badger.game.repository.claimed_badge')
23
            ->findAll();
24
25
        return $this->render('@Game/claimed-badges/index.html.twig', [
26
            'claimedBadges' => $claimedBadges
27
        ]);
28
    }
29
30
    /**
31
     * Reject a claimed badge by removing it from the database.
32
     *
33
     * @param $id
34
     *
35
     * @return RedirectResponse
36
     */
37
    public function rejectAction($id)
38
    {
39
        $claimedBadge = $this->get('badger.game.repository.claimed_badge')
40
            ->find($id);
41
42
        if (null === $claimedBadge) {
43
            throw new \LogicException(sprintf('No ClaimedBadge entity with id %s', $id));
44
        }
45
46
        $badgeTitle = $claimedBadge->getBadge()->getTitle();
47
        $username = $claimedBadge->getUser()->getUsername();
48
49
        $claimedBadgeRemover = $this->get('badger.game.remover.claimed_badge');
50
        $claimedBadgeRemover->remove($claimedBadge);
51
52
        $this->addFlash('notice', sprintf(
53
            'Successfully rejected the badge "%s" for %s!',
54
            $badgeTitle,
55
            $username
56
        ));
57
58
        return $this->redirectToRoute('admin_claimed_badge_index');
59
    }
60
61
    /**
62
     * Accept a claimed badge by creating a new unlocked badge.
63
     *
64
     * @param $id
65
     *
66
     * @return RedirectResponse
67
     */
68
    public function acceptAction($id)
69
    {
70
        $claimedBadge = $this->get('badger.game.repository.claimed_badge')
71
            ->find($id);
72
73
        if (null === $claimedBadge) {
74
            throw new \LogicException(sprintf('No ClaimedBadge entity with id %s', $id));
75
        }
76
77
        $user = $claimedBadge->getUser();
78
        $badge = $claimedBadge->getBadge();
79
80
        $isUnlocked = $this->get('badger.game.repository.unlocked_badge')
81
            ->findOneBy([
82
                'user' => $user,
83
                'badge' => $badge
84
            ]);
85
86 View Code Duplication
        if ($isUnlocked) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            $this->addFlash(
88
                'error',
89
                sprintf(
90
                    '%s already has the badge "%s"',
91
                    $user->getUsername(),
92
                    $badge->getTitle()
93
                )
94
            );
95
96
            return $this->redirectToRoute('admin_claimed_badge_index');
97
        }
98
99
        $validator = $this->get('validator');
100
101
        $unlockedBadgeFactory = $this->get('badger.game.unlocked_badge.factory');
102
        $unlockedBadge = $unlockedBadgeFactory->create($user, $badge);
103
104
        $errors = $validator->validate($unlockedBadge);
105
106
        if (0 === count($errors)) {
107
            $unlockedBadgeSaver = $this->get('badger.game.saver.unlocked_badge');
108
            $claimedBadgeRemover = $this->get('badger.game.remover.claimed_badge');
109
110
            $unlockedBadgeSaver->save($unlockedBadge);
111
            $claimedBadgeRemover->remove($claimedBadge);
112
113
            $this->addFlash('notice', sprintf(
114
                '%s successfully received the badge "%s"!',
115
                $user->getUsername(),
116
                $badge->getTitle()
117
            ));
118
        } else {
119
            $this->addFlash('error', (string) $errors);
120
        }
121
122
        return $this->redirectToRoute('admin_claimed_badge_index');
123
    }
124
}
125