Passed
Push — develop ( 6aacf8...6c0b7f )
by BENARD
11:52
created

YoutubeDataHandler::process()   A

Complexity

Conditions 4
Paths 17

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 31
rs 9.584
cc 4
nc 17
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Handler\Video;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use VideoGamesRecords\CoreBundle\DataProvider\YoutubeProvider;
7
use VideoGamesRecords\CoreBundle\Entity\Video;
8
use VideoGamesRecords\CoreBundle\ValueObject\VideoType;
9
10
class YoutubeDataHandler
11
{
12
    protected EntityManagerInterface $em;
13
    protected YoutubeProvider $youtubeProvider;
14
15
    public function __construct(EntityManagerInterface $em, YoutubeProvider $youtubeProvider)
16
    {
17
        $this->em = $em;
18
        $this->youtubeProvider = $youtubeProvider;
19
    }
20
21
    /**
22
     * @param Video $video
23
     * @return void
24
     */
25
    public function process(Video $video): void
26
    {
27
        if ($video->getType()->getValue() !== VideoType::TYPE_YOUTUBE) {
28
            throw new \InvalidArgumentException();
29
        }
30
31
        try {
32
            $response = $this->youtubeProvider->getVideo($video->getVideoId());
33
            if (count($response->getItems()) > 0) {
34
                $youtubeVideo = $response->getItems()[0];
35
36
                $snippet = $youtubeVideo->getSnippet();
37
                $video->setTitle($snippet->getTitle());
38
                echo $video->getId() . "\n";
39
                //var_dump($snippet->getThumbnails());
40
                $video->setThumbnail(
41
                    $snippet->getThumbnails()
42
                        ->getHigh()
43
                        ->getUrl()
44
                );
45
46
                $video->setDescription($snippet->getDescription());
47
48
                $statistics = $youtubeVideo->getStatistics();
49
                $video->setLikeCount($statistics->getLikeCount() ?? 0);
0 ignored issues
show
Bug introduced by
It seems like $statistics->getLikeCount() ?? 0 can also be of type string; however, parameter $likeCount of VideoGamesRecords\CoreBu...y\Video::setLikeCount() does only seem to accept integer, 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

49
                $video->setLikeCount(/** @scrutinizer ignore-type */ $statistics->getLikeCount() ?? 0);
Loading history...
50
                $video->setViewCount($statistics->getViewCount() ?? 0);
0 ignored issues
show
Bug introduced by
It seems like $statistics->getViewCount() ?? 0 can also be of type string; however, parameter $viewCount of VideoGamesRecords\CoreBu...y\Video::setViewCount() does only seem to accept integer, 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

50
                $video->setViewCount(/** @scrutinizer ignore-type */ $statistics->getViewCount() ?? 0);
Loading history...
51
            } else {
52
                $video->setIsActive(false);
53
            }
54
            $this->em->flush();
55
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
56
57
        }
58
    }
59
}
60