Passed
Push — master ( bf69e0...103a68 )
by Dev
12:41
created

FeedDumpService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 4
b 0
f 0
nc 2
nop 7
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 6
rs 9.9332
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
use PiedWeb\CMSBundle\Repository\PageRepository;
10
11
/**
12
 * Inspired by https://github.com/eko/FeedBundle.
13
 */
14
class FeedDumpService
15
{
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    private $em;
20
21
    /**
22
     * @var \PiedWeb\CMSBundle\Service\PageCanonicalService
23
     */
24
    private $pc;
25
26
    /**
27
     * @var \Symfony\Component\Filesystem\Filesystem
28
     */
29
    private $filesystem;
30
31
    /**
32
     * @var \Twig_Environment
33
     */
34
    private $twig;
35
36
    /**
37
     * @var string
38
     */
39
    private $webDir;
40
41
    /**
42
     * @var string
43
     */
44
    private $page_class;
45
46
    private $locale;
47
    private $locales;
48
49
    public function __construct(
50
        EntityManager $em,
51
        Twig_Environment $twig,
52
        PageCanonicalService $pc,
53
        string $webDir,
54
        string $page_class,
55
        string $locale,
56
        string $locales
57
    ) {
58
        $this->em = $em;
59
        $this->pc = $pc;
60
        $this->filesystem = new Filesystem();
61
        $this->twig = $twig;
62
        $this->webDir = $webDir;
63
        $this->page_class = $page_class;
64
        $this->locale = $locale;
65
        $this->locales = explode('|', $locales);
66
        if (empty($this->locales)) {
67
            $this->locales = [$locale];
68
        }
69
    }
70
71
    /**
72
     * @throws \RuntimeException
73
     * @throws \LogicException
74
     */
75
    public function dump()
76
    {
77
        $this->dumpSitemap();
78
79
        foreach ($this->locales as $locale) {
80
            $this->dumpFeed($locale);
81
            $this->dumpSitemap($locale);
82
        }
83
    }
84
85
    public function postUpdate()
86
    {
87
        $this->dump();
88
    }
89
90
    protected function dumpFeed(string $locale)
91
    {
92
        $dump = $this->renderFeed($locale, 'feed'.($this->locale == $locale ? '' : '.'.$locale).'.xml');
93
        $filepath = $this->webDir.'/feed'.($this->locale == $locale ? '' : '.'.$locale).'.xml';
94
95
        $this->filesystem->dumpFile($filepath, $dump);
96
    }
97
98
    protected function dumpSitemap(?string $locale = null)
99
    {
100
        //$file = $this->webDir.'/sitemap'.($this->locale == $locale ? '' : '.'.$locale);
101
        $file = $this->webDir.'/sitemap'.(null === $locale ? '' : '.'.$locale);
102
103
        $pages = $this->getPages($locale);
104
        $this->filesystem->dumpFile($file.'.txt', $this->renderSitemapTxt($pages));
105
        $this->filesystem->dumpFile($file.'.xml', $this->renderSitemapXml($pages));
106
    }
107
108
    protected function getPageRepository() :PageRepository
109
    {
110
        return $this->em->getRepository($this->page_class);
111
    }
112
113
    protected function getPages(?string $locale, ?int $limit = null)
114
    {
115
        $qb = $this->getPageRepository()->getQueryToFindPublished('p');
116
        $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi2')
117
            ->setParameter('noi2', '%noindex%');
118
        $qb->andWhere('p.mainContent IS NULL OR p.mainContent NOT LIKE :noi')->setParameter('noi', 'Location:%');
119
120
        // avoid bc break and site with no locale configured
121
        if (null !== $locale) {
122
            $qb->andWhere(($this->locale == $locale ? 'p.locale IS NULL OR ' : '').'p.locale LIKE :locale')
123
            ->setParameter('locale', $locale);
124
        }
125
126
        if (null !== $limit) {
127
            $qb->setMaxResults($limit);
128
        }
129
130
        $pages = $qb->getQuery()->getResult();
131
132
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
133
        //exit('feed updated');
134
135
        return $pages;
136
    }
137
138
    protected function renderFeed(string $locale, string $feedUri)
139
    {
140
        // assuming yoy name it with your locale identifier
141
        $LocaleHomepage = $this->em->getRepository($this->page_class)->findOneBy(['slug'=>$locale]);
142
        $page = $LocaleHomepage ?? $this->em->getRepository($this->page_class)->findOneBy(['slug'=>'homepage']);
143
144
        return $this->twig->render('@PiedWebCMS/page/rss.xml.twig', [
145
            'pages' => $this->getPages($locale, 5),
146
            'page' => $page,
147
            'feedUri' => $feedUri,
148
        ]);
149
    }
150
151
    protected function renderSitemapTxt($pages)
152
    {
153
        return $this->twig->render('@PiedWebCMS/page/sitemap.txt.twig', ['pages' => $pages]);
154
    }
155
156
    protected function renderSitemapXml($pages)
157
    {
158
        return $this->twig->render('@PiedWebCMS/page/sitemap.xml.twig', ['pages' => $pages]);
159
    }
160
}
161