|
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) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.