Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config/packages/cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
framework:
cache:
pools:
# Bloc « Dernières vidéos » de la home : une heure suffit, une vidéo est
# publiée quelques jours après l'événement, pas à la minute près.
cache.latest_videos:
adapter: cache.app
default_lifetime: 3600

when@test:
framework:
cache:
pools:
# Pas de cache persistant en test : chaque scénario Behat recharge
# la base et peut changer la date courante.
cache.latest_videos:
adapter: cache.adapter.array
88 changes: 88 additions & 0 deletions db/seeds/TalksWithVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

use Phinx\Seed\AbstractSeed;

/**
* Talks d'une édition passée dédiée au bloc « Dernières vidéos » de la home :
* des talks publiés avec vidéo, dont les créneaux ne sont pas dans l'ordre des identifiants,
* et des talks qui ne doivent pas y apparaître (clinique, sans vidéo, non planifié,
* créneau pas encore terminé).
* Édition à part pour ne pas changer les données des autres éditions.
*/
class TalksWithVideo extends AbstractSeed
{
public const ID_EVENT = 4;

#[\Override]
public function getDependencies(): array
{
// Event et Session vident les tables remplies ici.
return ['Event', 'Session'];
}

public function run(): void
{
$this->table('afup_forum')
->insert([
'id' => self::ID_EVENT,
'titre' => 'Rencontres vidéo 2019',
'path' => 'rencontres-video-2019',
'nb_places' => 100,
'date_debut' => '2019-05-23',
'date_fin' => '2019-05-24',
'annee' => 2019,
])
->save();

$notEndedYet = new \DateTime('+10 days 10:00:00')->format('Y-m-d H:i:s');

$talks = [
100 => ['titre' => 'PHP 8 en production', 'fin' => '2019-05-24 10:00:00'],
101 => ['titre' => 'Les fibers expliquées', 'fin' => '2019-05-24 17:00:00'],
102 => ['titre' => 'Sécuriser une API', 'fin' => '2019-05-24 11:00:00'],
103 => ['titre' => 'Domain-Driven Design au quotidien', 'fin' => '2019-05-24 15:00:00'],
104 => ['titre' => 'Le plus ancien talk en vidéo', 'fin' => '2019-05-23 10:00:00'],
105 => ['titre' => 'Une clinique filmée', 'fin' => '2019-05-24 18:00:00', 'genre' => 6],
106 => ['titre' => 'Un talk sans vidéo', 'fin' => '2019-05-24 18:00:00', 'youtube_id' => ''],
107 => ['titre' => 'Un talk non planifié', 'fin' => '2019-05-24 18:00:00', 'plannifie' => 0],
108 => ['titre' => 'Un talk au créneau pas terminé', 'fin' => $notEndedYet],
];

$sessions = [];
$speakers = [];
$plannings = [];
foreach ($talks as $sessionId => $talk) {
$sessions[] = [
'session_id' => $sessionId,
'id_forum' => self::ID_EVENT,
'date_soumission' => '2019-01-15',
'titre' => $talk['titre'],
'abstract' => 'Résumé du talk « ' . $talk['titre'] . ' ».',
'genre' => $talk['genre'] ?? 1,
'skill' => 0,
'plannifie' => $talk['plannifie'] ?? 1,
'youtube_id' => $talk['youtube_id'] ?? 'video' . $sessionId,
'language_code' => 'fr',
];
$speakers[] = [
'session_id' => $sessionId,
'conferencier_id' => Conferenciers::ID_CONFERENCIER,
];
$end = new \DateTime($talk['fin']);
$plannings[] = [
'id' => $sessionId,
'id_session' => $sessionId,
'debut' => (clone $end)->modify('-40 minutes')->format('U'),
'fin' => $end->format('U'),
'id_forum' => self::ID_EVENT,
'keynote' => 0,
];
}

$this->table('afup_sessions')->insert($sessions)->save();
$this->table('afup_conferenciers_sessions')->insert($speakers)->save();
$this->table('afup_forum_planning')->insert($plannings)->save();
}
}
3 changes: 3 additions & 0 deletions sources/AppBundle/Controller/Website/Global/HomeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AppBundle\Event\Entity\Repository\MeetupRepository;
use AppBundle\Event\Model\Repository\EventRepository;
use AppBundle\Event\Talk\LatestVideos;
use AppBundle\Site\ArticleImageStorage;
use AppBundle\Site\Entity\Repository\ArticleRepository;
use AppBundle\Twig\ViewRenderer;
Expand All @@ -20,6 +21,7 @@ public function __construct(
private readonly MeetupRepository $meetupRepository,
private readonly EventRepository $eventRepository,
private readonly ArticleImageStorage $articleImageStorage,
private readonly LatestVideos $latestVideos,
) {}

public function __invoke(): Response
Expand All @@ -32,6 +34,7 @@ public function __invoke(): Response
'meetups' => $meetups,
'lastArticleImageUrl' => $this->articleImageStorage->getUrl($articles[0]) ?? 'images/article-default-image.svg',
'currentEvent' => $this->eventRepository->getMostRecentEvent(),
'latestVideos' => $this->latestVideos->get(),
]);
}
}
25 changes: 25 additions & 0 deletions sources/AppBundle/Event/Talk/LatestVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

/**
* Talk publié avec vidéo, tel qu'affiché dans le bloc « Dernières vidéos » de la home.
* Objet simple pour pouvoir être mis en cache.
*/
final readonly class LatestVideo
{
public function __construct(
public int $talkId,
public string $slug,
public string $title,
public string $youtubeId,
public string $eventTitle,
) {}

public function getThumbnailUrl(): string
{
return 'https://img.youtube.com/vi/' . rawurlencode($this->youtubeId) . '/hqdefault.jpg';
}
}
64 changes: 64 additions & 0 deletions sources/AppBundle/Event/Talk/LatestVideoRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

use AppBundle\Event\Model\Talk;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;

final readonly class LatestVideoRepository
{
public function __construct(private Connection $connection) {}

/**
* Derniers talks publiés avec une vidéo, du créneau terminé le plus récent au plus ancien.
* « Publié » reprend les règles de la page talk : talk planifié, affiché dans l'historique
* (ni clinique, ni introduction des speakers), date de publication passée ou vide,
* et tous ses créneaux de planning terminés.
*
* @return list<LatestVideo>
*/
public function getLatest(\DateTimeInterface $now, int $limit): array
{
/** @var list<array{session_id: int|string, titre: string, youtube_id: string, event_title: string}> $rows */
$rows = $this->connection->fetchAllAssociative(
'SELECT talk.session_id, talk.titre, talk.youtube_id, event.titre AS event_title
FROM afup_sessions AS talk
INNER JOIN (
SELECT id_session, MAX(fin) AS fin
FROM afup_forum_planning
GROUP BY id_session
) AS planning ON planning.id_session = talk.session_id
INNER JOIN afup_forum AS event ON event.id = talk.id_forum
WHERE talk.plannifie = 1
AND talk.genre NOT IN (:clinic, :speakerIntroductions)
AND LENGTH(talk.youtube_id) > 0
AND (talk.date_publication IS NULL OR talk.date_publication < :now)
AND planning.fin <= :nowTimestamp
ORDER BY planning.fin DESC, talk.session_id DESC
LIMIT :limit',
[
'clinic' => Talk::TYPE_CLINIC,
'speakerIntroductions' => Talk::TYPE_SPEAKER_INTRODUCTIONS,
'now' => $now->format('Y-m-d H:i:s'),
'nowTimestamp' => $now->getTimestamp(),
'limit' => $limit,
],
['limit' => ParameterType::INTEGER],
);

return array_map(
static fn(array $row): LatestVideo => new LatestVideo(
(int) $row['session_id'],
// Slug construit par le modèle, pour rester identique à celui de la page talk
(new Talk())->setTitle($row['titre'])->getSlug(),
$row['titre'],
$row['youtube_id'],
$row['event_title'],
),
$rows,
);
}
}
41 changes: 41 additions & 0 deletions sources/AppBundle/Event/Talk/LatestVideos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final readonly class LatestVideos
{
public const int LIMIT = 6;
private const string CACHE_KEY = 'latest_videos';

public function __construct(
private LatestVideoRepository $latestVideoRepository,
private ClockInterface $clock,
// Durée de vie du cache définie sur le pool (config/packages/cache.yaml).
#[Autowire('@cache.latest_videos')]
private CacheItemPoolInterface $cache,
) {}

/**
* @return list<LatestVideo>
*/
public function get(): array
{
$cacheItem = $this->cache->getItem(self::CACHE_KEY);

if (!$cacheItem->isHit()) {
$cacheItem->set($this->latestVideoRepository->getLatest($this->clock->now(), self::LIMIT));
$this->cache->save($cacheItem);
}

/** @var list<LatestVideo> $videos */
$videos = $cacheItem->get();

return $videos;
}
}
35 changes: 35 additions & 0 deletions templates/site/home.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,41 @@
</div>
</section>

{% if latestVideos is not empty %}
<section id="dernieres-videos" class="w-full flex flex-col gap-5">
<h2 class="text-afup-800 font-titre text-2xl sm:text-4xl border-b-2 border-neutre-300 pb-3">
Dernières vidéos
</h2>

<ul class="grid grid-cols-1 sm:grid-cols-3 gap-5">
{% for video in latestVideos %}
<li>
<twig:Card class="p-0 h-full overflow-hidden">
<a href="{{ path('talks_show', {id: video.talkId, slug: video.slug}) }}"
class="flex flex-col h-full group">
<img src="{{ video.thumbnailUrl }}"
alt=""
loading="lazy"
class="w-full aspect-video object-cover bg-neutre-200" />

<span class="flex flex-col gap-2 p-4">
<span class="font-titre text-lg leading-snug text-afup-800 group-hover:underline">
{{ video.title }}
</span>
<span class="text-sm text-neutre-400">{{ video.eventTitle }}</span>
</span>
</a>
</twig:Card>
</li>
{% endfor %}
</ul>

<twig:Button variant="tertiary" as="a" href="{{ path('talks_list') }}" class="self-start flex gap-2">
Toutes les vidéos <twig:UX:Icon name="mingcute:arrow-right-fill" class="size-5" />
</twig:Button>
</section>
{% endif %}

<section class="flex flex-col sm:grid grid-flow-col grid-cols-2 gap-10">
<twig:Card class="row-span-1">
<twig:Card:Title size="big">Dans les antennes</twig:Card:Title>
Expand Down
28 changes: 28 additions & 0 deletions tests/behat/features/PublicSite/Home.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,31 @@ Feature: Site Public - Home
When I am on "/home"
Then the response status code should be 301
And the response header "Location" should match "#^(https?://[^/]+)?/$#"

@reloadDbWithTestData
Scenario: Bloc des dernières vidéos
Given I am on the homepage
Then I should see "Dernières vidéos" in the "#dernieres-videos h2" element
# Les 6 talks publiés avec vidéo dont le créneau s'est terminé le plus récemment, du plus récent au plus ancien
And I should see 6 "#dernieres-videos li" elements
And I should see "REST ou GraphQL ? Exemples illustrés avec Symfony et API Platform" in the "#dernieres-videos li:nth-child(1)" element
And the "#dernieres-videos li:nth-child(1)" element should contain "href=\"/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform\""
And the "#dernieres-videos li:nth-child(1)" element should contain "https://img.youtube.com/vi/QhAToFl_Omo/hqdefault.jpg"
And I should see "forum" in the "#dernieres-videos li:nth-child(1)" element
And I should see "Jouons tous ensemble à un petit jeu" in the "#dernieres-videos li:nth-child(2)" element
And I should see "Les fibers expliquées" in the "#dernieres-videos li:nth-child(3)" element
And the "#dernieres-videos li:nth-child(3)" element should contain "href=\"/talks/101-les-fibers-expliquees\""
And I should see "Rencontres vidéo 2019" in the "#dernieres-videos li:nth-child(3)" element
And I should see "Domain-Driven Design au quotidien" in the "#dernieres-videos li:nth-child(4)" element
And I should see "Sécuriser une API" in the "#dernieres-videos li:nth-child(5)" element
And I should see "PHP 8 en production" in the "#dernieres-videos li:nth-child(6)" element
# Au-delà des 6 plus récents
And I should not see "Le plus ancien talk en vidéo" in the "#dernieres-videos" element
# Talks non publiés ou sans vidéo
And I should not see "Révolutionons PHP" in the "#dernieres-videos" element
And I should not see "Une clinique filmée" in the "#dernieres-videos" element
And I should not see "Un talk sans vidéo" in the "#dernieres-videos" element
And I should not see "Un talk non planifié" in the "#dernieres-videos" element
And I should not see "Un talk au créneau pas terminé" in the "#dernieres-videos" element
When I follow "Toutes les vidéos"
Then the url should match "/talks/$"
41 changes: 41 additions & 0 deletions tests/unit/AppBundle/Event/Talk/LatestVideosTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace AppBundle\Tests\Event\Talk;

use AppBundle\Event\Talk\LatestVideo;
use AppBundle\Event\Talk\LatestVideoRepository;
use AppBundle\Event\Talk\LatestVideos;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Clock\MockClock;

final class LatestVideosTest extends TestCase
{
public function testMapsRowsToVideosAndCachesTheResult(): void
{
$connection = $this->createMock(Connection::class);
$connection
->expects(self::once())
->method('fetchAllAssociative')
->willReturn([[
'session_id' => '42',
'titre' => 'Les fibers expliquées',
'youtube_id' => 'abc123',
'event_title' => 'Forum PHP 2026',
]]);

$latestVideos = new LatestVideos(
new LatestVideoRepository($connection),
new MockClock(new \DateTimeImmutable('2026-11-17 12:00:00')),
new ArrayAdapter(),
);

$expected = [new LatestVideo(42, 'les-fibers-expliquees', 'Les fibers expliquées', 'abc123', 'Forum PHP 2026')];
self::assertEquals($expected, $latestVideos->get());
// Le second appel est servi par le cache : la base n'est interrogée qu'une fois.
self::assertEquals($expected, $latestVideos->get());
}
}
Loading