Passed
Pull Request — master (#120)
by Matt
01:33
created

wizard::get_bbvideo_sites()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.042

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 26
ccs 17
cts 19
cp 0.8947
crap 6.042
rs 9.2222
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;
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\controller\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\exception\http_exception;
0 ignored issues
show
Bug introduced by
The type phpbb\exception\http_exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use phpbb\template\template;
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use phpbb\textformatter\s9e\factory as textformatter;
0 ignored issues
show
Bug introduced by
The type phpbb\textformatter\s9e\factory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * ABBC3 BBCode Wizard class
22
 */
23
class wizard
24
{
25
	/** @var string The default BBvideo site */
26
	const BBVIDEO_DEFAULT = 'youtube';
27
28
	/** @var cache_driver */
29
	protected $cache;
30
31
	/** @var helper */
32
	protected $helper;
33
34
	/** @var request */
35
	protected $request;
36
37
	/** @var template */
38
	protected $template;
39
40
	/** @var textformatter */
41
	protected $textformatter;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param cache_driver  $cache         Cache driver
47
	 * @param helper        $helper        Controller helper object
48
	 * @param request       $request       Request object
49
	 * @param template      $template      Template object
50
	 * @param textformatter $textformatter TextFormatter Factory
51
	 * @access public
52
	 */
53 10
	public function __construct(cache_driver $cache, helper $helper, request $request, template $template, textformatter $textformatter)
54
	{
55 10
		$this->cache = $cache;
56 10
		$this->helper = $helper;
57 10
		$this->request = $request;
58 10
		$this->template = $template;
59 10
		$this->textformatter = $textformatter;
60 10
	}
61
62
	/**
63
	 * BBCode wizard controller accessed with the URL /wizard/bbcode/{mode}
64
	 * (where {mode} is a placeholder for a string of the bbcode tag name)
65
	 * intended to be accessed via AJAX only
66
	 *
67
	 * @param string $mode Mode taken from the URL
68
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
	 * @throws http_exception A http exception
70
	 * @access public
71
	 */
72
	public function bbcode_wizard($mode)
73 10
	{
74
		// Only allow valid AJAX requests
75
		if (!$this->request->is_ajax() || !in_array($mode, ['bbvideo', 'pipes', 'url']))
76 10
		{
77 10
			throw new http_exception(404, 'GENERAL_ERROR');
78 3
		}
79 3
80 1
		if ($mode === 'bbvideo')
81 1
		{
82
			$this->template->assign_vars([
83 3
				'ABBC3_BBVIDEO_SITES'	=> $this->get_bbvideo_sites(),
84
				'ABBC3_BBVIDEO_DEFAULT'	=> self::BBVIDEO_DEFAULT,
85
			]);
86 7
		}
87
88
		return $this->helper->render("@vse_abbc3/abbc3_{$mode}_wizard.html");
89
	}
90
91
	/**
92
	 * Set template variables for the BBvideo wizard
93
	 *
94 1
	 * @access protected
95
	 */
96 1
	protected function get_bbvideo_sites()
97 1
	{
98 1
		if (($bbvideo_sites = $this->cache->get('_bbvideo_sites')) !== false)
99 1
		{
100 1
			return $bbvideo_sites;
101
		}
102
103 1
		$bbvideo_sites = [];
104 1
		$configurator = $this->textformatter->get_configurator();
105
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
106
		{
107 1
			// check that siteID is not already a custom bbcode and that it exists in MediaEmbed
108
			if (!isset($configurator->BBCodes[$siteId]) && $configurator->tags->exists($siteId))
109 1
			{
110 1
				$bbvideo_sites[$siteId] = [
111
					'name'    => $siteConfig['name'],
112 1
					'example' => isset($siteConfig['example']) ? current((array) $siteConfig['example']) : '',
113
				];
114 1
			}
115 1
		}
116 1
117 1
		ksort($bbvideo_sites);
118 1
119 1
		$this->cache->put('_bbvideo_sites', $bbvideo_sites);
120
121
		return $bbvideo_sites;
122
	}
123
}
124