menus_admin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
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 menus_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\menus\action_handler */
26
	protected $action_handler;
27
28
	/** @var boolean */
29
	protected $return_url;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\auth\auth									$auth				Auth object
35
	 * @param \phpbb\request\request_interface					$request			Request object
36 5
	 * @param \phpbb\language\language							$translator			Languag object
37
	 * @param \blitze\sitemaker\services\menus\action_handler	$action_handler		Handles menu actions
38 5
	 * @param bool												$return_url
39 5
	 */
40 5
	public function __construct(\phpbb\auth\auth $auth, \phpbb\request\request_interface $request, \phpbb\language\language $translator, \blitze\sitemaker\services\menus\action_handler $action_handler, $return_url = false)
41 5
	{
42 5
		$this->auth = $auth;
43
		$this->request = $request;
44
		$this->translator = $translator;
45
		$this->action_handler = $action_handler;
46
		$this->return_url = $return_url;
47
	}
48 5
49
	/**
50 5
	 * @param string $action
51
	 * @return \Symfony\Component\HttpFoundation\Response|string
52 5
	 */
53 5
	public function handle($action)
54 1
	{
55
		$return_data = array();
56 1
		if (!$this->request->is_ajax() || !$this->auth->acl_get('a_sm_manage_menus'))
57 1
		{
58
			$return_data['message'] = $this->translator->lang('NOT_AUTHORISED');
59
			return new JsonResponse($return_data, 401);
60
		}
61
62 4
		try
63 4
		{
64
			$command = $this->action_handler->create($action);
65 2
			$return_data = $command->execute();
66
67 4
			$this->action_handler->clear_cache();
68
		}
69 1
		catch (\blitze\sitemaker\exception\base $e)
70 1
		{
71 1
			$return_data['message'] = $e->get_message($this->translator);
72
		}
73 1
		catch (\Exception $e)
74
		{
75
			$return_data['message'] = $this->translator->lang($e->getMessage());
76 4
		}
77
78
		return new JsonResponse($return_data);
79
	}
80
}
81