Completed
Push — develop ( 86b038...fd19b8 )
by Daniel
09:51
created

startpage   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 163
ccs 68
cts 68
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getSubscribedEvents() 0 8 1
A add_forum_to_navbar() 0 23 3
A cleanup_breadcrumbs() 0 7 2
A set_startpage() 0 20 4
A exit_handler() 0 4 1
B get_startpage_controller() 0 24 4
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\event;
11
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
15
class startpage implements EventSubscriberInterface
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var ContainerInterface */
21
	protected $phpbb_container;
22
23
	/** @var \phpbb\request\request_interface */
24
	protected $request;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\language\language */
30
	protected $translator;
31
32
	/** @var \phpbb\user */
33
	protected $user;
34
35
	/** @var string */
36
	protected $php_ext;
37
38
	/* @var bool */
39
	protected $is_startpage = false;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param \phpbb\config\config						$config					Config object
45
	 * @param ContainerInterface						$phpbb_container		Service container
46
	 * @param \phpbb\request\request_interface			$request				Request object
47
	 * @param \phpbb\template\template					$template				Template object
48
	 * @param \phpbb\language\language					$translator				Language object
49
	 * @param \phpbb\user								$user					User object
50
	 * @param string									$php_ext				php file extension
51
	 */
52 16
	public function __construct(\phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user, $php_ext)
53
	{
54 16
		$this->config = $config;
55 16
		$this->phpbb_container = $phpbb_container;
56 16
		$this->request = $request;
57 16
		$this->template = $template;
58 16
		$this->translator = $translator;
59 16
		$this->user = $user;
60 16
		$this->php_ext = $php_ext;
61 16
	}
62
63
	/**
64
	 * @return array
65
	 */
66 1
	public static function getSubscribedEvents()
67
	{
68
		return array(
69 1
			'core.page_header'					=> 'add_forum_to_navbar',
70 1
			'core.display_forums_modify_sql'	=> 'set_startpage',
71 1
			'core.page_footer'					=> 'cleanup_breadcrumbs',
72 1
		);
73
	}
74
75
	/**
76
	 * If start page is set,
77
	 * - Add "Forum" to navbar
78
	 * - Add "Forum" to the breadcrump when viewing forum page (viewforum/viewtopic/posting)
79
	 */
80 7
	public function add_forum_to_navbar()
81
	{
82 7
		if ($this->config['sitemaker_startpage_controller'])
83 7
		{
84 6
			$u_viewforum = $this->phpbb_container->get('controller.helper')->route('blitze_sitemaker_forum');
85
86
			// show 'Forum' menu item in navbar
87 6
			$this->template->assign_vars(array(
88 6
				'SM_FORUM_ICON'		=> $this->config['sm_forum_icon'],
89 6
				'SM_SHOW_FORUM_NAV'	=> $this->config['sm_show_forum_nav'],
90 6
				'U_SM_VIEWFORUM'	=> $u_viewforum,
91 6
			));
92
93
			// Add "Forum" to breadcrumb menu when viewing forum pages (viewforum/viewtopic/posting)
94 6
			if ($this->request->is_set('f'))
95 6
			{
96 3
				$this->template->alter_block_array('navlinks', array(
97 3
					'FORUM_NAME'	=> $this->translator->lang('FORUM'),
98 3
					'U_VIEW_FORUM'	=> $u_viewforum,
99 3
				));
100 3
			}
101 6
		}
102 7
	}
103
104
	/**
105
	 * If we are on the index page and we have set a custom start page,
106
	 * we do not want breadcrumbs like Home > Articles on the index page.
107
	 * This removes everything else, leaving just 'Home'
108
	 * @return void
109
	 */
110 2
	public function cleanup_breadcrumbs()
111
	{
112 2
		if ($this->is_startpage)
113 2
		{
114 1
			$this->template->destroy_block_vars('navlinks');
115 1
		}
116 2
	}
117
118
	/**
119
	 * @return void
120
	 */
121 6
	public function set_startpage()
122
	{
123 6
		if ($this->user->page['page_name'] == 'index.' . $this->php_ext && !$this->is_startpage && ($controller_object = $this->get_startpage_controller()) !== false)
124 6
		{
125 1
			$method = $this->config['sitemaker_startpage_method'];
126 1
			$this->is_startpage = true;
127
128 1
			$controller_dir = explode('\\', get_class($controller_object));
129 1
			$controller_style_dir = 'ext/' . $controller_dir[0] . '/' . $controller_dir[1] . '/styles';
130 1
			$this->template->set_style(array($controller_style_dir, 'styles'));
131
132 1
			$arguments = explode('/', $this->config['sitemaker_startpage_params']);
133
134
			/** @type \Symfony\Component\HttpFoundation\Response $response */
135 1
			$response = call_user_func_array(array($controller_object, $method), $arguments);
136 1
			$response->send();
137
138 1
			$this->exit_handler();
139 1
		}
140 6
	}
141
142
	/**
143
	 * @codeCoverageIgnore
144
	 */
145
	protected function exit_handler()
146
	{
147
		exit_handler();
148
	}
149
150
	/**
151
	 * @return object|false
152
	 */
153 5
	protected function get_startpage_controller()
154
	{
155 5
		$controller_service_name = $this->config['sitemaker_startpage_controller'];
156 5
		if ($this->phpbb_container->has($controller_service_name))
157 5
		{
158 2
			$controller_object = $this->phpbb_container->get($controller_service_name);
159 2
			$method = $this->config['sitemaker_startpage_method'];
160
161 2
			if (is_callable(array($controller_object, $method)))
162 2
			{
163 1
				return $controller_object;
164
			}
165 1
		}
166
167
		// we have a startpage controller but it does not exist or it is not callable so remove it
168
		if ($controller_service_name)
169 4
		{
170 2
			$this->config->set('sitemaker_startpage_controller', '');
171 2
			$this->config->set('sitemaker_startpage_method', '');
172 2
			$this->config->set('sitemaker_startpage_params', '');
173 2
		}
174
175 4
		return false;
176
	}
177
}
178