GameAdminController::importCsvAction()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 62
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 62
rs 7.6924
cc 9
nc 7
nop 2

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
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Admin;
6
7
use Sonata\AdminBundle\Controller\CRUDController;
8
use Symfony\Bundle\SecurityBundle\Security;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use VideoGamesRecords\CoreBundle\Contracts\SecurityInterface;
14
use VideoGamesRecords\CoreBundle\Entity\Chart;
15
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
16
use VideoGamesRecords\CoreBundle\Entity\ChartType;
17
use VideoGamesRecords\CoreBundle\Entity\Game;
18
use VideoGamesRecords\CoreBundle\Entity\Group;
19
use VideoGamesRecords\CoreBundle\Form\DefaultForm;
20
use VideoGamesRecords\CoreBundle\Form\ImportCsv;
21
use VideoGamesRecords\CoreBundle\Form\VideoProofOnly;
22
use VideoGamesRecords\CoreBundle\Manager\GameManager;
23
use Yokai\SonataWorkflow\Controller\WorkflowControllerTrait;
24
25
class GameAdminController extends CRUDController implements SecurityInterface
26
{
27
    use WorkflowControllerTrait;
28
29
    private GameManager $gameManager;
30
    private Security $security;
31
32
    public function __construct(GameManager $gameManager, Security $security)
33
    {
34
        $this->gameManager = $gameManager;
35
        $this->security = $security;
36
    }
37
38
    /**
39
     * @param $id
40
     * @param Request $request
41
     * @return Response
42
     */
43
    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

43
    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...
44
    {
45
        /** @var Game $game */
46
        $game = $this->admin->getSubject();
47
48
        if (!$this->isGranted(self::ROLE_SUPER_ADMIN)) {
49
            $this->addFlash('sonata_flash_error', 'The game was not copied.');
50
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $game->getId()]));
51
        }
52
53
        $form = $this->createForm(DefaultForm::class);
54
        $form->handleRequest($request);
55
        if ($form->isSubmitted() && $form->isValid()) {
56
            $this->gameManager->copy($game);
57
            $this->addFlash('sonata_flash_success', 'The game was successfully copied.');
58
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $game->getId()]));
59
        }
60
61
        return $this->render(
62
            '@VideoGamesRecordsCore/Admin/Form/form.default.html.twig',
63
            [
64
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
65
                'admin' => $this->admin,
66
                'object' => $game,
67
                'form' => $form,
68
                'title' => 'Copy => ' . $game->getName(),
69
                'action' => 'edit'
70
            ]
71
        );
72
    }
73
74
    /**
75
     * @param $id
76
     * @return RedirectResponse
77
     */
78
    public function majAction($id): RedirectResponse
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

78
    public function majAction(/** @scrutinizer ignore-unused */ $id): RedirectResponse

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...
79
    {
80
        $this->gameManager->maj($this->admin->getSubject());
81
        $this->addFlash('sonata_flash_success', 'Game maj successfully');
82
        return new RedirectResponse($this->admin->generateUrl('list'));
83
    }
84
85
    /**
86
     * @param         $id
87
     * @param Request $request
88
     * @return RedirectResponse|Response
89
     */
90
    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

90
    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...
91
    {
92
        /** @var Game $game */
93
        $game = $this->admin->getSubject();
94
95
        $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

95
        $em = $this->admin->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->admin->getClass());
Loading history...
96
        $form = $this->createForm(VideoProofOnly::class);
97
        $form->handleRequest($request);
98
        if ($form->isSubmitted() && $form->isValid()) {
99
            $data = $form->getData();
100
            $isVideoProofOnly = $data['isVideoProofOnly'];
101
            foreach ($game->getGroups() as $group) {
102
                foreach ($group->getCharts() as $chart) {
103
                    $chart->setIsProofVideoOnly($isVideoProofOnly);
104
                }
105
            }
106
            $em->flush();
107
108
            $this->addFlash('sonata_flash_success', 'All charts are updated successfully');
109
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $game->getId()]));
110
        }
111
112
        return $this->render(
113
            '@VideoGamesRecordsCore/Admin/Form/form.set_video_proof_only.html.twig',
114
            [
115
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
116
                'admin' => $this->admin,
117
                'object' => $game,
118
                'form' => $form,
119
                'title' => $game->getName(),
120
                'action' => 'edit'
121
            ]
122
        );
123
    }
124
125
126
    public function importCsvAction(int $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

126
    public function importCsvAction(/** @scrutinizer ignore-unused */ int $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...
127
    {
128
        /** @var Game $game */
129
        $game = $this->admin->getSubject();
130
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
131
132
        $form = $this->createForm(ImportCsv::class);
133
        $form->handleRequest($request);
134
        if ($form->isSubmitted() && $form->isValid()) {
135
            /** @var UploadedFile $csvFile */
136
            $csvFile = $form->get('csv')->getData();
137
138
            $rows = str_getcsv($csvFile->getContent(), "\n");
139
140
            /** @var Group $group */
141
            $group = null;
142
            foreach ($rows as $index => $row) {
143
                if ($index === 0) {
144
                    continue;
145
                }
146
                $data = str_getcsv($row, ";");
147
                if (count($data) !== 5) {
148
                    throw new \RuntimeException('Invalid number of rows.');
149
                }
150
151
                if (!is_numeric($data[4])) {
152
                    throw new \RuntimeException('Column 5 must be numeric.');
153
                }
154
                if ($group === null || $group->getLibGroupEn() !== $data[0]) {
155
                    $group = new Group();
156
                    $group->setGame($game);
157
                    $group->setLibGroupEn($data[0]);
158
                    $group->setLibGroupFr($data[1]);
159
                    $em->persist($group);
160
                }
161
162
                $chart = new Chart();
163
                $chart->setGroup($group);
164
                $chart->setLibChartEn($data[2]);
165
                $chart->setLibChartFr($data[3]);
166
                $em->persist($chart);
167
168
                $chartLib = new ChartLib();
169
                $chartLib->setChart($chart);
170
                $chartLib->setType($em->getReference(ChartType::class, $data[4]));
171
                $em->persist($chartLib);
172
            }
173
            $em->flush();
174
175
            $this->addFlash('sonata_flash_success', 'CSV DATA successfully imported');
176
            return new RedirectResponse($this->admin->generateUrl('show', ['id' => $game->getId()]));
177
        }
178
179
        return $this->render(
180
            '@VideoGamesRecordsCore/Admin/Form/form.default.html.twig',
181
            [
182
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
183
                'admin' => $this->admin,
184
                'object' => $game,
185
                'form' => $form,
186
                'title' => $game->getName(),
187
                'action' => 'edit'
188
            ]
189
        );
190
    }
191
}
192