Completed
Push — master ( da2f6e...d3b8cb )
by Kamil
20:06
created

ProductUrlProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Sylius\Bundle\CoreBundle\Sitemap\Provider;
13
14
use Sylius\Bundle\CoreBundle\Sitemap\Factory\SitemapUrlFactoryInterface;
15
use Sylius\Bundle\CoreBundle\Sitemap\Model\ChangeFrequency;
16
use Sylius\Bundle\CoreBundle\Sitemap\Model\SitemapUrlInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
class ProductUrlProvider implements UrlProviderInterface
24
{
25
    /**
26
     * @var RepositoryInterface
27
     */
28
    private $productRepository;
29
30
    /**
31
     * @var RouterInterface
32
     */
33
    private $router;
34
35
    /**
36
     * @var SitemapUrlFactoryInterface
37
     */
38
    private $sitemapUrlFactory;
39
40
    /**
41
     * @var array
42
     */
43
    private $urls = [];
44
45
    /**
46
     * @param RepositoryInterface $productRepository
47
     * @param RouterInterface $router
48
     * @param SitemapUrlFactoryInterface $sitemapUrlFactory
49
     */
50
    public function __construct(
51
        RepositoryInterface $productRepository,
52
        RouterInterface $router,
53
        SitemapUrlFactoryInterface $sitemapUrlFactory
54
    ) {
55
        $this->productRepository = $productRepository;
56
        $this->router = $router;
57
        $this->sitemapUrlFactory = $sitemapUrlFactory;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function generate()
64
    {
65
        $products = $this->productRepository->findAll();
66
67
        foreach ($products as $product) {
68
            $productUrl = $this->sitemapUrlFactory->createNew();
69
            $localization = $this->router->generate($product, [], true);
70
71
            $productUrl->setLastModification($product->getUpdatedAt());
72
            $productUrl->setLocalization($localization);
73
            $productUrl->setChangeFrequency(ChangeFrequency::always());
74
            $productUrl->setPriority(0.5);
75
76
            $this->urls[] = $productUrl;
77
        }
78
79
        return $this->urls;
80
    }
81
}
82