Passed
Push — develop ( 1d51ce...e805ec )
by BENARD
06:13
created

GroupAdminController::copyWithLibChartAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Admin;
6
7
use Sonata\AdminBundle\Controller\CRUDController;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
12
use VideoGamesRecords\CoreBundle\Entity\Group;
13
use VideoGamesRecords\CoreBundle\Form\CopyGroupForm;
14
use VideoGamesRecords\CoreBundle\Form\Type\ChartTypeType;
15
use VideoGamesRecords\CoreBundle\Form\VideoProofOnly;
16
17
class GroupAdminController extends CRUDController
18
{
19
    /**
20
     * @param $id
21
     * @param Request $request
22
     * @return Response
23
     */
24
    public function copyAction($id, Request $request): Response
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    public function copyAction(/** @scrutinizer ignore-unused */ $id, Request $request): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        /** @var Group $group */
27
        $group = $this->admin->getSubject();
28
29
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Sonata\AdminBundle\Model\ModelManagerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sonata\AdminBundle\Model\LockInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $em = $this->admin->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->admin->getClass());
Loading history...
30
        $form = $this->createForm(CopyGroupForm::class);
31
        $form->handleRequest($request);
32
        if ($form->isSubmitted() && $form->isValid()) {
33
            $data = $form->getData();
34
            $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->copy($group, $data['withLibs']);
35
36
            $this->addFlash('sonata_flash_success', 'Group was successfully copied.');
37
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $group->getId()]));
38
        }
39
40
        return $this->render(
41
            '@VideoGamesRecordsCore/Admin/Form/form.default.html.twig',
42
            [
43
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
44
                'admin' => $this->admin,
45
                'object' => $group,
46
                'form' => $form,
47
                'title' => 'Copy => ' . $group->getGame()->getName() . ' / ' . $group->getName(),
48
                'action' => 'edit'
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @param         $id
55
     * @param Request $request
56
     * @return RedirectResponse|Response
57
     */
58
    public function addLibChartAction($id, Request $request): RedirectResponse|Response
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
    public function addLibChartAction(/** @scrutinizer ignore-unused */ $id, Request $request): RedirectResponse|Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        /** @var Group $group */
61
        $group = $this->admin->getSubject();
62
63
        if ($group->getGame()->getGameStatus()->isActive()) {
64
            $this->addFlash('sonata_flash_error', 'Game is already activated');
65
            return new RedirectResponse(
66
                $this->admin->generateUrl(
67
                    'list',
68
                    ['filter' => $this->admin->getFilterParameters()]
69
                )
70
            );
71
        }
72
73
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
74
        $form = $this->createForm(ChartTypeType::class);
75
        $form->handleRequest($request);
76
        if ($form->isSubmitted() && $form->isValid()) {
77
            $data = $form->getData();
78
            $type = $data['type'];
79
            $chartType = $em->getRepository('VideoGamesRecords\CoreBundle\Entity\ChartType')->find($type);
80
81
            foreach ($group->getCharts() as $chart) {
82
                $libChart = new ChartLib();
83
                $libChart->setType($chartType);
84
                $chart->addLib($libChart);
85
                $em->persist($libChart);
86
            }
87
            $em->flush();
88
89
            $this->addFlash('sonata_flash_success', 'Add all libchart on group successfully');
90
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $group->getId()]));
91
        }
92
93
        return $this->render(
94
            '@VideoGamesRecordsCore/Admin/Object/Group/form.add_libchart.html.twig',
95
            [
96
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
97
                'admin' => $this->admin,
98
                'object' => $group,
99
                'form' => $form,
100
                'group' => $group,
101
                'action' => 'edit'
102
            ]
103
        );
104
    }
105
106
    /**
107
     * @param         $id
108
     * @param Request $request
109
     * @return RedirectResponse|Response
110
     */
111
    public function setVideoProofOnlyAction($id, Request $request): RedirectResponse|Response
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    public function setVideoProofOnlyAction(/** @scrutinizer ignore-unused */ $id, Request $request): RedirectResponse|Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        /** @var Group $group */
114
        $group = $this->admin->getSubject();
115
116
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
117
        $form = $this->createForm(VideoProofOnly::class);
118
        $form->handleRequest($request);
119
        if ($form->isSubmitted() && $form->isValid()) {
120
            $data = $form->getData();
121
            $isVideoProofOnly = $data['isVideoProofOnly'];
122
            foreach ($group->getCharts() as $chart) {
123
                $chart->setIsProofVideoOnly($isVideoProofOnly);
124
            }
125
            $em->flush();
126
127
            $this->addFlash('sonata_flash_success', 'All charts are updated successfully');
128
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $group->getId()]));
129
        }
130
131
        return $this->render(
132
            '@VideoGamesRecordsCore/Admin/Form/form.set_video_proof_only.html.twig',
133
            [
134
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
135
                'admin' => $this->admin,
136
                'object' => $group,
137
                'form' => $form,
138
                'title' => $group->getGame()->getName() . ' / ' . $group->getName(),
139
                'action' => 'edit'
140
            ]
141
        );
142
    }
143
}
144