Completed
Pull Request — master (#120)
by Matt
13:38 queued 06:09
created

visual_demo_controller   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 90
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B handle() 0 39 4
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2018 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
 * Visual demo controller
15
 */
16
class visual_demo_controller
17
{
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\config\config */
22
	protected $config;
23
24
	/** @var \phpbb\request\request */
25
	protected $request;
26
27
	/** @var \phpbb\user */
28
	protected $user;
29
30
	/** @var string */
31
	protected $root_path;
32
33
	/** @var string */
34
	protected $php_ext;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\auth\auth       $auth
40
	 * @param \phpbb\config\config   $config
41
	 * @param \phpbb\request\request $request
42
	 * @param \phpbb\user            $user
43
	 * @param string                 $root_path
44
	 * @param string                 $php_ext
45
	 */
46 4
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\request\request $request, \phpbb\user $user, $root_path, $php_ext)
47
	{
48 4
		$this->auth = $auth;
49 4
		$this->config = $config;
50 4
		$this->request = $request;
51 4
		$this->user = $user;
52 4
		$this->root_path = $root_path;
53 4
		$this->php_ext = $php_ext;
54 4
	}
55
56
	/**
57
	 * Visual demo handler
58
	 *
59
	 * When called by an admin, add or remove the visual demo cookie
60
	 * and direct them to an appropriate forum page to view.
61
	 *
62
	 * @param string $action enable|disable
63
	 * @return \Symfony\Component\HttpFoundation\JsonResponse
64
	 * @throws \phpbb\exception\http_exception An http exception
65
	 */
66 4
	public function handle($action)
67
	{
68 4
		var_dump('in handler');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('in handler'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
69
		// Protect against unauthorised access
70 4
		if (!$this->auth->acl_get('a_'))
71 4
		{
72 2
			throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
73
		}
74
75 2
		if ($action === 'disable')
76 2
		{
77
			// If cookie already exists, lets destroy it and redirect user to previous page viewed.
78 1
			$this->user->set_cookie('phpbb_ads_visual_demo', '', 1);
79 1
			$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
80 1
		}
81
		else
82
		{
83
			// Since cookie does not exist yet, lets create one and send user to the index page.
84 1
			$this->user->set_cookie('phpbb_ads_visual_demo', time(), 0);
85 1
			$redirect = "{$this->root_path}index.{$this->php_ext}";
86
		}
87
88 2
		var_dump('cookie set? ' . $this->request->is_set($this->config['cookie_name'] . '_phpbb_ads_visual_demo', \phpbb\request\request_interface::COOKIE));
89
90
		// Send a JSON response if an AJAX request was used
91 2
		if ($this->request->is_ajax())
92 2
		{
93 2
			return new \Symfony\Component\HttpFoundation\JsonResponse(array(
94 2
				'success' => true,
95 2
			));
96
		}
97
98
		// Redirect user to a page
99
		$redirect = reapply_sid($redirect);
100
		redirect($redirect);
101
102
		// We shouldn't get here, but throw an http exception just in case
103
		throw new \phpbb\exception\http_exception(500, 'GENERAL_ERROR');
104
	}
105
}
106