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

ClaimedBadgeController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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