SendVideo   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 81
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B __invoke() 0 59 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\PlayerChart;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Exception\ORMException;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
14
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
15
use VideoGamesRecords\CoreBundle\Entity\Proof;
16
use VideoGamesRecords\CoreBundle\Entity\Video;
17
use VideoGamesRecords\CoreBundle\Exception\AccessDeniedException;
18
use VideoGamesRecords\CoreBundle\Security\UserProvider;
19
use VideoGamesRecords\CoreBundle\ValueObject\VideoType;
20
21
class SendVideo extends AbstractController
22
{
23
    private UserProvider $userProvider;
24
    private EntityManagerInterface $em;
25
    private TranslatorInterface $translator;
26
27
    public function __construct(
28
        UserProvider $userProvider,
29
        EntityManagerInterface $em,
30
        TranslatorInterface $translator
31
    ) {
32
        $this->userProvider = $userProvider;
33
        $this->em = $em;
34
        $this->translator = $translator;
35
    }
36
37
    /**
38
     * @param PlayerChart $playerChart
39
     * @param Request     $request
40
     * @return Proof
41
     * @throws AccessDeniedException|ORMException
42
     */
43
    public function __invoke(PlayerChart $playerChart, Request $request): Proof
44
    {
45
        $player = $this->userProvider->getPlayer();
46
47
        if ($playerChart->getPlayer() !== $player) {
48
            throw new AccessDeniedException('ACESS DENIED');
49
        }
50
        if (!in_array($playerChart->getStatus()->getId(), PlayerChartStatus::getStatusForProving())) {
51
            throw new AccessDeniedException('ACESS DENIED');
52
        }
53
54
        $data = json_decode($request->getContent(), true);
55
        $url = $data['url'];
56
57
        $videoIn = new Video();
58
        $videoIn->setUrl($url);
59
60
        if (in_array($videoIn->getType(), array(VideoType::TWITCH, VideoType::YOUTUBE))) {
61
            $video = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Video')->findOneBy(
62
                array(
63
                    'externalId' => $videoIn->getExternalId(),
64
                )
65
            );
66
67
            if ($video == null) {
68
                //-- Video
69
                $video = new Video();
70
                $video->setUrl($url);
71
                $video->setPlayer($player);
72
                $video->setGame($playerChart->getChart()->getGroup()->getGame());
73
                $this->em->persist($video);
74
            }
75
76
            //-- Proof
77
            $proof = new Proof();
78
            $proof->setVideo($video);
79
            $proof->setPlayer($playerChart->getPlayer());
80
            $proof->setChart($playerChart->getChart());
81
            $this->em->persist($proof);
82
83
            //-- PlayerChart
84
            $playerChart->setProof($proof);
85
            if ($playerChart->getStatus()->getId() === PlayerChartStatus::ID_STATUS_NORMAL) {
86
                // NORMAL TO NORMAL_SEND_PROOF
87
                $playerChart->setStatus(
88
                    $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

88
                    /** @scrutinizer ignore-type */ $this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF)
Loading history...
89
                );
90
            } else {
91
                // INVESTIGATION TO DEMAND_SEND_PROOF
92
                $playerChart->setStatus(
93
                    $this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF)
94
                );
95
            }
96
            $this->em->flush();
97
        } else {
98
            throw new BadRequestException($this->translator->trans('video.type_not_found', [], 'VgrCore'));
99
        }
100
101
        return $proof;
102
    }
103
}
104