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

SendVideo   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 43
c 1
b 0
f 0
dl 0
loc 82
rs 10

2 Methods

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

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