Completed
Push — master ( 6746ea...50fd55 )
by Matt
06:52
created

wizard   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 97
ccs 14
cts 35
cp 0.4
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A bbcode_wizard() 0 15 4
B generate_bbvideo_wizard() 0 25 7
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
	/**
43
	 * Constructor
44
	 *
45
	 * @param cache_driver  $cache         Cache driver
46
	 * @param helper        $helper        Controller helper object
47
	 * @param request       $request       Request object
48
	 * @param template      $template      Template object
49
	 * @param textformatter $textformatter TextFormatter Factory
50
	 * @access public
51
	 */
52 9
	public function __construct(cache_driver $cache, helper $helper, request $request, template $template, textformatter $textformatter)
53
	{
54 9
		$this->cache = $cache;
55 9
		$this->helper = $helper;
56 9
		$this->request = $request;
57 9
		$this->template = $template;
58 9
		$this->textformatter = $textformatter;
59 9
	}
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
	 * @throws \phpbb\exception\runtime_exception Runtime exception if JSON fails
70
	 * @access public
71
	 */
72 9
	public function bbcode_wizard($mode)
73
	{
74
		// Only allow valid AJAX requests
75 9
		if ($this->request->is_ajax() && in_array($mode, array('bbvideo', 'pipes', 'url')))
76 9
		{
77 2
			if ($mode === 'bbvideo')
78 2
			{
79
				$this->generate_bbvideo_wizard();
80
			}
81
82 2
			return $this->helper->render("abbc3_{$mode}_wizard.html");
83
		}
84
85 7
		throw new \phpbb\exception\http_exception(404, 'GENERAL_ERROR');
86
	}
87
88
	/**
89
	 * Set template variables for the BBvideo wizard
90
	 *
91
	 * @access protected
92
	 */
93
	protected function generate_bbvideo_wizard()
94
	{
95
		if (($bbvideo_sites = $this->cache->get('_bbvideo_sites')) === false)
96
		{
97
			$configurator = $this->textformatter->get_configurator();
98
			foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
99
			{
100
				// check that siteID is not already a custom bbcode and that it exists in MediaEmbed
101
				if (!isset($configurator->BBCodes[$siteId]) && $configurator->tags->exists($siteId))
102
				{
103
					$bbvideo_sites[$siteId] = isset($siteConfig['example']) ? current((array) $siteConfig['example']) : '';
104
				}
105
			}
106
107
			$this->cache->put('_bbvideo_sites', $bbvideo_sites);
108
		}
109
110
		ksort($bbvideo_sites);
111
112
		$this->template->assign_vars(array(
113
			'ABBC3_BBVIDEO_SITES'	=> $bbvideo_sites,
114
			'ABBC3_BBVIDEO_LINK_EX'	=> isset($bbvideo_sites[self::BBVIDEO_DEFAULT]) ? $bbvideo_sites[self::BBVIDEO_DEFAULT] : '',
115
			'ABBC3_BBVIDEO_DEFAULT'	=> self::BBVIDEO_DEFAULT,
116
		));
117
	}
118
}
119