Completed
Push — master ( 531489...6d1d7c )
by Josh
19:03
created

LiveSiteDefinitionProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 122
ccs 42
cts 42
cp 1
rs 10
wmc 18
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getIds() 0 10 2
A getConfigFromXmlFile() 0 7 1
B getElementConfig() 0 25 6
A getFilePath() 0 4 1
A getValueFromElement() 0 6 3
A getSiteConfig() 0 5 1
A hasSiteConfig() 0 4 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator;
9
10
use DOMDocument;
11
use DOMElement;
12
use DOMXPath;
13
use InvalidArgumentException;
14
15
class LiveSiteDefinitionProvider extends SiteDefinitionProvider
16
{
17
	/**
18
	* @var string Path to site definitions' dir
19
	*/
20
	protected $path;
21
22
	/**
23
	* Constructor
24
	*
25
	* @param  string $path Path to site definitions' dir
26
	*/
27 9
	public function __construct($path)
28
	{
29 9
		if (!file_exists($path) || !is_dir($path))
30 9
		{
31 1
			throw new InvalidArgumentException('Invalid site directory');
32
		}
33 8
		$this->path = $path;
34 8
	}
35
36
	/**
37
	* {@inheritdoc}
38
	*/
39 1
	public function getIds()
40
	{
41 1
		$siteIds = [];
42 1
		foreach (glob($this->path . '/*.xml') as $filepath)
43
		{
44 1
			$siteIds[] = basename($filepath, '.xml');
45 1
		}
46
47 1
		return $siteIds;
48
	}
49
50
	/**
51
	* Extract a site's config from its XML file
52
	*
53
	* @param  string $filepath Path to the XML file
54
	* @return mixed
55
	*/
56 2
	protected function getConfigFromXmlFile($filepath)
57
	{
58 2
		$dom = new DOMDocument;
59 2
		$dom->load($filepath);
60
61 2
		return $this->getElementConfig($dom->documentElement);
62
	}
63
64
	/**
65
	* Extract a site's config from its XML representation
66
	*
67
	* @param  DOMElement $element Current node
68
	* @return mixed
69
	*/
70 2
	protected function getElementConfig(DOMElement $element)
71
	{
72 2
		$config = [];
73 2
		foreach ($element->attributes as $attribute)
74
		{
75 2
			$config[$attribute->name] = $attribute->value;
76 2
		}
77
78
		// Group child nodes by name
79 2
		$childNodes = [];
80 2
		foreach ($element->childNodes as $childNode)
81
		{
82 2
			if ($childNode instanceof DOMElement)
83 2
			{
84 2
				$childNodes[$childNode->nodeName][] = $this->getValueFromElement($childNode);
85 2
			}
86 2
		}
87
88 2
		foreach ($childNodes as $nodeName => $values)
89
		{
90 2
			$config[$nodeName] = (count($values) === 1) ? end($values) : $values;
91 2
		}
92
93 2
		return $config;
94
	}
95
96
	/**
97
	* Return the path that corresponds to given siteId
98
	*
99
	* @param  string $siteId
100
	* @return string
101
	*/
102 5
	protected function getFilePath($siteId)
103
	{
104 5
		return $this->path . '/' . $siteId . '.xml';
105
	}
106
107
	/**
108
	* Extract a value from given element
109
	*
110
	* @param  DOMElement $element
111
	* @return mixed
112
	*/
113 2
	protected function getValueFromElement(DOMElement $element)
114
	{
115 2
		return (!$element->attributes->length && $element->childNodes->length === 1)
1 ignored issue
show
Bug introduced by
The property length does not seem to exist in DOMNamedNodeMap.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
116 2
		     ? $element->nodeValue
117 2
		     : $this->getElementConfig($element);
118
	}
119
120
	/**
121
	* {@inheritdoc}
122
	*/
123 2
	protected function getSiteConfig($siteId)
124
	{
125
		// Extract the site info from the node and put it into an array
126 2
		return $this->getConfigFromXmlFile($this->getFilePath($siteId));
127
	}
128
129
	/**
130
	* {@inheritdoc}
131
	*/
132 5
	protected function hasSiteConfig($siteId)
133
	{
134 5
		return file_exists($this->getFilePath($siteId));
135
	}
136
}