Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

FeedDumpService::getPages()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 3
eloc 10
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 12
cp 0
crap 12
rs 9.9332
c 5
b 1
f 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Filesystem\Filesystem;
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
    private $locale;
46
    private $locales;
47
48
    public function __construct(
49
        EntityManager $em,
50
        Twig_Environment $twig,
51
        PageCanonicalService $pc,
52
        string $webDir,
53
        string $page_class,
54
        string $locale,
55
        string $locales
56
    ) {
57
        $this->em = $em;
58
        $this->pc = $pc;
59
        $this->filesystem = new Filesystem();
60
        $this->twig = $twig;
61
        $this->webDir = $webDir;
62
        $this->page_class = $page_class;
63
        $this->locale = $locale;
64
        $this->locales = explode('|', $locales);
65
        if (empty($this->locales)) {
66
            $this->locales = [$locale];
67
        }
68
    }
69
70
    /**
71
     * @throws \RuntimeException
72
     * @throws \LogicException
73
     */
74
    public function dump()
75
    {
76
        foreach ($this->locales as $locale) {
77
            $this->dumpFeed($locale);
78
            $this->dumpSitemap($locale);
79
        }
80
    }
81
82
    public function postUpdate()
83
    {
84
        $this->dump();
85
    }
86
87
    protected function dumpFeed(string $locale)
88
    {
89
        $dump = $this->renderFeed($locale);
90
        $filepath = $this->webDir.'/feed'.($this->locale == $locale ? '' : '.'.$locale).'.xml';
91
92
        $this->filesystem->dumpFile($filepath, $dump);
93
    }
94
95
    protected function dumpSitemap(string $locale)
96
    {
97
        $file = $this->webDir.'/sitemap'.($this->locale == $locale ? '' : '.'.$locale);
98
99
        $pages = $this->getPages($locale);
100
        $this->filesystem->dumpFile($file.'.txt', $this->renderSitemapTxt($pages));
101
        $this->filesystem->dumpFile($file.'.xml', $this->renderSitemapXml($pages));
102
    }
103
104
    protected function getPages(string $locale, ?int $limit = null)
105
    {
106
        $qb = $this->em->getRepository($this->page_class)->getQueryToFindPublished('p');
1 ignored issue
show
Bug introduced by
The method getQueryToFindPublished() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ORM\EntityRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $qb = $this->em->getRepository($this->page_class)->/** @scrutinizer ignore-call */ getQueryToFindPublished('p');
Loading history...
107
        $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi2')
108
            ->setParameter('noi2', '%noindex%');
109
        $qb->andWhere('p.mainContent IS NULL OR p.mainContent NOT LIKE :noi')->setParameter('noi', 'Location:%');
110
111
        // avoid bc break and site with no locale configured
112
        $qb->andWhere(($this->locale == $locale ? 'p.locale IS NULL OR ' : '').'p.locale LIKE :locale')
113
            ->setParameter('locale', $locale);
114
115
        if (null !== $limit) {
116
            $qb->setMaxResults($limit);
117
        }
118
119
        $pages = $qb->getQuery()->getResult();
120
121
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
122
        //exit('feed updated');
123
124
        return $pages;
125
    }
126
127
    protected function renderFeed(string $locale)
128
    {
129
        return $this->twig->render('@PiedWebCMS/page/rss.xml.twig', ['pages' => $this->getPages($locale, 5)]);
130
    }
131
132
    protected function renderSitemapTxt($pages)
133
    {
134
        return $this->twig->render('@PiedWebCMS/page/sitemap.txt.twig', ['pages' => $pages]);
135
    }
136
137
    protected function renderSitemapXml($pages)
138
    {
139
        return $this->twig->render('@PiedWebCMS/page/sitemap.xml.twig', ['pages' => $pages]);
140
    }
141
}
142