Completed
Push — master ( 0175b6...27ff26 )
by Benjamin
47:22 queued 26:57
created

SitemapListener::populateSitemap()   D

Complexity

Conditions 10
Paths 25

Size

Total Lines 46
Code Lines 29

Duplication

Lines 6
Ratio 13.04 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 6
loc 46
rs 4.983
cc 10
eloc 29
nc 25
nop 1

How to fix   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 Alpixel\Bundle\SEOBundle\Event\SitemapPopulateEvent;
6
use Alpixel\Bundle\SEOBundle\Service\SitemapListenerInterface;
7
use Alpixel\Bundle\SEOBundle\Sitemap\Url\UrlConcrete;
8
use Doctrine\Bundle\DoctrineBundle\Registry;
9
use Symfony\Component\Routing\RouterInterface;
10
11
class SitemapListener implements SitemapListenerInterface
12
{
13
    protected $doctrine;
14
    private $router;
15
16
    public function __construct(Registry $doctrine, RouterInterface $router)
17
    {
18
        $this->doctrine = $doctrine;
19
        $this->router = $router;
20
    }
21
22
    public function populateSitemap(SitemapPopulateEvent $event)
23
    {
24
        $section = $event->getSection();
25
        if (is_null($section) || $section == 'cms') {
26
            $entities = [];
27
            $entityManager = $this->doctrine->getManager();
28
            $meta = $entityManager->getMetadataFactory()->getAllMetadata();
29
30 View Code Duplication
            foreach ($meta as $m) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
                $relations = $m->getAssociationMappings();
32
                if (array_key_exists('node', $relations) && $relations['node']['targetEntity'] == 'Alpixel\Bundle\CMSBundle\Entity\Node') {
33
                    $entities[] = $m;
34
                }
35
            }
36
37
            $pages = [];
38
            foreach ($entities as $entity) {
39
                $objects = $entityManager
40
                            ->getRepository($entity->getName())
41
                            ->findAll();
42
                foreach ($objects as $object) {
43
                    if ($object->getNode()->getPublished() === true) {
44
                        $pages[] = $object;
45
                    }
46
                }
47
            }
48
49
            foreach ($pages as $cms) {
50
                $url = $this->router->generate('front_cms', [
51
                        'slug' => $cms->getNode()->getSlug(),
52
                    ],
53
                    true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
                );
55
56
                $event->getGenerator()->addUrl(
57
                    new UrlConcrete(
58
                        $url,
59
                        new \DateTime(),
60
                        UrlConcrete::CHANGEFREQ_MONTHLY,
61
                        .5
62
                    ),
63
                    'cms'
64
                );
65
            }
66
        }
67
    }
68
}
69