Completed
Push — develop-3.2 ( 9c48fd...ed32eb )
by Matt
11:41 queued 09:27
created

wizard::bbcode_wizard()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 6
nc 3
nop 1
crap 4
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\controller;
12
13
use phpbb\controller\helper;
14
use phpbb\request\request;
15
use phpbb\template\template;
16
use vse\abbc3\ext;
17
18
/**
19
 * ABBC3 BBCode Wizard class
20
 */
21
class wizard
22
{
23
	/** @var string The default BBvideo site */
24
	const BBVIDEO_DEFAULT = 'youtube.com';
25
26
	/** @var helper */
27
	protected $helper;
28
29
	/** @var request */
30
	protected $request;
31
32
	/** @var template */
33
	protected $template;
34
35
	/** @var string */
36
	protected $ext_root_path;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param helper   $helper        Controller helper object
42
	 * @param request  $request       Request object
43
	 * @param template $template      Template object
44
	 * @param string   $ext_root_path Path to abbc3 extension root
45
	 * @access public
46
	 */
47 13
	public function __construct(helper $helper, request $request, template $template, $ext_root_path)
48
	{
49 13
		$this->helper = $helper;
50 13
		$this->request = $request;
51 13
		$this->template = $template;
52 13
		$this->ext_root_path = $ext_root_path;
53 13
	}
54
55
	/**
56
	 * BBCode wizard controller accessed with the URL /wizard/bbcode/{mode}
57
	 * (where {mode} is a placeholder for a string of the bbcode tag name)
58
	 * intended to be accessed via AJAX only
59
	 *
60
	 * @param string $mode Mode taken from the URL
61
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
62
	 * @throws \phpbb\exception\http_exception An http exception
63
	 * @throws \phpbb\exception\runtime_exception Runtime exception if JSON fails
64
	 * @access public
65
	 */
66 10
	public function bbcode_wizard($mode)
67
	{
68
		// Only allow valid AJAX requests
69 10
		if ($this->request->is_ajax() && in_array($mode, array('bbvideo', 'pipes', 'url')))
70 10
		{
71 3
			if ($mode === 'bbvideo')
72 3
			{
73 1
				$this->generate_bbvideo_wizard();
74 1
			}
75
76 3
			return $this->helper->render("abbc3_{$mode}_wizard.html");
77
		}
78
79 7
		throw new \phpbb\exception\http_exception(404, 'GENERAL_ERROR');
80
	}
81
82
	/**
83
	 * Set template variables for the BBvideo wizard
84
	 *
85
	 * @throws \phpbb\exception\runtime_exception
86
	 * @access protected
87
	 */
88 1
	protected function generate_bbvideo_wizard()
89
	{
90
		// Construct BBvideo allowed site select options
91 1
		$bbvideo_sites = $this->load_json_data('bbvideo.json');
92
93
		// Construct BBvideo size preset select options
94
		$bbvideo_size_presets = array(
95 1
			array('w' => '560', 'h' => '315'),
96 1
			array('w' => '640', 'h' => '360'),
97 1
			array('w' => '853', 'h' => '480'),
98 1
			array('w' => '1280', 'h' => '720'),
99 1
		);
100
101 1
		$this->template->assign_vars(array(
102 1
			'ABBC3_BBVIDEO_SITES'	=> $bbvideo_sites,
103 1
			'ABBC3_BBVIDEO_LINK_EX'	=> isset($bbvideo_sites[self::BBVIDEO_DEFAULT]) ? $bbvideo_sites[self::BBVIDEO_DEFAULT] : '',
104 1
			'ABBC3_BBVIDEO_DEFAULT'	=> self::BBVIDEO_DEFAULT,
105 1
			'ABBC3_BBVIDEO_HEIGHT'	=> ext::BBVIDEO_HEIGHT,
106 1
			'ABBC3_BBVIDEO_WIDTH'	=> ext::BBVIDEO_WIDTH,
107 1
			'ABBC3_BBVIDEO_PRESETS'	=> $bbvideo_size_presets,
108 1
		));
109 1
	}
110
111
	/**
112
	 * Return decoded JSON data from a JSON file (stored in assets/)
113
	 *
114
	 * @param string $json_file The name of the JSON file to get
115
	 * @return array JSON data
116
	 * @throws \phpbb\exception\runtime_exception
117
	 * @access protected
118
	 */
119 4
	protected function load_json_data($json_file)
120
	{
121 4
		$json_file = $this->ext_root_path . 'assets/' . $json_file;
122
123 4
		if (!file_exists($json_file))
124 4
		{
125 1
			throw new \phpbb\exception\runtime_exception('FILE_NOT_FOUND', array($json_file));
126
		}
127
128 3
		if (!($file_contents = file_get_contents($json_file)))
129 3
		{
130 1
			throw new \phpbb\exception\runtime_exception('FILE_CONTENT_ERR', array($json_file));
131
		}
132
133 2
		if (($json_data = json_decode($file_contents, true)) === null)
134 2
		{
135 1
			throw new \phpbb\exception\runtime_exception('FILE_JSON_DECODE_ERR', array($json_file));
136
		}
137
138 1
		return $json_data;
139
	}
140
}
141