SitemapListener::populateSitemap()   C
last analyzed

Complexity

Conditions 11
Paths 15

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 6.6824
c 0
b 0
f 0
cc 11
nc 15
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Generator\UrlGeneratorInterface;
11
use Symfony\Component\Routing\RouterInterface;
12
13
class SitemapListener implements SitemapListenerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Presta\SitemapBundle\Ser...itemapListenerInterface has been deprecated with message: This interface has been deprecated in favor of Symfony standard event listener and subscriber. Please see documentation if you are in trouble. To be removed in next major release : 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
{
15
    private $entityManager;
16
    private $router;
17
    private $defaultLocale;
18
    private $locales;
19
    private $baseUrl;
20
    private $contentTypes;
21
22
    public function __construct(
23
        RouterInterface $router,
24
        EntityManager $entityManager,
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...
25
        $defaultLocale,
26
        $locales,
27
        $baseUrl,
28
        $contentTypes
29
    ) {
30
        $this->entityManager = $entityManager;
31
        $this->router = $router;
32
        $this->defaultLocale = $defaultLocale;
33
        $this->locales = $locales;
34
        $this->baseUrl = $baseUrl;
35
        $this->contentTypes = $contentTypes;
36
    }
37
38
    public function populateSitemap(SitemapPopulateEvent $event)
39
    {
40
        $section = $event->getSection();
41
42
        if (is_null($section) || $section == 'cms') {
43
            foreach ($this->locales as $locale) {
44
                $nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node');
45
                $pages = $nodeRepository->findAllWithLocale($locale);
0 ignored issues
show
Bug introduced by
The method findAllWithLocale() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findAll()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
47
                foreach ($pages as $page) {
48
                    $hasController = true;
49
                    foreach ($this->contentTypes as $contentType) {
50
                        if (get_class($page) == $contentType['class'] && $contentType['controller'] === null) {
51
                            $hasController = false;
52
                        }
53
                    }
54
55
                    if ($hasController === false) {
56
                        continue;
57
                    }
58
59
                    $url = $this->router->generate(
60
                        'alpixel_cms',
61
                        [
62
                            'slug'    => $page->getSlug(),
63
                            '_locale' => $page->getLocale(),
64
                        ],
65
                        UrlGeneratorInterface::ABSOLUTE_URL
66
                    );
67
68
                    $url = new UrlConcrete(
69
                        $url,
70
                        $page->getDateUpdated(),
71
                        UrlConcrete::CHANGEFREQ_MONTHLY,
72
                        .7
73
                    );
74
75
                    $urlLang = new GoogleMultilangUrlDecorator($url);
76
77
                    $translations = $nodeRepository->findTranslations($page);
78
                    foreach ($translations as $translation) {
79
                        if ($locale !== $translation->getLocale()) {
80
                            $url = $this->router->generate(
81
                                'alpixel_cms',
82
                                [
83
                                    'slug'    => $translation->getSlug(),
84
                                    '_locale' => $translation->getLocale(),
85
                                ],
86
                                UrlGeneratorInterface::ABSOLUTE_URL
87
                            );
88
                            $urlLang->addLink($url, $translation->getLocale());
89
                        }
90
                    }
91
92
                    $event->getGenerator()->addUrl(
0 ignored issues
show
Deprecated Code introduced by
The method Presta\SitemapBundle\Eve...teEvent::getGenerator() has been deprecated with message: in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()`

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
                        $urlLang,
94
                        'cms.'.$locale
95
                    );
96
                }
97
            }
98
        }
99
    }
100
}
101