Completed
Push — develop-3.2.x ( edca4c...477e50 )
by Matt
05:24
created

wizard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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