Passed
Push — develop ( 18f462...398719 )
by BENARD
05:10
created

SendPicture   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 67
c 1
b 0
f 0
dl 0
loc 114
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B __invoke() 0 87 5
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller\PlayerChart;
4
5
use Aws\S3\S3Client;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Exception;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use VideoGamesRecords\CoreBundle\Entity\Picture;
13
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
14
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
15
use VideoGamesRecords\CoreBundle\Entity\Proof;
16
use VideoGamesRecords\CoreBundle\Exception\AccessDeniedException;
17
use VideoGamesRecords\CoreBundle\Security\UserProvider;
18
19
class SendPicture extends AbstractController
20
{
21
    private S3Client $s3client;
22
    private UserProvider $userProvider;
23
    private EntityManagerInterface $em;
24
25
    private array $extensions = array(
26
        'image/png' => '.png',
27
        'image/jpeg' => '.jpg',
28
    );
29
30
    public function __construct(
31
        S3Client $s3client,
32
        UserProvider $userProvider,
33
        EntityManagerInterface $em
34
    ) {
35
        $this->s3client = $s3client;
36
        $this->userProvider = $userProvider;
37
        $this->em = $em;
38
    }
39
40
    /**
41
     * @param PlayerChart $playerChart
42
     * @param Request     $request
43
     * @return Response
44
     * @throws Exception
45
     */
46
    public function __invoke(PlayerChart $playerChart, Request $request): Response
47
    {
48
        $player = $this->userProvider->getPlayer();
49
50
        if ($playerChart->getPlayer() !== $player) {
51
            throw new AccessDeniedException('ACESS DENIED');
52
        }
53
        if (!in_array($playerChart->getStatus()->getId(), PlayerChartStatus::getStatusForProving())) {
54
            throw new AccessDeniedException('ACESS DENIED');
55
        }
56
57
        $id = $playerChart->getId();
58
        $idPlayer = $playerChart->getPlayer()->getId();
59
        $idGame = $playerChart->getChart()->getGroup()->getGame()->getId();
60
61
        $data = json_decode($request->getContent(), true);
62
        $file = $data['file'];
63
64
        $hash = hash_file('sha256', $file);
65
        $picture = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Picture')->findOneBy(
66
            array(
67
                'hash' => $hash,
68
                'player' => $playerChart->getPlayer(),
69
                'game' => $playerChart->getChart()->getGroup()->getGame(),
70
            )
71
        );
72
73
        if ($picture == null) {
74
            $fp = fopen($file, 'r');
75
            $meta = stream_get_meta_data($fp);
76
77
            $metadata = [
78
                'idplayer' => $idPlayer,
79
                'idgame' => $idGame,
80
            ];
81
            $key = $idPlayer . '/' . $idGame . '/' . uniqid() . $this->extensions[$meta['mediatype']];
82
83
            $this->s3client->putObject([
84
                'Bucket' => $_ENV['AWS_S3_BUCKET_PROOF'],
85
                'Key'    => $key,
86
                'Body'   => $fp,
87
                'ACL'    => 'public-read',
88
                'ContentType' => $meta['mediatype'],
89
                'Metadata' => [
90
                    'idplayer' => $idPlayer,
91
                    'idgame' => $idGame
92
                ],
93
                'StorageClass' => 'STANDARD',
94
            ]);
95
96
            //-- Picture
97
            $picture = new Picture();
98
            $picture->setPath($key);
99
            $picture->setMetadata(serialize($metadata));
100
            $picture->setPlayer($playerChart->getPlayer());
101
            $picture->setGame($playerChart->getChart()->getGroup()->getGame());
102
            $picture->setHash($hash);
103
            $this->em->persist($picture);
104
        }
105
106
107
        //-- Proof
108
        $proof = new Proof();
109
        $proof->setPicture($picture);
110
        $proof->setPlayer($playerChart->getPlayer());
111
        $proof->setChart($playerChart->getChart());
112
        $this->em->persist($proof);
113
114
        //-- PlayerChart
115
        $playerChart->setProof($proof);
116
        if ($playerChart->getStatus()->getId() === PlayerChartStatus::ID_STATUS_NORMAL) {
117
            // NORMAL TO NORMAL_SEND_PROOF
118
            $playerChart->setStatus(
119
                $this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)
0 ignored issues
show
Bug introduced by
It seems like $this->em->getReference(...ATUS_NORMAL_SEND_PROOF) can also be of type null; however, parameter $status of VideoGamesRecords\CoreBu...layerChart::setStatus() does only seem to accept VideoGamesRecords\CoreBu...ntity\PlayerChartStatus, maybe add an additional type check? ( Ignorable by Annotation )

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

119
                /** @scrutinizer ignore-type */ $this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)
Loading history...
120
            );
121
        } else {
122
            // INVESTIGATION TO DEMAND_SEND_PROOF
123
            $playerChart->setStatus(
124
                $this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)
125
            );
126
        }
127
        $this->em->flush();
128
129
        return new JsonResponse([
130
            'id' => $id,
131
            'file' => $file,
132
        ], 200);
133
    }
134
}
135