Completed
Push — master ( be1a06...838f20 )
by Dev
07:19
created

FeedDumpService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 \Symfony\Component\Filesystem\Filesystem
22
     */
23
    private $filesystem;
24
25
    /**
26
     * @var \Twig_Environment
27
     */
28
    private $twig;
29
30
    /**
31
     * @var string
32
     */
33
    private $webDir;
34
35
    /**
36
     * @var string
37
     */
38
    private $page_class;
39
40
    public function __construct(EntityManager $em, Twig_Environment $twig, string $webDir, string $page_class)
41
    {
42
        $this->em = $em;
43
        $this->filesystem = new Filesystem();
44
        $this->twig = $twig;
45
        $this->webDir = $webDir;
46
        $this->page_class = $page_class;
47
    }
48
49
    /**
50
     * @throws \RuntimeException
51
     * @throws \LogicException
52
     */
53
    public function dump()
54
    {
55
        if (!method_exists($this->filesystem, 'dumpFile')) {
56
            throw new \RuntimeException('Method dumpFile() is not available on your Filesystem component version, you should upgrade it.');
57
        }
58
59
        $this->dumpFeed();
60
        $this->dumpSitemap();
61
    }
62
63
    protected function dumpFeed()
64
    {
65
        $dump = $this->renderFeed();
66
        $filepath = $this->webDir.'/feed.xml';
67
68
        $this->filesystem->dumpFile($filepath, $dump);
69
    }
70
71
    protected function dumpSitemap()
72
    {
73
        $pages = $this->getPages();
74
        $this->filesystem->dumpFile($this->webDir.'/sitemap.txt', $this->renderSitemapTxt($pages));
75
        $this->filesystem->dumpFile($this->webDir.'/sitemap.xml', $this->renderSitemapXml($pages));
76
    }
77
78
    protected function getPages(?int $limit = null)
79
    {
80
        $qb = $this->em->getRepository($this->page_class)->getQueryToFindPublished('p');
81
        $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi')->setParameter('noi', '%no-index%');
82
        $qb->andWhere('p.mainContent NOT LIKE :noi')->setParameter('noi', 'Location:%');
83
84
        if ($limit !== null) {
85
            $qb->setMaxResults($limit);
86
        }
87
88
        return $qb->getQuery()->getResult();
89
    }
90
91
    protected function renderFeed()
92
    {
93
        return $this->twig->render('@PiedWebCMS/page/rss.xml.twig', ['pages' => $this->getPages(5)]);
94
    }
95
96
    protected function renderSitemapTxt($pages)
97
    {
98
        return $this->twig->render('@PiedWebCMS/page/sitemap.txt.twig', ['pages' => $pages]);
99
    }
100
101
    protected function renderSitemapXml($pages)
102
    {
103
        return $this->twig->render('@PiedWebCMS/page/sitemap.xml.twig', ['pages' => $pages]);
104
    }
105
}
106