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

SitemapListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 10.34 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
c 2
b 1
f 0
lcom 1
cbo 1
dl 6
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D populateSitemap() 6 46 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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