Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

modules/FeedGenerator/FeedGenerator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\FeedGenerator;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Db;
6
use Redaxscript\Header;
7
use Redaxscript\Model;
8
use Redaxscript\Module;
9
use XMLWriter;
10
use function strip_tags;
11
12
/**
13
 * provide feeds to your audience
14
 *
15
 * @since 2.3.0
16
 *
17
 * @package Redaxscript
18
 * @category Modules
19
 * @author Henry Ruhs
20
 */
21
22
class FeedGenerator extends Module\Module
23
{
24
	/**
25
	 * array of the module
26
	 *
27
	 * @var array
28
	 */
29
30
	protected static $_moduleArray =
31
	[
32
		'name' => 'Feed Generator',
33
		'alias' => 'FeedGenerator',
34
		'author' => 'Redaxmedia',
35
		'description' => 'Provide feeds to your audience',
36
		'version' => '4.5.0',
37
		'license' => 'MIT'
38
	];
39
40
	/**
41
	 * renderStart
42
	 *
43
	 * @since 2.3.0
44
	 */
45
46
	public function renderStart() : void
47
	{
48
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'feed-generator')
49
		{
50
			$this->_registry->set('renderBreak', true);
51
			if ($this->_registry->get('thirdParameter') === 'articles')
52
			{
53
				echo $this->render('articles');
54
			}
55
			if ($this->_registry->get('thirdParameter') === 'comments')
56
			{
57
				echo $this->render('comments');
58
			}
59
		}
60
	}
61
62
	/**
63
	 * render
64
	 *
65
	 * @since 2.3.0
66
	 *
67
	 * @param string $table name of the table
68
	 *
69
	 * @return string
70
	 */
71
72
	public function render(string $table = null) : string
73
	{
74
		/* query result */
75
76
		$resultArray[$table] = Db::forTablePrefix($table)
0 ignored issues
show
Coding Style Comprehensibility introduced by
$resultArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resultArray = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
77
			->whereLanguageIs($this->_registry->get('language'))
78
			->whereNull('access')
79
			->where('status', 1)
80
			->orderBySetting('rank')
81
			->findMany();
82
83
		/* write xml */
84
85
		Header::contentType('application/atom+xml');
86
		return $this->_writeXML($resultArray);
87
	}
88
89
	/**
90
	 * @param array $resultArray
91
	 *
92
	 * @return string
93
	 */
94
95
	protected function _writeXML(array $resultArray = []) : string
96
	{
97
		$writer = new XMLWriter();
98
		$contentModel = new Model\Content();
99
		$settingModel = new Model\Setting();
100
		$dater = new Dater();
101
		$dater->init($this->_registry->get('now'));
102
		$root = $this->_registry->get('root');
103
		$parameterRoute = $this->_registry->get('parameterRoute');
104
105
		/* prepare href */
106
107
		$href = $root . '/' . $parameterRoute . $this->_registry->get('fullRoute');
108
		if ($this->_request->getQuery('l'))
109
		{
110
			$href .= $this->_registry->get('languageRoute') . $this->_registry->get('language');
111
		}
112
113
		/* write xml */
114
115
		$writer->openMemory();
116
		$writer->setIndent(true);
117
		$writer->setIndentString('	');
118
		$writer->startDocument('1.0', $settingModel->get('charset'));
119
		$writer->startElement('feed');
120
		$writer->writeAttribute('xmlns', 'http://www.w3.org/2005/Atom');
121
		$writer->startElement('link');
122
		$writer->writeAttribute('type', 'application/atom+xml');
123
		$writer->writeAttribute('href', $href);
124
		$writer->writeAttribute('rel', 'self');
125
		$writer->endElement();
126
		$writer->writeElement('id', $href);
127
		$writer->writeElement('title', $settingModel->get('title'));
128
		$writer->writeElement('updated', $dater->getDateTime()->format('c'));
129
		$writer->startElement('author');
130
		$writer->writeElement('name', $settingModel->get('author'));
131
		$writer->endElement();
132
133
		/* process result */
134
135
		foreach ($resultArray as $table => $result)
136
		{
137
			foreach ($result as $value)
138
			{
139
				$dater->init($value->date);
140
				$writer->startElement('entry');
141
				$writer->writeElement('id', $root . '/' . $parameterRoute . $contentModel->getRouteByTableAndId($table, $value->id));
142
				$writer->writeElement('title', $value->title);
143
				$writer->writeElement('updated', $dater->getDateTime()->format('c'));
144
				$writer->writeElement('content', strip_tags($value->text));
145
				$writer->endElement();
146
			}
147
		}
148
		$writer->endElement();
149
		$writer->endDocument();
150
		return $writer->outputMemory(true);
151
	}
152
}
153