/**
  * @return JsonResponse
  *
  * @Route("/sync")
  */
 public function syncAction()
 {
     $youtube = new Youtube(['key' => $this->getParameter('youtube_api_key')]);
     $videos = $youtube->getPlaylistItemsByPlaylistId($this->getParameter('youtube_playlist_id'));
     $em = $this->getDoctrine()->getManager();
     foreach ($videos as $video) {
         $name = $video->snippet->title;
         $youtubeId = $video->snippet->resourceId->videoId;
         $description = $video->snippet->description;
         if (property_exists($video->snippet->thumbnails, 'maxres')) {
             $thumbnail = $video->snippet->thumbnails->maxres->url;
         } elseif (property_exists($video->snippet->thumbnails, 'high')) {
             $thumbnail = $video->snippet->thumbnails->high->url;
         } elseif (property_exists($video->snippet->thumbnails, 'medium')) {
             $thumbnail = $video->snippet->thumbnails->medium->url;
         } else {
             $thumbnail = $video->snippet->thumbnails->default->url;
         }
         $video = $em->getRepository('TGVideoBundle:Video')->findOneByYoutubeId($youtubeId);
         if ($video === null) {
             $video = new Video();
             $video->setYoutubeId($youtubeId);
             $em->persist($video);
         }
         $video->setName($name);
         $video->setDescription($description);
         $video->setThumbnail($thumbnail);
         $video->setUrl('https://www.youtube.com/embed/' . $youtubeId);
     }
     $em->flush();
     $videos = $this->getDoctrine()->getRepository('TGVideoBundle:Video')->findAll([], ['createdAt' => 'DESC']);
     $serializer = new Serializer([$this->get('tg_video.normalizer')]);
     return new JsonResponse(['videos' => $serializer->normalize($videos)]);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $faker = $this->faker;
     for ($i = 0; $i < self::FAKE_VIDEO_COUNT; $i += 1) {
         $randomElement = $faker->randomElement($this->randomVideos);
         $url = 'https://www.youtube.com/watch?v=' . $randomElement;
         $thumbnail = 'https://i.ytimg.com/vi/' . $randomElement . '/default.jpg';
         $video = new Video();
         $video->setName($faker->text(15));
         $video->setDescription($faker->text(500));
         $video->setUrl($url);
         $video->setThumbnail($thumbnail);
         $video->setYoutubeId($randomElement);
         $this->addReference('video-' . $i, $video);
         $manager->persist($video);
     }
     $manager->flush();
 }