1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PODEntender\Infrastructure\Application\StaticSite; |
4
|
|
|
|
5
|
|
|
use PODEntender\Application\Service\Post\FetchExistentCategoryNames; |
6
|
|
|
use PODEntender\Application\Service\Post\FetchLatestEpisodes; |
7
|
|
|
use PODEntender\Application\Service\Post\FetchRecommendationsForPost; |
8
|
|
|
use PODEntender\Infrastructure\Domain\Factory\JigsawPostFactory; |
9
|
|
|
use TightenCo\Jigsaw\Jigsaw; |
10
|
|
|
use TightenCo\Jigsaw\PageVariable; |
11
|
|
|
|
12
|
|
|
class JigsawDecoratePagesAfterCollections implements JigsawEventHandler |
13
|
|
|
{ |
14
|
|
|
const NUMBER_OF_RECOMMENDED_EPISODES = 3; |
15
|
|
|
|
16
|
|
|
public function handle(Jigsaw $jigsaw): void |
17
|
|
|
{ |
18
|
|
|
$factory = $jigsaw->app->make(JigsawPostFactory::class); |
19
|
|
|
$recommendationsService = $jigsaw->app->make(FetchRecommendationsForPost::class); |
20
|
|
|
$latestEpisodeService = $jigsaw->app->make(FetchLatestEpisodes::class); |
21
|
|
|
$categoryNames = $jigsaw->app->make(FetchExistentCategoryNames::class)->execute(); |
22
|
|
|
|
23
|
|
|
$latestEpisodesPerCategory = []; |
24
|
|
|
foreach ($categoryNames as $categoryName) { |
25
|
|
|
$latestEpisodesPerCategory[$categoryName] = $latestEpisodeService->execute(3, $categoryName); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$jigsaw->setConfig('lastEpisode', $latestEpisodeService->execute(1, null)->first()); |
29
|
|
|
$jigsaw->setConfig('latestEpisodesPerCategory', $latestEpisodesPerCategory); |
30
|
|
|
|
31
|
|
|
$jigsaw->getCollection('episodes') |
32
|
|
|
->each(function (PageVariable $page) use ($factory, $recommendationsService) { |
33
|
|
|
// AudioEpisode |
34
|
|
|
$page->audioEpisode = $factory->newAudioEpisodeFromPageVariable($page); |
35
|
|
|
|
36
|
|
|
// Recommended episodes |
37
|
|
|
$recommendedEpisodes = $recommendationsService->execute( |
38
|
|
|
$page->audioEpisode, |
39
|
|
|
self::NUMBER_OF_RECOMMENDED_EPISODES |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$page->recommendations = $recommendedEpisodes; |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|