Completed
Push — master ( 521616...78d52b )
by Benjamin
02:26
created

SitemapListener::populateSitemap()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 44
rs 6.7272
cc 7
eloc 26
nc 6
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Listener;
4
5
use Doctrine\ORM\EntityManager;
6
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
7
use Presta\SitemapBundle\Service\SitemapListenerInterface;
8
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
9
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
10
use Symfony\Component\Routing\RouterInterface;
11
12
class SitemapListener implements SitemapListenerInterface
13
{
14
    private $entityManager;
15
    private $router;
16
    private $defaultLocale;
17
    private $locales;
18
    private $baseUrl;
19
20
    public function __construct(RouterInterface $router, EntityManager $entityManager, $defaultLocale, $locales, $baseUrl)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
21
    {
22
        $this->entityManager = $entityManager;
23
        $this->router = $router;
24
        $this->defaultLocale = $defaultLocale;
25
        $this->locales = $locales;
26
        $this->baseUrl = $baseUrl;
27
    }
28
29
    public function populateSitemap(SitemapPopulateEvent $event)
30
    {
31
        $section = $event->getSection();
32
33
        if (is_null($section) || $section == 'cms') {
34
            $nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node');
35
            $pages = $nodeRepository->findAllWithLocale($this->defaultLocale);
36
37
            foreach ($pages as $page) {
38
                $url = $this->router->generate('alpixel_cms', [
39
                    'slug' => $page->getSlug(),
40
                    '_locale' => $page->getLocale(),
41
                ]);
42
43
                $url = new UrlConcrete(
44
                    $url,
45
                    $page->getDateUpdated(),
46
                    UrlConcrete::CHANGEFREQ_MONTHLY,
47
                    .7
48
                );
49
50
                $urlLang = new GoogleMultilangUrlDecorator($url);
51
                foreach ($this->locales as $locale) {
52
                    if ($locale !== $this->defaultLocale) {
53
                        $translatedPage = $nodeRepository->findTranslation($page, $locale);
54
55
                        if($translatedPage !== null) {
56
                            $url = $this->router->generate('alpixel_cms', [
57
                                'slug' => $translatedPage->getSlug(),
58
                                '_locale' => $translatedPage->getLocale(),
59
                            ]);
60
61
                            $urlLang->addLink($url, $locale);
62
                        }
63
                    }
64
                }
65
66
                $event->getGenerator()->addUrl(
67
                    $urlLang,
68
                    'cms'
69
                );
70
            }
71
        }
72
    }
73
}
74