Completed
Push — master ( e32eeb...714cda )
by
unknown
89:50 queued 46:29
created

getConfigDirectories()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace Oro\Bundle\WorkflowBundle\Configuration;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Yaml\Yaml;
8
9
abstract class AbstractConfigurationProvider
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $configDirectory = '/Resources/config/';
15
16
    /**
17
     * @var array
18
     */
19
    protected $kernelBundles = array();
20
21
    /**
22
     * @param array $kernelBundles
23
     */
24
    public function __construct(array $kernelBundles)
25
    {
26
        $this->kernelBundles = $kernelBundles;
27
    }
28
29
    /**
30
     * @param array $directoriesWhiteList
31
     * @return Finder
32
     */
33
    protected function getConfigFinder(array $directoriesWhiteList = array())
34
    {
35
        $configDirectories = $this->getConfigDirectories($directoriesWhiteList);
0 ignored issues
show
Unused Code introduced by
The call to AbstractConfigurationPro...:getConfigDirectories() has too many arguments starting with $directoriesWhiteList.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
36
37
        // prepare finder
38
        $finder = new Finder();
39
        $finder->in($configDirectories)->name($this->getConfigFilePattern());
40
41
        if ($directoriesWhiteList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $directoriesWhiteList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            $finder->filter(
43
                function ($file) use ($directoriesWhiteList) {
44
                    foreach ($directoriesWhiteList as $allowedDirectory) {
45
                        if ($allowedDirectory &&
46
                            strpos($file, realpath($allowedDirectory) . DIRECTORY_SEPARATOR) === 0
47
                        ) {
48
                            return true;
49
                        }
50
                    }
51
                    return false;
52
                }
53
            );
54
        }
55
56
        return $finder;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    protected function getConfigDirectories()
63
    {
64
        $configDirectory = str_replace('/', DIRECTORY_SEPARATOR, $this->configDirectory);
65
        $configDirectories = array();
66
67
        foreach ($this->kernelBundles as $bundle) {
68
            $reflection = new \ReflectionClass($bundle);
69
            $bundleConfigDirectory = dirname($reflection->getFilename()) . $configDirectory;
70
            if (is_dir($bundleConfigDirectory) && is_readable($bundleConfigDirectory)) {
71
                $configDirectories[] = realpath($bundleConfigDirectory);
72
            }
73
        }
74
75
        return $configDirectories;
76
    }
77
78
    /**
79
     * Load config file and include imports.
80
     *
81
     * @param \SplFileInfo $file
82
     * @throws InvalidConfigurationException
83
     * @return array
84
     */
85
    protected function loadConfigFile(\SplFileInfo $file)
86
    {
87
        $realPathName = $file->getRealPath();
88
        $configData = Yaml::parse(file_get_contents($realPathName)) ? : [];
89
90
        if (array_key_exists('imports', $configData) && is_array($configData['imports'])) {
91
            $imports = $configData['imports'];
92
            unset($configData['imports']);
93
            foreach ($imports as $importData) {
94
                if (array_key_exists('resource', $importData)) {
95
                    $resourceFile = new \SplFileInfo($file->getPath() . DIRECTORY_SEPARATOR . $importData['resource']);
96
                    if ($resourceFile->isReadable()) {
97
                        $includedData = $this->loadConfigFile($resourceFile);
98
                        $configData = array_merge_recursive($configData, $includedData);
99
                    } else {
100
                        throw new InvalidConfigurationException(
101
                            sprintf('Resource "%s" is unreadable', $resourceFile->getBasename())
102
                        );
103
                    }
104
                }
105
            }
106
        }
107
108
        return $configData;
109
    }
110
111
    /**
112
     * Returns file pattern to find
113
     *
114
     * @return string
115
     */
116
    abstract protected function getConfigFilePattern();
117
}
118