Index.php 2.44 KB
Newer Older
1
<?php
2
/**
3 4
 * Copyright (c) 2016 by the ACP3 Developers.
 * See the LICENCE file at the top-level module directory for licencing details.
5
 */
6

7
namespace ACP3\Modules\ACP3\Feeds\Controller\Frontend\Index;
8 9 10

use ACP3\Core;
use ACP3\Modules\ACP3\Feeds;
11
use ACP3\Modules\ACP3\System\Installer\Schema;
12 13 14

/**
 * Class Index
15
 * @package ACP3\Modules\ACP3\Feeds\Controller\Frontend\Index
16
 */
17
class Index extends Core\Controller\AbstractFrontendAction
18
{
Tino Goratsch's avatar
Tino Goratsch committed
19 20
    use Core\Cache\CacheResponseTrait;

21
    /**
22
     * @var \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator
23 24
     */
    protected $feedGenerator;
25
    /**
26
     * @var Feeds\Utility\FeedAvailabilityRegistrar
27 28
     */
    protected $availableFeedsRegistrar;
29 30

    /**
31
     * @param \ACP3\Core\Controller\Context\FrontendContext $context
32
     * @param \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator $feedGenerator
33
     * @param Feeds\Utility\FeedAvailabilityRegistrar $availableFeedsRegistrar
34 35
     */
    public function __construct(
36
        Core\Controller\Context\FrontendContext $context,
37
        Feeds\View\Renderer\FeedGenerator $feedGenerator,
38
        Feeds\Utility\FeedAvailabilityRegistrar $availableFeedsRegistrar
39
    ) {
40
        parent::__construct($context);
41 42

        $this->feedGenerator = $feedGenerator;
43
        $this->availableFeedsRegistrar = $availableFeedsRegistrar;
44 45
    }

46 47 48
    /**
     * @param string $feed
     *
49
     * @return \Symfony\Component\HttpFoundation\Response
50
     * @throws \ACP3\Core\Controller\Exception\ResultNotExistsException
51
     */
52
    public function execute($feed)
53
    {
54
        if ($this->acl->hasPermission('frontend/' . $feed) === true) {
tinogo's avatar
tinogo committed
55
            $this->setCacheResponseCacheable(
56
                $this->config->getSettings(Schema::MODULE_NAME)['cache_lifetime']
tinogo's avatar
tinogo committed
57
            );
Tino Goratsch's avatar
Tino Goratsch committed
58

59 60 61 62
            try {
                $feedItems = $this->availableFeedsRegistrar
                    ->getFeedItemsByModuleName($feed)
                    ->fetchFeedItems();
63

64 65 66 67 68 69 70 71
                $this->feedGenerator
                    ->setTitle($this->config->getSettings(Schema::MODULE_NAME)['site_title'])
                    ->setDescription($this->translator->t($feed, $feed))
                    ->assign($feedItems);

                $this->setContentType('text/xml');
                return $this->response->setContent($this->feedGenerator->generateFeed());
            } catch (\InvalidArgumentException $e) {
72

73
            }
74
        }
75

76
        throw new Core\Controller\Exception\ResultNotExistsException();
77 78
    }
}