Completed
Push — master ( b3f9bf...16b026 )
by Sam
03:02
created

YamlReader::readConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Jalle19\StatusManager\Configuration\Reader;
4
5
use Jalle19\StatusManager\Exception\InvalidConfigurationException;
6
use Symfony\Component\Yaml\Exception\ParseException;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Class YamlReader
11
 * @package   Jalle19\StatusManager\Configuration\Reader
12
 * @copyright Copyright &copy; Sam Stenvall 2016-
13
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
14
 */
15
class YamlReader implements ReaderInterface
16
{
17
18
	/**
19
	 * @var string
20
	 */
21
	private $_file;
22
23
24
	/**
25
	 * YamlReader constructor.
26
	 *
27
	 * @param string $file
28
	 */
29 3
	public function __construct($file)
30
	{
31 3
		$this->_file = $file;
32 3
	}
33
34
35
	/**
36
	 * @inheritdoc
37
	 */
38 3
	public function readConfiguration()
39
	{
40
		// Check that the configuration file is readable
41 3
		if (!is_readable($this->_file))
42 3
			throw new InvalidConfigurationException('The configuration file does not exist or is not readable');
43
44
		// Parse the configuration file
45
		try
46
		{
47 2
			return (array)Yaml::parse(file_get_contents($this->_file));
48
		}
49 1
		catch (ParseException $e)
50
		{
51 1
			throw new InvalidConfigurationException('Failed to parse the specified configuration file: ' . $e->getMessage());
52
		}
53
	}
54
55
}
56