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

FeedDumpService::dumpFeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
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