Completed
Pull Request — master (#145)
by Adrien
02:37
created

BadgeCompletionController::giveAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 64
Code Lines 39

Duplication

Lines 51
Ratio 79.69 %

Importance

Changes 0
Metric Value
dl 51
loc 64
rs 8.6346
c 0
b 0
f 0
cc 5
eloc 39
nc 5
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\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
11
12
/**
13
 * @author  Adrien Pétremann <[email protected]>
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class BadgeCompletionController extends Controller
17
{
18
    /**
19
     * Lists all pending badge completions.
20
     *
21
     * @return Response
22
     */
23 View Code Duplication
    public function indexAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
24
    {
25
        $pendingCompletions = $this->get('badger.game.repository.badge_completion')
26
            ->findBy(['pending' => 1]);
27
28
        return $this->render('@Game/claimed-badges/index.html.twig', [
29
            'pendingCompletions' => $pendingCompletions
30
        ]);
31
    }
32
33
    /**
34
     * Reject a badge completion by removing it from the database.
35
     *
36
     * @param string $id
37
     *
38
     * @return RedirectResponse
39
     */
40 View Code Duplication
    public function rejectAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        $pendingCompletion = $this->get('badger.game.repository.badge_completion')
43
            ->findOneBy(['id' => $id, 'pending' => 1]);
44
45
        if (null === $pendingCompletion) {
46
            throw new NotFoundHttpException(sprintf('No pending BadgeCompletion entity with id %s', $id));
47
        }
48
49
        $badgeTitle = $pendingCompletion->getBadge()->getTitle();
50
        $username = $pendingCompletion->getUser()->getUsername();
51
52
        $badgeCompletionRemover = $this->get('badger.game.remover.badge_completion');
53
        $badgeCompletionRemover->remove($pendingCompletion);
54
55
        $this->addFlash('notice', sprintf(
56
            'Successfully rejected the claimed badge "%s" for %s!',
57
            $badgeTitle,
58
            $username
59
        ));
60
61
        return $this->redirectToRoute('admin_claimed_badge_index');
62
    }
63
64
    /**
65
     * Accept a badge completion.
66
     *
67
     * @param string $id
68
     *
69
     * @return RedirectResponse
70
     */
71 View Code Duplication
    public function acceptAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
72
    {
73
        $pendingCompletion = $this->get('badger.game.repository.badge_completion')
74
            ->findOneBy(['id' => $id, 'pending' => 1]);
75
76
        if (null === $pendingCompletion) {
77
            throw new NotFoundHttpException(sprintf('No pending BadgeCompletion entity with id %s', $id));
78
        }
79
80
        $user = $pendingCompletion->getUser();
81
        $badge = $pendingCompletion->getBadge();
82
83
        $pendingCompletion->setPending(false);
84
        $pendingCompletion->setCompletionDate(new \DateTime());
85
86
        $errors = $this->get('validator')->validate($pendingCompletion);
87
88
        if (0 === count($errors)) {
89
            $this->get('badger.game.saver.badge_completion')->save($pendingCompletion);
90
91
            $this->addFlash('notice', sprintf(
92
                '%s successfully unlocked the badge "%s"!',
93
                $user->getUsername(),
94
                $badge->getTitle()
95
            ));
96
        } else {
97
            $this->addFlash('error', (string) $errors);
98
        }
99
100
        return $this->redirectToRoute('admin_claimed_badge_index');
101
    }
102
103
    /**
104
     * Give a badge to a user directly by creating a completed badge completion.
105
     *
106
     * @param Request $request
107
     *
108
     * @return RedirectResponse
109
     */
110
    public function giveAction(Request $request)
111
    {
112
        $badgeRepository = $this->get('badger.game.repository.badge');
113
114 View Code Duplication
        if ('POST' === $request->getMethod()) {
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...
115
            $validator = $this->get('validator');
116
117
            $user = $this->get('fos_user.user_manager')->findUserByUsername($request->get('user'));
118
            $badge = $badgeRepository->find($request->get('badge'));
119
120
            $token = new UsernamePasswordToken($user, 'none', 'none', $user->getRoles());
121
            $isUnlockable = $this->get('security.access.decision_manager')->decide($token, ['view'], $badge);
122
123
            if (!$isUnlockable) {
124
                $this->addFlash('error', sprintf('%s does not have access to badge "%s"',
125
                    $user->getUsername(),
126
                    $badge->getTitle()
127
                ));
128
129
                return $this->redirectToRoute('admin_unlocked_badge_give');
130
            }
131
132
            $isUnlocked = $this->get('badger.game.repository.unlocked_badge')
133
                ->findOneBy([
134
                    'user' => $user,
135
                    'badge' => $badge
136
                ]);
137
138
            if ($isUnlocked) {
139
                $this->addFlash('error', sprintf('%s already has the badge "%s"',
140
                    $user->getUsername(),
141
                    $badge->getTitle()
142
                ));
143
144
                return $this->redirectToRoute('admin_unlocked_badge_give');
145
            }
146
147
            $unlockedBadgeFactory = $this->get('badger.game.unlocked_badge.factory');
148
            $unlockedBadge = $unlockedBadgeFactory->create($user, $badge);
149
150
            $errors = $validator->validate($unlockedBadge);
151
152
            if (0 === count($errors)) {
153
                $unlockedBadgeSaver = $this->get('badger.game.saver.unlocked_badge');
154
                $unlockedBadgeSaver->save($unlockedBadge);
155
156
                $this->addFlash('notice', sprintf(
157
                    '%s successfully received the badge "%s"!',
158
                    $user->getUsername(),
159
                    $badge->getTitle()
160
                ));
161
            } else {
162
                $this->addFlash('error', (string) $errors);
163
            }
164
        }
165
166
        $badges = $badgeRepository->findAll();
167
        $usernames = $this->get('badger.user.repository.user')->getAllUsernames();
168
169
        return $this->render('@Game/unlocked-badges/give.html.twig', [
170
            'badges' => json_encode($badges),
171
            'users'  => json_encode($usernames)
172
        ]);
173
    }
174
175
    /**
176
     * Remove a badge from a user by removing the badge completion.
177
     *
178
     * @return RedirectResponse
179
     */
180
    public function removeAction()
181
    {
182
183
    }
184
}
185