Completed
Push — master ( eda6e2...0d43e9 )
by Matt
07:15
created

wizard   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 95
ccs 32
cts 34
cp 0.9412
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A bbcode_wizard() 0 15 4
C generate_bbvideo_wizard() 0 23 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 11
	public function __construct(cache_driver $cache, helper $helper, request $request, template $template, textformatter $textformatter)
53
	{
54 11
		$this->cache = $cache;
55 11
		$this->helper = $helper;
56 11
		$this->request = $request;
57 11
		$this->template = $template;
58 11
		$this->textformatter = $textformatter;
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
	 * @throws \phpbb\exception\runtime_exception Runtime exception if JSON fails
70
	 * @access public
71
	 */
72 10
	public function bbcode_wizard($mode)
73
	{
74
		// Only allow valid AJAX requests
75 10
		if ($this->request->is_ajax() && in_array($mode, array('bbvideo', 'pipes', 'url')))
76 10
		{
77 3
			if ($mode === 'bbvideo')
78 3
			{
79 1
				$this->generate_bbvideo_wizard();
80 1
			}
81
82 3
			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 2
	protected function generate_bbvideo_wizard()
94
	{
95 2
		if (($bbvideo_sites = $this->cache->get('bbvideo_sites')) === false)
96 2
		{
97 2
			$configurator = $this->textformatter->get_configurator();
98 2
			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 2
				if (!isset($configurator->BBCodes[$siteId]) && $configurator->tags->exists($siteId))
102 2
				{
103
					$bbvideo_sites[$siteId] = isset($siteConfig['example']) ? current((array) $siteConfig['example']) : '';
104
				}
105 2
			}
106
107 2
			$this->cache->put('bbvideo_sites', $bbvideo_sites);
108 2
		}
109
110 2
		$this->template->assign_vars(array(
111 2
			'ABBC3_BBVIDEO_SITES'	=> $bbvideo_sites,
112 2
			'ABBC3_BBVIDEO_LINK_EX'	=> isset($bbvideo_sites[self::BBVIDEO_DEFAULT]) ? $bbvideo_sites[self::BBVIDEO_DEFAULT] : '',
113 2
			'ABBC3_BBVIDEO_DEFAULT'	=> self::BBVIDEO_DEFAULT,
114 2
		));
115 2
	}
116
}
117