Completed
Push — master ( 7c1165...cb0569 )
by Dev
13:37
created

FeedDumpService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
eloc 33
dl 0
loc 102
ccs 0
cts 56
cp 0
rs 10
c 8
b 1
f 1
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 4 1
A __construct() 0 13 1
A dumpFeed() 0 6 1
A dumpSitemap() 0 5 1
A renderFeed() 0 3 1
A renderSitemapXml() 0 3 1
A renderSitemapTxt() 0 3 1
A getPages() 0 15 2
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Doctrine\ORM\EntityManager;
8
use Twig_Environment;
9
10
/**
11
 * Inspired by https://github.com/eko/FeedBundle.
12
 */
13
class FeedDumpService
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * @var \PiedWeb\CMSBundle\Service\PageCanonicalService
22
     */
23
    private $pc;
24
25
    /**
26
     * @var \Symfony\Component\Filesystem\Filesystem
27
     */
28
    private $filesystem;
29
30
    /**
31
     * @var \Twig_Environment
32
     */
33
    private $twig;
34
35
    /**
36
     * @var string
37
     */
38
    private $webDir;
39
40
    /**
41
     * @var string
42
     */
43
    private $page_class;
44
45
    public function __construct(
46
        EntityManager $em,
47
        Twig_Environment $twig,
48
        PageCanonicalService $pc,
49
        string $webDir,
50
        string $page_class
51
    ) {
52
        $this->em = $em;
53
        $this->pc = $pc;
54
        $this->filesystem = new Filesystem();
55
        $this->twig = $twig;
56
        $this->webDir = $webDir;
57
        $this->page_class = $page_class;
58
    }
59
60
    /**
61
     * @throws \RuntimeException
62
     * @throws \LogicException
63
     */
64
    public function dump()
65
    {
66
        $this->dumpFeed();
67
        $this->dumpSitemap();
68
    }
69
70
    protected function dumpFeed()
71
    {
72
        $dump = $this->renderFeed();
73
        $filepath = $this->webDir.'/feed.xml';
74
75
        $this->filesystem->dumpFile($filepath, $dump);
76
    }
77
78
    protected function dumpSitemap()
79
    {
80
        $pages = $this->getPages();
81
        $this->filesystem->dumpFile($this->webDir.'/sitemap.txt', $this->renderSitemapTxt($pages));
82
        $this->filesystem->dumpFile($this->webDir.'/sitemap.xml', $this->renderSitemapXml($pages));
83
    }
84
85
    protected function getPages(?int $limit = null)
86
    {
87
        $qb = $this->em->getRepository($this->page_class)->getQueryToFindPublished('p');
88
        $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots != :noi OR p.metaRobots NOT LIKE :noi2')
89
           ->setParameter('noi', 'noindex')
90
           ->setParameter('noi2', '%noindex%');
91
        $qb->andWhere('p.mainContent NOT LIKE :noi')->setParameter('noi', 'Location:%');
92
93
        if (null !== $limit) {
94
            $qb->setMaxResults($limit);
95
        }
96
97
        $pages = $qb->getQuery()->getResult();
98
99
        return $pages;
100
    }
101
102
    protected function renderFeed()
103
    {
104
        return $this->twig->render('@PiedWebCMS/page/rss.xml.twig', ['pages' => $this->getPages(5)]);
105
    }
106
107
    protected function renderSitemapTxt($pages)
108
    {
109
        return $this->twig->render('@PiedWebCMS/page/sitemap.txt.twig', ['pages' => $pages]);
110
    }
111
112
    protected function renderSitemapXml($pages)
113
    {
114
        return $this->twig->render('@PiedWebCMS/page/sitemap.xml.twig', ['pages' => $pages]);
115
    }
116
}
117