From 1e308260c86463d730bd2f8a33cb6450e1dc1ecc Mon Sep 17 00:00:00 2001 From: Camille Simiand <camille.simiand@tetras-libre.fr> Date: Mon, 7 Feb 2022 17:35:54 +0100 Subject: [PATCH] Refacto --- src/Controller/CapsuleController.php | 43 +------------------ src/Entity/Capsule.php | 20 +++++++++ templates/capsule/index.html.twig | 19 +++----- templates/project/project_not_found.html.twig | 4 +- tests/functional/ProjectControllerTest.php | 6 +-- 5 files changed, 32 insertions(+), 60 deletions(-) diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index 59797db..dbf0c44 100644 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -10,7 +10,6 @@ use App\Helper\StringHelper; use App\Repository\CapsuleRepository; use App\Builder\CapsuleBuilder; use App\Form\CreateCapsuleFormType; -use App\Retriever\ProjectRetriever; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Filesystem\Filesystem; @@ -110,31 +109,6 @@ class CapsuleController extends AbstractController ); } - /** - * @Route("capsule/{capsule_id}/video_preview", name="video_preview") - */ - public function videoPreview(int $capsule_id): Response - { - chdir('../legacy/'); - $file_system = new Filesystem(); - $capsule = $this->capsule_repository->findOneBy(['id' => $capsule_id]); - - if (! $capsule instanceof Capsule) { - throw new \Exception('The retrieved capsule is not an instance of Capsule.'); - } - - if (! $file_system->exists($capsule->getLinkPath())) { - return $this->render('project/project_not_found.html.twig'); - } - - $video_preview_image_link = $this->getVideoPreviewImageLink($capsule); - - return $this->render( - 'project/project_preview.html.twig', - ['video_preview_url' => $video_preview_image_link] - ); - } - /** * @Route("capsule/edit/{path}", name="edit_capsule") */ @@ -148,12 +122,12 @@ class CapsuleController extends AbstractController $capsule = $this->capsule_repository->findOneBy(['link_path' => $path]); if (null === $capsule) { - $this->addFlash('warning', $translator->trans('capsule.edit.not_found')); + $this->addFlash('warning', $this->translator->trans('capsule.edit.not_found')); return $this->redirectToRoute('capsule_list'); } if (! $capsule->getEditors()->contains($current_user)) { - $this->addFlash('warning', $translator->trans('capsule.edit.not_allowed')); + $this->addFlash('warning', $this->translator->trans('capsule.edit.not_allowed')); return $this->redirectToRoute('capsule_list'); } @@ -327,17 +301,4 @@ class CapsuleController extends AbstractController return $capsule; } - - private function getVideoPreviewImageLink(Capsule $capsule): string - { - $project_retriever = new ProjectRetriever($capsule); - $video_url = $project_retriever->getVideoUrl(); - $video_id = $project_retriever->getVideoId($video_url); - - if (strpos($video_url, 'yout') !== false) { - return 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; - } - - return 'https://vumbnail.com/' . $video_id . '.jpg'; - } } diff --git a/src/Entity/Capsule.php b/src/Entity/Capsule.php index 28ecd46..08ff123 100644 --- a/src/Entity/Capsule.php +++ b/src/Entity/Capsule.php @@ -211,4 +211,24 @@ class Capsule { return $this->editors; } + + + public function getVideoPreviewImageLink(): ?string + { + $file_system = new Filesystem(); + + if (! $file_system->exists('../legacy/' . $this->getLinkPath())) { + return null; + } + + $project_retriever = new ProjectRetriever($this); + $video_url = $project_retriever->getVideoUrl(); + $video_id = $project_retriever->getVideoId($video_url); + + if (strpos($video_url, 'yout') !== false) { + return 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; + } + + return 'https://vumbnail.com/' . $video_id . '.jpg'; + } } diff --git a/templates/capsule/index.html.twig b/templates/capsule/index.html.twig index 23bb03d..24c3d22 100644 --- a/templates/capsule/index.html.twig +++ b/templates/capsule/index.html.twig @@ -56,22 +56,13 @@ <div class="d-flex flex-column flex-md-row justify-content-center align-items-center"> <div class="m-4 ratio ratio-16x9"> -{# <img src="{{ capsule.getVideoPreviewImageLink() }}" alt="video preview">#} - {# <img src="/capsule/{{ capsule.getId() }}/video_preview" alt="video preview">#} + {% if capsule.getVideoPreviewImageLink() is null %} + <img src="{{ asset('build/images/project_not_found.jpg') }}" alt="video preview"> + {% else %} + <img src="{{ capsule.getVideoPreviewImageLink() }}" alt="video preview"> + {% endif %} </div> -{# {% if 'yout' in video_url %}#} -{# <img src="https://img.youtube.com/vi/{{ capsule.getVideoId() }}maxresdefault.jpg">#} -{# {% else %}#} -{# <img src="https://vumbnail.com/{{ capsule.getVideoId() }}.jpg">#} - {# <div class="m-4 ratio ratio-16x9">#} -{# <iframe#} -{# src="/capsule/preview/{{ capsule.getLinkPath() }}"#} -{# allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"#} -{# allowfullscreen>#} -{# </iframe>#} -{# </div>#} - <div class="d-flex flex-column justify-content-center m-2 pe-2"> <i class="fa-thin fa-gears"></i> diff --git a/templates/project/project_not_found.html.twig b/templates/project/project_not_found.html.twig index 0112bbe..05a7eba 100644 --- a/templates/project/project_not_found.html.twig +++ b/templates/project/project_not_found.html.twig @@ -1,13 +1,13 @@ {% extends 'layout.html.twig' %} {% block title %} - {{ 'project.not_found'|trans }} + {{ 'project.not_exist'|trans }} - {{ parent() }} {% endblock %} {% block body %} <div class="col-10 col-md-8 col-lg-6 col-xl-5 col-xxl-6 m-auto d-flex flex-row align-items-center justify-content-center mb-5 alert alert-warning" role="status"> - <span class="p-5">{{ 'project.not_found' | trans }}</span> + <span class="p-5">{{ 'project.not_exist' | trans }}</span> </div> {% endblock %} \ No newline at end of file diff --git a/tests/functional/ProjectControllerTest.php b/tests/functional/ProjectControllerTest.php index 0416828..7db3e68 100644 --- a/tests/functional/ProjectControllerTest.php +++ b/tests/functional/ProjectControllerTest.php @@ -86,7 +86,7 @@ class ProjectControllerTest extends WebTestCase $submit_button = $crawler->selectButton('Create a capsule'); $this->form = $submit_button->form(); - $video_url = "https://TestUrl"; + $video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley"; $this->form['create_capsule_form[name]'] = self::CAPSULE_NAME; $this->form['create_capsule_form[video_url]'] = $video_url; @@ -123,7 +123,7 @@ class ProjectControllerTest extends WebTestCase $video_url_in_xml_file = $video_node->getAttribute('url'); - $this->assertEquals($video_url, $video_url_in_xml_file); + $this->assertEquals($video_url, htmlspecialchars_decode($video_url_in_xml_file)); $this->assertSame(self::CAPSULE_NAME, $capsule_name_in_db); } @@ -293,7 +293,7 @@ class ProjectControllerTest extends WebTestCase $submit_button = $crawler->selectButton('Create a capsule'); $this->form = $submit_button->form(); - $video_url = "https://TestUrl"; + $video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley"; $this->form['create_capsule_form[name]'] = self::CAPSULE_NAME; $this->form['create_capsule_form[video_url]'] = $video_url; -- GitLab