Passed
Push — master ( a025c6...613f63 )
by Josh
03:19
created

AbstractConfigurableHostHelper::addHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 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
	abstract protected function getSiteId(): string;
35
36
	public function setHosts(array $hosts): void
37
	{
38
		$siteId = $this->getSiteId();
39
		if (!isset($this->configurator->registeredVars['MediaEmbed.sites'][$siteId]))
40
		{
41
			$this->configurator->MediaEmbed->add($siteId);
42
		}
43
44
		// Remove previously set hosts for this site
45
		$unsetHosts = array_keys(
46
			(array) $this->configurator->registeredVars['MediaEmbed.hosts'],
47
			$siteId,
48
			true
49
		);
50
		foreach ($unsetHosts as $host)
51
		{
52
			unset($this->configurator->registeredVars['MediaEmbed.hosts'][$host]);
53
		}
54
55
		$this->addHosts($hosts);
56
	}
57
}