AdventureStepCompletionController::rejectAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
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
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
/**
11
 * @author  Adrien Pétremann <[email protected]>
12
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
13
 */
14
class AdventureStepCompletionController extends Controller
15
{
16
    /**
17
     * Lists all pending adventure step completions.
18
     *
19
     * @return Response
20
     */
21 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...
22
    {
23
        $pendingCompletions = $this->get('badger.game.repository.adventure_step_completion')
24
            ->findBy(['pending' => 1]);
25
26
        return $this->render('@Game/claimed-adventure-steps/index.html.twig', [
27
            'pendingCompletions' => $pendingCompletions
28
        ]);
29
    }
30
31
    /**
32
     * Reject an adventure step completion by removing it from the database.
33
     *
34
     * @param string $id
35
     *
36
     * @return RedirectResponse
37
     */
38 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...
39
    {
40
        $pendingCompletion = $this->get('badger.game.repository.adventure_step_completion')
41
            ->findOneBy(['id' => $id, 'pending' => 1]);
42
43
        if (null === $pendingCompletion) {
44
            throw new NotFoundHttpException(sprintf('No pending AdventureStepCompletion entity with id %s', $id));
45
        }
46
47
        $stepTitle = $pendingCompletion->getAdventureStep()->getTitle();
48
        $username = $pendingCompletion->getUser()->getUsername();
49
50
        $questCompletionRemover = $this->get('badger.game.remover.adventure_step_completion');
51
        $questCompletionRemover->remove($pendingCompletion);
52
53
        $this->addFlash('notice', sprintf(
54
            'Successfully rejected the claimed adventure step "%s" for %s!',
55
            $stepTitle,
56
            $username
57
        ));
58
59
        return $this->redirectToRoute('admin_claimed_adventure_step_index');
60
    }
61
62
    /**
63
     * Accept an adventure step completion.
64
     *
65
     * @param string $id
66
     *
67
     * @return RedirectResponse
68
     */
69
    public function acceptAction($id)
70
    {
71
        $pendingCompletion = $this->get('badger.game.repository.adventure_step_completion')
72
            ->findOneBy(['id' => $id, 'pending' => 1]);
73
74
        if (null === $pendingCompletion) {
75
            throw new NotFoundHttpException(sprintf('No pending AdventureStepCompletion entity with id %s', $id));
76
        }
77
78
        $user = $pendingCompletion->getUser();
79
        $step = $pendingCompletion->getAdventureStep();
80
        $adventure = $step->getAdventure();
81
82
        $pendingCompletion->setPending(false);
83
        $pendingCompletion->setCompletionDate(new \DateTime());
84
85
        $errors = $this->get('validator')->validate($pendingCompletion);
86
87
        if (0 === count($errors)) {
88
            $stepBadge = $step->getBadge();
89
            if (null !== $stepBadge) {
90
                $this->get('badger.game.unlocker.badge')->unlockBadge($user, $stepBadge);
91
            }
92
93
            $this->addFlash('notice', sprintf(
94
                '%s successfully completed the step "%s"!',
95
                $user->getUsername(),
96
                $step->getTitle()
97
            ));
98
99
            $user->addNuts($step->getRewardPoint());
100
            $this->get('badger.game.saver.adventure_step_completion')->save($pendingCompletion);
101
102
            // Check if adventure is complete
103
            $completedSteps = count($this->get('badger.game.repository.adventure_step_completion')
104
                ->userAdventureCompletedSteps($user, $adventure));
105
            $totalStep = count($adventure->getSteps());
106
107
            if ($completedSteps === $totalStep) {
108
                $user->addNuts($adventure->getRewardPoint());
109
110
                $adventureBadge = $adventure->getBadge();
111
                if (null !== $adventureBadge) {
112
                    $this->get('badger.game.unlocker.badge')->unlockBadge($user, $adventureBadge);
113
                }
114
            }
115
        } else {
116
            $this->addFlash('error', (string) $errors);
117
        }
118
119
        return $this->redirectToRoute('admin_claimed_adventure_step_index');
120
    }
121
}
122