Passed
Branch develop (5d255a)
by BENARD
05:18
created

PlayerChartController::last()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller;
4
5
use Aws\S3\S3Client;
6
use Exception;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
use Symfony\Contracts\Translation\TranslatorInterface;
12
use VideoGamesRecords\CoreBundle\Entity\Game;
13
use VideoGamesRecords\CoreBundle\Entity\Picture;
14
use VideoGamesRecords\CoreBundle\Entity\Platform;
15
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
16
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
17
use VideoGamesRecords\CoreBundle\Entity\Proof;
18
use VideoGamesRecords\CoreBundle\Entity\Video;
19
use VideoGamesRecords\CoreBundle\Exception\AccessDeniedException;
20
use VideoGamesRecords\CoreBundle\Service\PlayerChartService;
21
22
/**
23
 * Class PlayerChartController
24
 * @Route("/player-chart")
25
 */
26
class PlayerChartController extends DefaultController
27
{
28
    private S3Client $s3client;
29
    private TranslatorInterface $translator;
30
    private PlayerChartService $playerChartService;
31
32
    private array $extensions = array(
33
        'text/plain' => '.txt',
34
        'image/png' => '.png',
35
        'image/jpeg' => '.jpg',
36
    );
37
38
    public function __construct(
39
        S3Client $s3client,
40
        TranslatorInterface $translator,
41
        PlayerChartService $playerChartService
42
    ) {
43
        $this->s3client = $s3client;
44
        $this->translator = $translator;
45
        $this->playerChartService = $playerChartService;
46
    }
47
48
    /**
49
     * @param Request $request
50
     * @return JsonResponse
51
     */
52
    public function majPlatform(Request $request): JsonResponse
53
    {
54
        $data = json_decode($request->getContent(), true);
55
        $idGame = $data['idGame'];
56
        $idPlatform = $data['idPlatform'];
57
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

57
        $em = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58
59
        $this->playerChartService->majPlatform(
60
            $this->getPlayer(),
61
            $em->getReference(Game::class, $idGame),
0 ignored issues
show
Bug introduced by
The method getReference() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Persistence\ObjectManagerDecorator. 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

61
            $em->/** @scrutinizer ignore-call */ 
62
                 getReference(Game::class, $idGame),
Loading history...
62
            $em->getReference(Platform::class, $idPlatform)
63
        );
64
        return new JsonResponse(['data' => true]);
65
    }
66
67
    /**
68
     * @param PlayerChart $playerChart
69
     * @param Request     $request
70
     * @return Response
71
     * @throws Exception
72
     */
73
    public function sendPicture(PlayerChart $playerChart, Request $request): Response
74
    {
75
        if ($playerChart->getPlayer() != $this->getPlayer()) {
76
            throw new AccessDeniedException('ACESS DENIED');
77
        }
78
        if (!in_array($playerChart->getStatus()->getId(), PlayerChartStatus::getStatusForProving())) {
79
            throw new AccessDeniedException('ACESS DENIED');
80
        }
81
82
        $id = $playerChart->getId();
83
        $idPlayer = $playerChart->getPlayer()->getId();
84
        $idGame = $playerChart->getChart()->getGroup()->getGame()->getId();
85
86
        $data = json_decode($request->getContent(), true);
87
        $file = $data['file'];
88
89
        $hash = hash_file('sha256', $file);
90
        $picture = $this->getDoctrine()->getRepository('VideoGamesRecords\CoreBundle\Entity\Picture')->findOneBy(
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

90
        $picture = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getRepository('VideoGamesRecords\CoreBundle\Entity\Picture')->findOneBy(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
            array(
92
                'hash' => $hash,
93
                'player' => $playerChart->getPlayer(),
94
                'game' => $playerChart->getChart()->getGroup()->getGame(),
95
            )
96
        );
97
98
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

98
        $em = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
100
        if ($picture == null) {
101
            $fp = fopen($file, 'r');
102
            $meta = stream_get_meta_data($fp);
103
104
            $metadata = [
105
                'idplayer' => $idPlayer,
106
                'idgame' => $idGame,
107
            ];
108
            $key = $idPlayer . '/' . $idGame . '/'. uniqid() . $this->extensions[$meta['mediatype']];
109
110
            $this->s3client->putObject([
111
                'Bucket' => $_ENV['AWS_BUCKET_PROOF'],
112
                'Key'    => $key,
113
                'Body'   => $fp,
114
                'ACL'    => 'public-read',
115
                'ContentType' => $meta['mediatype'],
116
                'Metadata' => [
117
                    'idplayer' => $idPlayer,
118
                    'idgame' => $idGame
119
                ],
120
                'StorageClass' => 'STANDARD',
121
            ]);
122
123
            //-- Picture
124
            $picture = new Picture();
125
            $picture->setPath($key);
126
            $picture->setMetadata(serialize($metadata));
127
            $picture->setPlayer($playerChart->getPlayer());
128
            $picture->setGame($playerChart->getChart()->getGroup()->getGame());
129
            $picture->setHash($hash);
130
            $em->persist($picture);
131
        }
132
133
134
        //-- Proof
135
        $proof = new Proof();
136
        $proof->setPicture($picture);
137
        $proof->setPlayer($playerChart->getPlayer());
138
        $proof->setChart($playerChart->getChart());
139
        $em->persist($proof);
140
141
        //-- PlayerChart
142
        $playerChart->setProof($proof);
143
        if ($playerChart->getStatus()->getId() === PlayerChartStatus::ID_STATUS_NORMAL) {
144
            // NORMAL TO NORMAL_SEND_PROOF
145
            $playerChart->setStatus(
146
                $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

146
                /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
147
            );
148
        } else {
149
            // INVESTIGATION TO DEMAND_SEND_PROOF
150
            $playerChart->setStatus(
151
                $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

151
                /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
152
            );
153
        }
154
        $em->flush();
155
156
        $response = new Response();
157
        $response->setContent(json_encode([
158
            'id' => $id,
159
            'file' => $file,
160
        ]));
161
        $response->headers->set('Content-Type', 'application/json');
162
        return $response;
163
    }
164
165
    /**
166
     * @param PlayerChart $playerChart
167
     * @param Request     $request
168
     * @return Response
169
     * @throws AccessDeniedException
170
     */
171
    public function sendVideo(PlayerChart $playerChart, Request $request): Response
172
    {
173
        if ($playerChart->getPlayer() != $this->getPlayer()) {
174
            throw new AccessDeniedException('ACESS DENIED');
175
        }
176
        if (!in_array($playerChart->getStatus()->getId(), PlayerChartStatus::getStatusForProving())) {
177
            throw new AccessDeniedException('ACESS DENIED');
178
        }
179
180
        $data = json_decode($request->getContent(), true);
181
        $url = $data['url'];
182
183
        $videoIn = new Video();
184
        $videoIn->setUrl($url);
185
186
        if (in_array($videoIn->getType(), array(Video::TYPE_TWITCH, Video::TYPE_YOUTUBE))) {
187
            $video = $this->getDoctrine()->getRepository('VideoGamesRecords\CoreBundle\Entity\Video')->findOneBy(
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

187
            $video = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getRepository('VideoGamesRecords\CoreBundle\Entity\Video')->findOneBy(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
188
                array(
189
                    'videoId' => $videoIn->getVideoId(),
190
                )
191
            );
192
193
            $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

193
            $em = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
194
195
            if ($video == null) {
196
                //-- Video
197
                $video = new Video();
198
                $video->setUrl($url);
199
                $video->setPlayer($this->getPlayer());
200
                $video->setGame($playerChart->getChart()->getGroup()->getGame());
201
                $video->setLibVideo($playerChart->getChart()->getCompleteName('en'));
202
                $em->persist($video);
203
            }
204
205
            //-- Proof
206
            $proof = new Proof();
207
            $proof->setVideo($video);
208
            $proof->setPlayer($playerChart->getPlayer());
209
            $proof->setChart($playerChart->getChart());
210
            $em->persist($proof);
211
212
            //-- PlayerChart
213
            $playerChart->setProof($proof);
214
            if ($playerChart->getStatus()->getId() === PlayerChartStatus::ID_STATUS_NORMAL) {
215
                // NORMAL TO NORMAL_SEND_PROOF
216
                $playerChart->setStatus(
217
                    $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

217
                    /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
218
                );
219
            } else {
220
                // INVESTIGATION TO DEMAND_SEND_PROOF
221
                $playerChart->setStatus(
222
                    $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

222
                    /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager()->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
223
                );
224
            }
225
            $em->flush();
226
227
        } else {
228
            return $this->getResponse(false, $this->translator->trans('video.type_not_found'));
229
        }
230
231
        return $this->getResponse(true, ($this->translator->trans('proof.form.success')));
232
    }
233
}
234