Completed
Push — master ( da1994...a846d4 )
by Adrien
11s
created

BadgeCompletionController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
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\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 Response
109
     */
110
    public function giveAction(Request $request)
111
    {
112
        $badgeRepository = $this->get('badger.game.repository.badge');
113
114
        if ('POST' === $request->getMethod()) {
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 View Code Duplication
            if (!$isUnlockable) {
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...
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
            $badgeCompletion = $this->get('badger.game.repository.badge_completion')
133
                ->findOneBy([
134
                    'user' => $user,
135
                    'badge' => $badge
136
                ]);
137
138
            if (null === $badgeCompletion) {
139
                $badgeCompletion = $this->get('badger.game.badge_completion.factory')
140
                    ->create($user, $badge);
141
            }
142
143 View Code Duplication
            if (!$badgeCompletion->isPending()) {
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...
144
                $this->addFlash('error', sprintf('%s already has the badge "%s"',
145
                    $user->getUsername(),
146
                    $badge->getTitle()
147
                ));
148
149
                return $this->redirectToRoute('admin_unlocked_badge_give');
150
            }
151
152
            $badgeCompletion->setPending(false);
153
            $badgeCompletion->setCompletionDate(new \DateTime());
154
155
            $errors = $validator->validate($badgeCompletion);
156
157
            if (0 === count($errors)) {
158
                $this->get('badger.game.saver.badge_completion')->save($badgeCompletion);
159
160
                $this->addFlash('notice', sprintf(
161
                    '%s successfully received the badge "%s"!',
162
                    $user->getUsername(),
163
                    $badge->getTitle()
164
                ));
165
            } else {
166
                $this->addFlash('error', (string) $errors);
167
            }
168
        }
169
170
        $badges = $badgeRepository->findAll();
171
        $usernames = $this->get('badger.user.repository.user')->getAllUsernames();
172
173
        return $this->render('@Game/unlocked-badges/give.html.twig', [
174
            'badges' => json_encode($badges),
175
            'users'  => json_encode($usernames)
176
        ]);
177
    }
178
179
    /**
180
     * Remove a badge from a user by removing the badge completion.
181
     *
182
     * @param Request $request
183
     *
184
     * @return Response
185
     */
186
    public function removeAction(Request $request)
187
    {
188
        $badgeRepository = $this->get('badger.game.repository.badge');
189
190
        if ('POST' === $request->getMethod()) {
191
            $user = $this->container->get('fos_user.user_manager')->findUserByUsername($request->get('user'));
192
            $badge = $badgeRepository->findOneById($request->get('badge'));
193
194
            $badgeCompletion = $this->get('badger.game.repository.badge_completion')
195
                ->findOneBy([
196
                    'user' => $user,
197
                    'badge' => $badge,
198
                    'pending' => false
199
                ]);
200
201 View Code Duplication
            if (null === $badgeCompletion) {
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...
202
                $this->addFlash('error', sprintf('%s has no badge named "%s"', $user->getUsername(), $badge->getTitle()));
203
204
                return $this->redirectToRoute('admin_unlocked_badge_remove');
205
            }
206
207
            $badgeCompletionRemover = $this->get('badger.game.remover.badge_completion');
208
            $badgeCompletionRemover->remove($badgeCompletion);
209
210
            $this->addFlash('notice', sprintf(
211
                'Successfully removed the badge "%s" to the user "%s"!',
212
                $badge->getTitle(),
213
                $user->getUsername()
214
            ));
215
        }
216
217
        $badges = $badgeRepository->findAll();
218
        $usernames = $this->get('badger.user.repository.user')->getAllUsernames();
219
220
        return $this->render('@Game/unlocked-badges/remove.html.twig', [
221
            'badges' => json_encode($badges),
222
            'users'  => json_encode($usernames)
223
        ]);
224
    }
225
}
226