Passed
Push — develop ( a13da5...ae80e4 )
by Daniel
05:57
created

blocks_admin::execute_action()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 6
nop 2
dl 0
loc 18
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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\controller;
11
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
14
class blocks_admin
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\request\request_interface */
20
	protected $request;
21
22
	/** @var \phpbb\language\language */
23
	protected $translator;
24
25
	/** @var \blitze\sitemaker\services\auto_lang */
26
	protected $auto_lang;
27
28
	/** @var \blitze\sitemaker\services\blocks\action_handler */
29
	protected $action_handler;
30
31
	/** @var boolean */
32
	protected $return_url;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\auth\auth									$auth				Auth object
38
	 * @param \phpbb\request\request_interface					$request			Request object
39
	 * @param \phpbb\language\language							$translator			Language object
40
	 * @param \blitze\sitemaker\services\auto_lang				$auto_lang			Auto lang object
41
	 * @param \blitze\sitemaker\services\blocks\action_handler	$action_handler		Handles block actions
42
	 * @param bool												$return_url
43
	 */
44 4
	public function __construct(\phpbb\auth\auth $auth, \phpbb\request\request_interface $request, \phpbb\language\language $translator, \blitze\sitemaker\services\auto_lang $auto_lang, \blitze\sitemaker\services\blocks\action_handler $action_handler, $return_url = false)
45
	{
46 4
		$this->auth = $auth;
47 4
		$this->request = $request;
48 4
		$this->translator = $translator;
49 4
		$this->action_handler = $action_handler;
50 4
		$this->auto_lang = $auto_lang;
51 4
		$this->return_url = $return_url;
52 4
	}
53
54
	/**
55
	 * @param string $action
56
	 * @return \Symfony\Component\HttpFoundation\Response|string
57
	 */
58 4
	public function handle($action)
59
	{
60 4
		$this->translator->add_lang('block_manager', 'blitze/sitemaker');
61
62 4
		$return_data = array(
63
			'id'		=> '',
64 4
			'title'		=> '',
65 4
			'content'   => '',
66 4
			'message'   => '',
67 4
		);
68 4
69
		if (!$this->request->is_ajax())
70 4
		{
71 4
			return redirect(generate_board_url(), $this->return_url);
72 2
		}
73 2
74 1
		if (!$this->auth->acl_get('a_sm_manage_blocks'))
75 1
		{
76
			$return_data['message'] = $this->translator->lang('NOT_AUTHORISED');
77 2
			return new JsonResponse($return_data, 401);
78 2
		}
79
80
		$this->execute_action($action, $return_data);
81 2
82
		return new JsonResponse($return_data);
83
	}
84
85 2
	/**
86
	 * @param string $action
87 2
	 * @param array $return_data
88 1
	 * @return void
89
	 */
90 1
	protected function execute_action($action, array &$return_data)
91
	{
92 2
		try
93
		{
94 1
			$this->auto_lang->add('blocks_admin');
95
96
			$style_id = $this->request->variable('style', 0);
97 2
			$command = $this->action_handler->create($action);
98
99
			$return_data = array_merge($return_data,
100
				$command->execute($style_id)
101
			);
102
103
			$this->action_handler->clear_cache();
104
		}
105
		catch (\blitze\sitemaker\exception\base $e)
106
		{
107
			$return_data['message'] = $e->get_message($this->translator);
108
		}
109
	}
110
}
111