Completed
Pull Request — master (#10)
by Alice
02:23
created

YamlParser::loadFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 4
1
<?php
2
3
namespace Wonderland\Container\Configuration\Parser;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Wonderland\Container\Exception\InvalidResourceException;
7
use Wonderland\Container\Exception\ResourceNotFoundException;
8
9
/**
10
 * Class Yaml
11
 * @package Wonderland\Container\Configuration\Parser
12
 * @author Alice Praud <[email protected]>
13
 */
14
class YamlParser implements ParserInterface
15
{
16
17
	/**
18
	 * @param string $filePath
19
	 * @return array
20
	 * @throws ResourceNotFoundException
21
	 * @throws InvalidResourceException
22
	 */
23 14
	public function loadFile(string $filePath): array
24
	{
25 14
		if (false === file_exists($filePath)) {
26 1
			throw new ResourceNotFoundException('The file "' . $filePath . '" does not exists');
27
		}
28
29 13
		if (false === is_file($filePath)) {
30 1
			throw new InvalidResourceException('The resource "' . $filePath . '" is not a file');
31
		}
32
33 12
		$parse = Yaml::parseFile($filePath);
34
35 12
		return null === $parse ? [] : $parse;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null === $parse ? array() : $parse also could return the type Symfony\Component\Yaml\Tag\TaggedValue|string which is incompatible with the return type mandated by Wonderland\Container\Con...erInterface::loadFile() of array.
Loading history...
36
	}
37
38
	/**
39
	 * @param string $directorPath
40
	 * @return array
41
	 * @throws InvalidResourceException
42
	 * @throws ResourceNotFoundException
43
	 */
44 4
	public function loadDirectory(string $directorPath): array
45
	{
46 4
		if (false === file_exists($directorPath)) {
47 1
			throw new ResourceNotFoundException('The directory "' . $directorPath . '" does not exists');
48
		}
49
50 3
		if (false === is_dir($directorPath)) {
51 1
			throw new InvalidResourceException('The resource "' . $directorPath . '" is not a directory');
52
		}
53
54 2
		$config = [];
55
56 2
		$files = array_diff(scandir($directorPath), array('.', '..'));
57 2
		foreach ($files as $file) {
58 2
			if (false !== preg_match('/^.+(yml|yaml)$/i', $file)) {
59 2
				$config[$file] = Yaml::parseFile(realpath($directorPath) . DIRECTORY_SEPARATOR . $file);
60
			}
61
		}
62
63 2
		return $config;
64
	}
65
66
}
67