Passed
Push — master ( 849580...062664 )
by Níckolas Daniel
04:51
created

GenerateRssFeed   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 44
dl 0
loc 60
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 47 2
1
<?php
2
3
namespace PODEntender\Application\Service\FileProcessing;
4
5
use PODEntender\Domain\Model\FileProcessing\OutputFile;
6
use PODEntender\Domain\Model\FileProcessing\RssFeedConfiguration;
7
use PODEntender\Domain\Model\Post\AudioEpisode;
8
use PODEntender\Domain\Model\Post\PostRepository;
9
use PODEntender\Domain\Model\Feed\ChannelBuilder;
10
use PODEntender\Domain\Model\Feed\FeedBuilder;
11
use PODEntender\Domain\Model\Feed\ItemBuilder;
12
13
class GenerateRssFeed
14
{
15
    /** @var FeedBuilder */
16
    private $builder;
17
18
    /** @var PostRepository */
19
    private $postsRepository;
20
21 4
    public function __construct(FeedBuilder $builder, PostRepository $postsRepository) {
22 4
        $this->builder = $builder;
23 4
        $this->postsRepository = $postsRepository;
24 4
    }
25
26 4
    public function execute(RssFeedConfiguration $configuration): OutputFile
27
    {
28 4
        $builder = $this->builder
29 4
            ->channel()
30 4
            ->title($configuration->title())
31 4
            ->subtitle($configuration->subtitle())
32 4
            ->description($configuration->description())
33 4
            ->lastBuildDate($configuration->lastBuildDate()->format(ChannelBuilder::DATE_FORMAT))
34 4
            ->language($configuration->language())
35 4
            ->generator($configuration->generator())
36 4
            ->managingEditor($configuration->managingEditor())
37 4
            ->imageUrl($configuration->imageUrl())
38 4
            ->url($configuration->url())
39 4
            ->feedUrl($configuration->feedUrl())
40 4
            ->author($configuration->author())
41 4
            ->explicit($configuration->explicit())
42 4
            ->type($configuration->type())
43 4
            ->email($configuration->email())
44 4
            ->category($configuration->category());
45
46 4
        $episodes = $this->postsRepository->withAudio()
47
            ->sortByDesc(function (AudioEpisode $episode) {
48 1
                return $episode->createdAt();
49 4
            });
50
51
        /** @var AudioEpisode $episode */
52 4
        foreach ($episodes as $episode) {
53 1
            $builder->addItem()
54 1
                ->guid($episode->guid())
55 1
                ->title($episode->title())
56 1
                ->subtitle($episode->description())
57 1
                ->image($episode->audioCover())
58 1
                ->description($episode->description())
59 1
                ->author($episode->author())
60 1
                ->link($episode->url())
61 1
                ->comments($episode->url())
62 1
                ->pubDate($episode->updatedAt()->format(ItemBuilder::DATE_FORMAT))
63 1
                ->explicit($configuration->explicit())
64 1
                ->duration($episode->duration())
65 1
                ->addCategory($episode->category())
66 1
                ->addEnclosure()
67 1
                ->url($episode->audioUrl())
68 1
                ->length('0')
69 1
                ->type('audio/mpeg');
70
        }
71
72 4
        return new OutputFile($configuration->outputFilepath(), $builder->close()->toXml());
73
    }
74
}
75