Completed
Push — master ( 8919bf...c1ce74 )
by Matt
06:32
created

wizard::generate_bbvideo_wizard()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0671

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 16
cts 18
cp 0.8889
rs 6.7272
cc 7
eloc 11
nc 2
nop 0
crap 7.0671
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\cache\driver\driver_interface as cache_driver;
14
use phpbb\controller\helper;
15
use phpbb\request\request;
16
use phpbb\template\template;
17
use phpbb\textformatter\s9e\factory as textformatter;
18
19
/**
20
 * ABBC3 BBCode Wizard class
21
 */
22
class wizard
23
{
24
	/** @var string The default BBvideo site */
25
	const BBVIDEO_DEFAULT = 'youtube';
26
27
	/** @var cache_driver */
28
	protected $cache;
29
30
	/** @var helper */
31
	protected $helper;
32
33
	/** @var request */
34
	protected $request;
35
36
	/** @var template */
37
	protected $template;
38
39
	/** @var textformatter */
40
	protected $textformatter;
41
42
	/** @var string */
43
	protected $ext_root_path;
44
45
	/**
46
	 * Constructor
47
	 *
48
	 * @param cache_driver  $cache         Cache driver
49
	 * @param helper        $helper        Controller helper object
50
	 * @param request       $request       Request object
51
	 * @param template      $template      Template object
52
	 * @param textformatter $textformatter TextFormatter Factory
53
	 * @param string        $ext_root_path Path to abbc3 extension root
54
	 * @access public
55
	 */
56 11
	public function __construct(cache_driver $cache, helper $helper, request $request, template $template, textformatter $textformatter, $ext_root_path)
57
	{
58 11
		$this->cache = $cache;
59 11
		$this->helper = $helper;
60 11
		$this->request = $request;
61 11
		$this->template = $template;
62 11
		$this->textformatter = $textformatter;
63 11
		$this->ext_root_path = $ext_root_path;
64 11
	}
65
66
	/**
67
	 * BBCode wizard controller accessed with the URL /wizard/bbcode/{mode}
68
	 * (where {mode} is a placeholder for a string of the bbcode tag name)
69
	 * intended to be accessed via AJAX only
70
	 *
71
	 * @param string $mode Mode taken from the URL
72
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
73
	 * @throws \phpbb\exception\http_exception An http exception
74
	 * @throws \phpbb\exception\runtime_exception Runtime exception if JSON fails
75
	 * @access public
76
	 */
77 10
	public function bbcode_wizard($mode)
78
	{
79
		// Only allow valid AJAX requests
80 10
		if ($this->request->is_ajax() && in_array($mode, array('bbvideo', 'pipes', 'url')))
81 10
		{
82 3
			if ($mode === 'bbvideo')
83 3
			{
84 1
				$this->generate_bbvideo_wizard();
85 1
			}
86
87 3
			return $this->helper->render("abbc3_{$mode}_wizard.html");
88
		}
89
90 7
		throw new \phpbb\exception\http_exception(404, 'GENERAL_ERROR');
91
	}
92
93
	/**
94
	 * Set template variables for the BBvideo wizard
95
	 *
96
	 * @access protected
97
	 */
98 2
	protected function generate_bbvideo_wizard()
99
	{
100 2
		if (($bbvideo_sites = $this->cache->get('bbvideo_sites')) === false)
101 2
		{
102 2
			$configurator = $this->textformatter->get_configurator();
103 2
			foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
104
			{
105
				// check that siteID is not already a custom bbcode and that it exists in MediaEmbed
106 2
				if (!isset($configurator->BBCodes[$siteId]) && $configurator->tags->exists($siteId))
107 2
				{
108
					$bbvideo_sites[$siteId] = isset($siteConfig['example']) ? current((array) $siteConfig['example']) : '';
109
				}
110 2
			}
111
112 2
			$this->cache->put('bbvideo_sites', $bbvideo_sites);
113 2
		}
114
115 2
		$this->template->assign_vars(array(
116 2
			'ABBC3_BBVIDEO_SITES'	=> $bbvideo_sites,
117 2
			'ABBC3_BBVIDEO_LINK_EX'	=> isset($bbvideo_sites[self::BBVIDEO_DEFAULT]) ? $bbvideo_sites[self::BBVIDEO_DEFAULT] : '',
118 2
			'ABBC3_BBVIDEO_DEFAULT'	=> self::BBVIDEO_DEFAULT,
119 2
		));
120 2
	}
121
}
122