Passed
Push — master ( 9896b0...0ad200 )
by Josh
02:46
created

AbstractConfigurableHostHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 19
c 1
b 0
f 1
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addHost() 0 3 1
A addHosts() 0 12 3
A getHosts() 0 10 1
A setHosts() 0 15 3
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers;
9
10
use function array_keys, sort, strtolower;
11
12
abstract class AbstractConfigurableHostHelper extends AbstractSiteHelper
13
{
14
	public function addHost(string $host): void
15
	{
16
		$this->addHosts([$host]);
17
	}
18
19
	public function addHosts(array $hosts): void
20
	{
21
		$siteId = $this->getSiteId();
22
		if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId]))
23
		{
24
			$this->configurator->MediaEmbed->add($siteId);
25
		}
26
27
		foreach ($hosts as $host)
28
		{
29
			$host = strtolower($host);
30
			$this->configurator->registeredVars['MediaEmbed.hosts'][$host] = $siteId;
31
		}
32
	}
33
34
	public function getHosts(): array
35
	{
36
		$hosts = array_keys(
37
			(array) ($this->configurator->registeredVars['MediaEmbed.hosts'] ?? []),
38
			$this->getSiteId(),
39
			true
40
		);
41
		sort($hosts, SORT_STRING);
42
43
		return $hosts;
44
	}
45
46
	abstract protected function getSiteId(): string;
47
48
	public function setHosts(array $hosts): void
49
	{
50
		$siteId = $this->getSiteId();
51
		if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId]))
52
		{
53
			$this->configurator->MediaEmbed->add($siteId);
54
		}
55
56
		// Remove previously set hosts for this site
57
		foreach ($this->getHosts() as $host)
58
		{
59
			unset($this->configurator->registeredVars['MediaEmbed.hosts'][$host]);
60
		}
61
62
		$this->addHosts($hosts);
63
	}
64
}