Pārlūkot izejas kodu

Merge branch 'develop' into features/background

François Drouhard 2 gadi atpakaļ
vecāks
revīzija
1e630b88ae

+ 0 - 1
src/Entity/Film.php

@@ -4,7 +4,6 @@ namespace App\Entity;
 
 use App\Repository\FilmRepository;
 use Doctrine\Common\Collections\Collection;
-use Doctrine\DBAL\Types\Types;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 

+ 26 - 13
src/Entity/MediaVideo.php

@@ -40,6 +40,12 @@ class MediaVideo
     
     private $url;
 
+    public function setFromTmdb(string $type, string $identif): void
+    {
+        $this->setType($type);
+        $this->setIdentif($identif);
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -47,17 +53,18 @@ class MediaVideo
 
     public function getUrl(): ?string
     {
-        return $this->url;
+        return $this->url();
     }
 
-    public function setUrl($url): self
+    public function setUrl(string $url): self
     {
         $this->url = $url;
+        $this->extractIdentif();
 
         return $this;
     }
     
-    public function setType($type): self
+    public function setType(string $type): self
     {
         $this->type = $type;
 
@@ -69,7 +76,7 @@ class MediaVideo
         return $this->type;
     }
     
-    public function setIdentif($identif): self
+    public function setIdentif(string $identif): self
     {
         $this->identif = $identif;
 
@@ -126,13 +133,13 @@ class MediaVideo
         $this->setType('vimeo');  // signale qu’il s’agit d’une video vimeo et l’inscrit dans l’attribut $type
     }
 
-    #[ORM\PrePersist()] // Les trois événement suivant s’exécute avant que l’entité soit enregistée
-    #[ORM\PreUpdate()]
-    #[ORM\PreFlush()]
+    //#[ORM\PrePersist()] // Les trois événements suivant s’exécutent avant que l’entité soit enregistrée
+    //#[ORM\PreUpdate()]
+    //#[ORM\PreFlush()]
     
     public function extractIdentif(): void
     {
-        $url = $this->getUrl();  // on récupère l’url
+        $url = $this->url;  // on récupère l’url
 
         if (preg_match("#^(http|https)://video.fdlibre.eu/#", $url))  // Si c'est peertube fdlibre
         {
@@ -158,7 +165,7 @@ class MediaVideo
 
     }
 
-    private function embedUrl()
+    public function embedUrl(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -175,10 +182,12 @@ class MediaVideo
         } else if ($control == 'peertube') {
             $embed = "https://video.fdlibre.eu/videos/embed/" . $id;
             return $embed;
+        } else {
+            return null;
         }
     }
 
-    private function url()
+    public function url(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -195,11 +204,13 @@ class MediaVideo
         } else if ($control == 'peertube') {
             $embed = "https://video.fdlibre.eu/videos/watch/" . $id;
             return $embed;
+        } else {
+            return null;
         }
     }
 
 
-    public function image()
+    public function image(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -214,13 +225,15 @@ class MediaVideo
             $hash = unserialize(file_get_contents("https://vimeo.com/api/v2/video/" . $id . ".php"));
             $image = $hash[0]['thumbnail_small'];
             return $image;
+        } else {
+            return null;
         }
     }
 
 
-    public function video()
+    public function video(): string
     {
-        $video = "<iframe src='".$this->embedUrl()."'  allowfullscreen></iframe>";
+        $video = "<iframe src='".$this->embedUrl()."' allowfullscreen></iframe>";
         return $video;
     }
 }

+ 26 - 9
src/Repository/FilmRepository.php

@@ -20,7 +20,8 @@ class FilmRepository extends ServiceEntityRepository
         parent::__construct($registry, Film::class);
     }
 
-    public function findFilm($id) {
+    public function findFilm($id): Film
+    {
         $qb = $this->createQueryBuilder('f');
         $query = $qb
             ->select('f.titre', 'f.annee')
@@ -30,7 +31,8 @@ class FilmRepository extends ServiceEntityRepository
         return $query->getSingleResult();
     }
 
-    public function findFilmWithGenre(array $genreNames) {
+    public function findFilmWithGenre(array $genreNames) : array
+    {
         $qb = $this->createQueryBuilder('f');
         $qb
             ->innerJoin('f.genres', 'g')
@@ -43,7 +45,8 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
-    public function findFilmWithReal(array $realisateursnames) {
+    public function findFilmWithReal(array $realisateursnames): array
+    {
         $qb = $this->createQueryBuilder('f');
         $qb
             ->leftJoin('f.realisateurs', 'rea')
@@ -55,7 +58,7 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
-    public function findFilmWithRealLike($query)
+    public function findFilmWithRealLike($query): array
     {
         $qb = $this->createQueryBuilder('f');
         $qb
@@ -69,7 +72,8 @@ class FilmRepository extends ServiceEntityRepository
 
     }
 
-    public function findFilmWithGenreLike($genreName) {
+    public function findFilmWithGenreLike($genreName): array
+    {
         $qb = $this->createQueryBuilder('f');
         $qb
             ->innerJoin('f.genres', 'g')
@@ -85,7 +89,8 @@ class FilmRepository extends ServiceEntityRepository
 
 
 
-    public function findTousDesc() {
+    public function findTousDesc(): array
+    {
         $qb = $this->createQueryBuilder('f');
         $qb
             ->select('f')
@@ -96,7 +101,8 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
-    public function findTous () {
+    public function findTous (): array
+    {
         $qb = $this->createQueryBuilder('f')
             ->leftJoin('f.authered', 'aut')->addSelect('aut')
             ->leftJoin('f.genres', 'gen')->addSelect('gen')
@@ -107,7 +113,7 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
-    public function findTousFavoritesByUser($user)
+    public function findTousFavoritesByUser($user): array
     {
         $qb = $this->createQueryBuilder('f');
         $qb
@@ -123,7 +129,7 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult();
     }
 
-    public function findProchaines()
+    public function findProchaines(): array
     {
         $date = new \DateTime('now');
         $interval = new \DateInterval("P1D");
@@ -141,4 +147,15 @@ class FilmRepository extends ServiceEntityRepository
             ->getQuery()
             ->getResult();
     }
+
+    public function findFilmWithExistentTitre(string $titre): array
+    {
+        return $this->createQueryBuilder('f')
+            ->leftJoin('f.realisateurs', 'r')->addSelect('r')
+            ->where('f.titre = :titre')
+            ->setParameter('titre', $titre)
+            ->getQuery()
+            ->getResult()
+        ;
+    }
 }

+ 6 - 1
src/Service/FilmManager.php

@@ -13,7 +13,12 @@ use Symfony\Component\Security\Core\Security;
 class FilmManager {
     protected $user;
 
-    public function __construct(protected EntityManagerInterface $em, protected UniciteCollections $uc, protected CommentaireManager $cm, Security $security)
+    public function __construct(
+        protected EntityManagerInterface $em,
+        protected UniciteCollections $uc,
+        protected CommentaireManager $cm,
+        Security $security
+    )
     {
         $this->user = $security->getUser();
     }

+ 28 - 1
src/Service/TmdbApiService.php

@@ -4,6 +4,7 @@ namespace App\Service;
 
 use App\Entity\Film;
 use App\Entity\Genre;
+use App\Entity\MediaVideo;
 use App\Entity\Realisateur;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
@@ -89,7 +90,7 @@ class TmdbApiService
     public function hydrateFilm($filmTmdbId): Film
     {
         $filmTmdb = json_decode($this->getDetailMovie($filmTmdbId));
-        
+        dump($filmTmdb);
         $film = new Film();
         $film
             ->setTitre($filmTmdb->title)
@@ -120,6 +121,13 @@ class TmdbApiService
             $film->addRealisateur($realisateur);
         }
 
+        $videoTmdb = $this->getOneVideo($filmTmdb);
+        if (!empty($videoTmdb)) {
+            $video = new MediaVideo();
+            $video->setFromTmdb($videoTmdb['type'], $videoTmdb['identif']);
+            $film->setMediaVideo($video);
+        }
+
         $film = $this->uc->assureUniciteCollections($film);
         return $film;
     }
@@ -136,4 +144,23 @@ class TmdbApiService
         }
         return $directors;
     }
+
+    public function getOneVideo($detailMovie): array
+    {
+        foreach($detailMovie->videos->results as $video) {
+            dump($video);
+            if (
+                ($video->site === "YouTube" || $video->site === "Vimeo" || $video->site === "DailyMotion") &&
+                $video->type === "Trailer" &&
+                $video->iso_639_1 === "fr" && $video->iso_3166_1 === "FR"
+            ) {
+                return [
+                    'type' => strtolower($video->site),
+                    'identif'   => $video->key
+                ];
+            }
+        }
+        return [];
+    }
+
 }

+ 114 - 0
tests/Entity/MediaVideoTest.php

@@ -0,0 +1,114 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Tests\Entity;
+
+use App\Entity\MediaVideo;
+use PHPUnit\Framework\TestCase;
+
+class MediaVideoTest extends TestCase
+{
+    /**
+     * @dataProvider urlsForMedia
+     */
+    public function testByUrl(
+        string $url,
+        string $expectedUrl,
+        string $expectedEmbedUrl,
+        string $expectedType,
+        string $expectedIdentif,
+        bool $isPathImageExists
+    ): void
+    {
+        $media = new MediaVideo();
+        $media->setUrl($url);
+        $this->assertEquals($expectedUrl, $media->getUrl());
+        $this->assertEquals($expectedEmbedUrl, $media->embedUrl());
+        $this->assertEquals($expectedType, $media->getType());
+        $this->assertEquals($expectedIdentif, $media->getIdentif());
+        $this->assertNull($media->getId());
+        $this->assertEquals("<iframe src='".$expectedEmbedUrl."' allowfullscreen></iframe>", $media->video());
+        if ($isPathImageExists) {
+            $this->assertIsString($media->image());
+        } else {
+            $this->assertNull($media->image());
+        }
+    }
+
+    /**
+     * @dataProvider urlsForMedia
+     */
+    public function testFromTmdb(
+        string $url,
+        string $expectedUrl,
+        string $expectedEmbedUrl,
+        string $expectedType,
+        string $expectedIdentif,
+        bool $isPathImageExists
+    ): void
+    {
+        $media2 = new MediaVideo();
+        $media2->setFromTmdb($expectedType, $expectedIdentif);
+        $this->assertEquals($expectedUrl, $media2->getUrl());
+        $this->assertEquals($expectedEmbedUrl, $media2->embedUrl());
+        $this->assertEquals($expectedType, $media2->getType());
+        $this->assertEquals($expectedIdentif, $media2->getIdentif());
+        $this->assertNull($media2->getId());
+        $this->assertEquals("<iframe src='".$expectedEmbedUrl."' allowfullscreen></iframe>", $media2->video());
+        if ($isPathImageExists) {
+            $this->assertIsString($media2->image());
+        } else {
+            $this->assertNull($media2->image());
+        }
+
+    }
+
+
+    public function urlsForMedia()
+    {
+        return [
+            [
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube-nocookie.com/embed/jZm8eSX7MTc',
+                'youtube',
+                'jZm8eSX7MTc',
+                true
+            ],
+            [
+                'https://www.dailymotion.com/video/x8efziw',
+                'https://www.dailymotion.com/video/x8efziw',
+                'https://www.dailymotion.com/embed/video/x8efziw',
+                'dailymotion',
+                'x8efziw',
+                true
+            ],
+            [
+                'https://vimeo.com/48130434',
+                'https://vimeo.com/48130434',
+                'https://player.vimeo.com/video/48130434',
+                'vimeo',
+                '48130434',
+                true
+            ],
+            [
+                'https://video.fdlibre.eu/videos/watch/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'https://video.fdlibre.eu/videos/watch/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'https://video.fdlibre.eu/videos/embed/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'peertube',
+                'eb38ccf6-62ce-4b52-874c-beaacc896612',
+                false
+            ],
+            [
+                'https://youtu.be/jZm8eSX7MTc',
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube-nocookie.com/embed/jZm8eSX7MTc',
+                'youtube',
+                'jZm8eSX7MTc',
+                true
+            ],
+
+        ];
+    }
+
+}

+ 24 - 0
tests/Entity/RealisateurTest.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Tests\Entity;
+
+use App\Entity\Film;
+use App\Entity\Realisateur;
+use PHPUnit\Framework\TestCase;
+
+class RealisateurTest extends TestCase
+{
+    public function testGetters(): void
+    {
+        $realisateur = new Realisateur();
+        $realisateur->setNomComplet('Bill Bilbao');
+        $film = new Film();
+        $realisateur->addFilm($film);
+        $this->assertEquals('Bill Bilbao', $realisateur->getNomComplet());
+        $this->assertCount(1, $realisateur->getFilms());
+        $this->assertContainsOnlyInstancesOf(Film::class, $realisateur->getFilms());
+        $this->assertNull($realisateur->getId());
+        $realisateur->removeFilm($film);
+        $this->assertCount(0, $realisateur->getFilms());
+    }
+}