Passed
Push — master ( 376082...008670 )
by Paul
04:04
created

DirectoryLoader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isReadable() 0 12 2
A checkFileType() 0 4 2
A createLoader() 0 11 3
A scanDirectory() 0 15 3
A load() 0 13 3
1
<?php
2
3
namespace CCT\Component\Rest\Serializer\Mapping\Loader;
4
5
use InvalidArgumentException;
6
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
7
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
8
9
/**
10
 * Load recursively all YAML or XML configuration files in services directories
11
 */
12
class DirectoryLoader
13
{
14
    public const FILE_TYPE_YAML = 'yaml';
15
    public const FILE_TYPE_XML = 'xml';
16
17
    /**
18
     * @param $dir
19
     * @param string $type supported types are DirectoryLoader::FILE_TYPE_YAML or DirectoryLoader::FILE_TYPE_XML
20
     *
21
     * @return array
22
     * @throws \Symfony\Component\Serializer\Exception\MappingException
23
     * @throws \InvalidArgumentException
24
     */
25 7
    public function load($dir, $type = self::FILE_TYPE_YAML): array
26
    {
27 7
        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
28
29 7
        if (!is_dir($dir)) {
30 1
            throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
31
        }
32
33 6
        if (!is_readable($dir)) {
34 1
            throw new InvalidArgumentException(sprintf('The directory "%s" is not readable.', $dir));
35
        }
36
37 5
        return $this->scanDirectory($dir, $type);
38
    }
39
40
    /**
41
     * Scan directory for files matching extension type
42
     *
43
     * @param string $dir
44
     * @param string $type
45
     *
46
     * @return array
47
     * @throws \InvalidArgumentException
48
     * @throws \Symfony\Component\Serializer\Exception\MappingException
49
     */
50 5
    private function scanDirectory($dir, $type): array
51
    {
52 5
        $loaders = [];
53
54 5
        foreach (scandir($dir, SCANDIR_SORT_NONE) as $file) {
55 5
            if (false === $this->checkFileType($file, $type)) {
56 4
                continue;
57
            }
58
59 3
            $this->isReadable($dir, $file);
60
61 3
            $loaders[] = $this->createLoader($dir . DIRECTORY_SEPARATOR . $file, $type);
62
        }
63
64 4
        return $loaders;
65
    }
66
67
    /**
68
     * Checks if file extension matches type
69
     *
70
     * @param string $file
71
     * @param string $type
72
     *
73
     * @return bool
74
     */
75 5
    private function checkFileType($file, $type): bool
76
    {
77 5
        return !(\in_array($file, array('.', '..'))
78 5
            || $type !== strtolower(pathinfo($file, PATHINFO_EXTENSION)));
79
    }
80
81
    /**
82
     * Is file readable
83
     *
84
     * @param string $dir
85
     * @param string $file
86
     *
87
     * @return bool
88
     * @throws \InvalidArgumentException
89
     */
90 3
    private function isReadable($dir, $file): bool
91
    {
92 3
        if (!is_readable($dir . DIRECTORY_SEPARATOR . $file)) {
93
            throw new InvalidArgumentException(
94
                sprintf(
95
                    'The file "%s" is not readable.',
96
                    $dir . DIRECTORY_SEPARATOR . $file
97
                )
98
            );
99
        }
100
101 3
        return true;
102
    }
103
104
    /**
105
     * Create config loaders
106
     *
107
     * @param $file
108
     * @param $type
109
     *
110
     * @return XmlFileLoader|YamlFileLoader
111
     * @throws \InvalidArgumentException
112
     * @throws \Symfony\Component\Serializer\Exception\MappingException
113
     */
114 3
    private function createLoader($file, $type)
115
    {
116 3
        if (self::FILE_TYPE_YAML === $type) {
117 1
            return new YamlFileLoader($file);
118
        }
119
120 2
        if (self::FILE_TYPE_XML === $type) {
121 1
            return new XmlFileLoader($file);
122
        }
123
124 1
        throw new InvalidArgumentException(sprintf('The file type "%s" is not supported.', $type));
125
    }
126
}
127