Completed
Push — master ( 6233be...4939f6 )
by Paul
03:30
created

GlobalController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 139
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getForumAnnouncements() 0 60 8
C homeAction() 0 72 7
1
<?php
2
/**
3
 *
4
 * @copyright (c) 2013 phpBB Group
5
 * @license http://opensource.org/licenses/gpl-3.0.php GNU General Public License v3
6
 * @author MichaelC
7
 *
8
 */
9
10
namespace AppBundle\Controller;
11
12
use AppBundle\Wrappers\PhpbbHandling;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class GlobalController extends Controller
17
{
18
	public function homeAction()
19
	{
20
		$templateVariables = array();
21
		$announcement_forum = 14;
22
23
		$finishedAnnouncements = $this->getForumAnnouncements($announcement_forum);
24
25
		$cache = $this->get('phpbb.cache');
26
27
		if ($cache->contains('blog_announcements_file') !== FALSE)
28
		{
29
			$blogFile = $cache->fetch('blog_announcements_file');
30
			$cacheStatus = 'Hit';
31
		}
32
		else
33
		{
34
            $rss_content = @file_get_contents('https://blog.phpbb.com/rss');
35
            $announcements = [];
36
            $blogFile = [];
37
38
            if (isset($rss_content))
39
            {
40
                $xml_elements = new \SimpleXMLElement($rss_content);
41
                $i = 0;
42
43
                foreach ($xml_elements->channel->item as $item)
44
                {
45
                    $post_time = strtotime($item->pubDate);
46
                    $blog_preview = wordwrap(html_entity_decode(strip_tags((string)$item->description)), 200, '>');
47
                    $blog_preview = substr($blog_preview, 0, strpos($blog_preview, '>'));
48
49
                    $announcements[$post_time] = array(
50
                        'DAY' => date('d', $post_time),
51
                        'MONTH' => date('M', $post_time),
52
                        'YEAR' => date('Y', $post_time),
53
                        'U_LINK' => (string) $item->link,
54
                        'TITLE' => (string) $item->title,
55
                        'FROM_BLOG' => true,
56
                        'PREVIEW' => $blog_preview,
57
                    );
58
59
                    $i++;
60
                    if ($i == 3)
61
                        break;
62
                }
63
64
                // JSON Output
65
                $blogFile = json_encode($announcements);
66
            }
67
            $cache->save('blog_announcements_file', $blogFile, 3600);
68
			$cacheStatus = 'Miss';
69
		}
70
71
		$blogJson = @json_decode($blogFile, true);
72
73
		$blogAnnouncements = $blogJson === null ? array() : $blogJson;
74
		$finishedAnnouncements = $finishedAnnouncements === null ? array() : $finishedAnnouncements;
75
76
		krsort($blogAnnouncements);
77
78
		$announcements = array_merge($finishedAnnouncements, $blogAnnouncements);
79
80
		$templateVariables += array(
81
			'homepage'			  => true,
82
			'announcements_forum'   => '/community/viewforum.php?f=' . $announcement_forum,
83
			'announcements'		 => $announcements,);
84
85
		$content = $this->renderView('AppBundle:Global:index.html.twig', $templateVariables);
86
		$response = new Response($content);
87
		$response->headers->set('X-Cache-Blog', $cacheStatus);
88
		return $response;
89
	}
90
91
	/**
92
	 * @param integer $announcement_forum
93
	 */
94
	private function getForumAnnouncements($announcement_forum)
95
	{
96
		$retrieve_limit = 3;
97
98
		$phpbbConnection = $this->get('doctrine.dbal.phpbb_connection');
99
		$forumAnnouncements = PhpbbHandling::getTopicsFromForum(
100
			$phpbbConnection,
101
			$announcement_forum,
102
			$retrieve_limit,
103
			$this->container->getParameter('phpbb_database_prefix')
104
		);
105
		$finishedAnnouncements = array();
106
107
		foreach ($forumAnnouncements as $announcement) {
108
			$preview = $announcement['post_text'];
109
			$preview = PhpbbHandling::bbcodeStripping($preview, $announcement['bbcode_uid']);
110
			$preview = trim(preg_replace('#http(?:\:|&\#58;)//\S+#', '', $preview));
111
112
			// Decide how large the preview text should be
113
			$preview_max_len = 200;
114
			$preview_len = strlen($preview);
115
116
			if ($preview_len > $preview_max_len) {
117
				// If the first thing is a link, nuke it
118
				$preview_clean = str_replace('&#58;', ':', $preview);
119
120
				if (substr($preview_clean, 0, 8) == 'https://' || substr($preview_clean, 0, 7) == 'http://') {
121
					$preview = preg_replace('/^(\S*)\s/', '', $preview);
122
				}
123
124
				// Truncate to the maximum length
125
				$preview = substr($preview, 0, $preview_max_len);
126
127
				// See if there is a nice place to break close to the max length
128
				$breakchars = array(' ', ',', '.');
129
				$clean_break_pos = 0;
130
131
				foreach ($breakchars as $char) {
132
					$clean_break_pos_new = strrpos($preview, $char);
133
					$clean_break_pos = $clean_break_pos_new > $clean_break_pos ? $clean_break_pos_new : $clean_break_pos;
134
				}
135
136
				if ($clean_break_pos) {
137
					$preview = substr($preview, 0, $clean_break_pos);
138
				}
139
			}
140
141
			$finishedAnnouncements[$announcement['topic_time']] = array(
142
				'DAY' 		=> date('d', $announcement['topic_time']),
143
				'MONTH' 	=> date('M', $announcement['topic_time']),
144
				'YEAR' 		=> date('Y', $announcement['topic_time']),
145
				'U_LINK' 	=> '/community/viewtopic.php?f=' . $announcement_forum . '&amp;t=' . $announcement['topic_id'],
146
				'TITLE' 	=> $announcement['topic_title'],
147
				'FROM_BLOG' => false,
148
				'PREVIEW' 	=> $preview,
149
			);
150
		}
151
152
		return $finishedAnnouncements;
153
	}
154
}
155