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

AbstractConfigurableHostHelper::getHosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
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
}