Completed
Pull Request — master (#166)
by Alex
06:00
created

Provider::getPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
# src/Feed/provider.php
4
5
namespace App\Feed;
6
7
// this part assumes that you published "Post" entities
8
use App\Entity\Post;
9
use App\Repository\PostRepository;
10
11
// All you really need to create a feed
12
use Debril\RssAtomBundle\Provider\FeedContentProviderInterface;
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use FeedIo\Feed;
15
use FeedIo\Feed\Node\Category;
16
use FeedIo\FeedInterface;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\Routing\Router;
19
20
class Provider implements FeedContentProviderInterface
21
{
22
23
    protected $logger;
24
25
    protected $registry;
26
27
    protected $router;
28
29
    /**
30
     * Provider constructor.
31
     * @param LoggerInterface $logger
32
     * @param Registry $registry
33
     * @param Router $router
34
     */
35
    public function __construct(LoggerInterface $logger, Registry $registry, Router $router)
36
    {
37
        $this->logger = $logger;
38
        $this->registry = $registry;
39
        $this->router = $router;
40
    }
41
42
    /**
43
     * @param array $options
44
     * @return FeedInterface
45
     */
46
    public function getFeedContent(array $options) : FeedInterface
47
    {
48
        $feed = new Feed();
49
        $feed->setTitle('Feed Title')
50
            ->setLink('Feed URL')
51
            ->setDescription('Feed description')
52
            ->setPublicId('Feed ID');
53
54
        $lastPostPublicationDate = null;
55
56
        $posts = $this->getPosts();
57
58
        /**
59
         * @var \App\Entity\Post $post
60
         */
61
        foreach ($posts as $post) {
62
            $lastPostPublicationDate = is_null($lastPostPublicationDate) ? $post->getPublicationDate():$lastPostPublicationDate;
63
64
            $item = new Feed\Item();
65
            $item->setTitle($post->getTitle());
66
67
            $category = new Category();
68
            $category->setLabel($post->getCategory());
69
            $item->addCategory($category);
70
71
            $item->setLastModified($post->getPublicationDate());
72
73
            // ... and all the stuff about content, public id, etc ...
74
75
            $feed->add($item);
76
        }
77
78
        // if the publication date is still empty, set it the current Date
79
        $lastPostPublicationDate = is_null($lastPostPublicationDate) ? new \DateTime():$lastPostPublicationDate;
80
        $feed->setLastModified($lastPostPublicationDate);
81
82
        return $feed;
83
    }
84
85
    /**
86
     * You'll need to code this
87
     * @return array
88
     */
89
    protected function getPosts()
90
    {
91
        return [];
92
    }
93
94
}
95