Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Front/Content/ActionRss.php (1 issue)

1
<?php
2
3
4
namespace Apps\Controller\Front\Content;
5
6
use Apps\Model\Front\Content\EntityCategoryList;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Suin\RSSWriter\Channel;
13
use Suin\RSSWriter\Feed;
14
use Suin\RSSWriter\Item;
15
16
/**
17
 * Trait ActionRss
18
 * @package Apps\Controller\Front\Content
19
 * @property View $view
20
 * @property Request $request
21
 * @property Response $response
22
 * @method array getConfigs
23
 */
24
trait ActionRss
25
{
26
    /**
27
     * Display rss feeds from content category
28
     * @return string
29
     * @throws ForbiddenException
30
     */
31
    public function rss(): ?string
32
    {
33
        $path = $this->request->getPathWithoutControllerAction();
34
        $configs = $this->getConfigs();
35
36
        // build model data
37
        $model = new EntityCategoryList($path, $configs, 0);
38
        // remove global layout
39
        $this->layout = null;
0 ignored issues
show
Bug Best Practice introduced by zenn
The property layout does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
41
        // check if rss display allowed for this category
42
        if ((int)$model->category['configs']['showRss'] !== 1) {
43
            throw new ForbiddenException(__('Rss feed is disabled for this category'));
44
        }
45
46
        // set rss/xml header
47
        $this->response->headers->set('Content-Type', 'application/rss+xml');
48
49
        // initialize rss feed objects
50
        $feed = new Feed();
51
        $channel = new Channel();
52
53
        // set channel data
54
        $channel->title($model->category['title'])
55
            ->description($model->category['description'])
56
            ->url(App::$Alias->baseUrl . '/content/list/' . $model->category['path'])
57
            ->appendTo($feed);
58
59
        // add content data
60
        if ($model->getContentCount() > 0) {
61
            foreach ($model->items as $row) {
62
                $item = new Item();
63
                // add title, short text, url
64
                $item->title($row['title'])
65
                    ->description($row['text'])
66
                    ->url(App::$Alias->baseUrl . $row['uri']);
67
                // add poster
68
                if ($row['thumb'] !== null) {
69
                    $item->enclosure(App::$Alias->scriptUrl . $row['thumb'], $row['thumbSize'], 'image/jpeg');
70
                }
71
72
                // append response to channel
73
                $item->appendTo($channel);
74
            }
75
        }
76
        // define rss read event
77
        App::$Event->run(static::EVENT_RSS_READ, [
78
            'model' => $model,
79
            'feed' => $feed,
80
            'channel' => $channel
81
        ]);
82
83
        // render response from feed object
84
        return $feed->render();
85
    }
86
}
87