Filesystem::getDefinitions()   C
last analyzed

Complexity

Conditions 13
Paths 44

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 14.3519

Importance

Changes 0
Metric Value
cc 13
eloc 30
c 0
b 0
f 0
nc 44
nop 2
dl 0
loc 50
ccs 24
cts 30
cp 0.8
crap 14.3519
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Loader;
4
5
use Kaliop\eZMigrationBundle\API\LoaderInterface;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
7
use Kaliop\eZMigrationBundle\API\Collection\MigrationDefinitionCollection;
8
use Kaliop\eZMigrationBundle\API\ConfigResolverInterface;
9
use Kaliop\eZMigrationBundle\API\Exception\MigrationBundleException;
10
use Symfony\Component\HttpKernel\KernelInterface;
11
12
/**
13
 * Loads migration definitions from disk (by default from a specific dir in enabled bundles)
14
 */
15
class Filesystem implements LoaderInterface
16
{
17
    /**
18
     * Name of the directory where migration versions are located
19
     * @var string
20
     */
21
    protected $versionDirectory;
22
    protected $kernel;
23
24
    /**
25
     * Filesystem constructor.
26
     * @param KernelInterface $kernel
27
     * @param string $versionDirectoryParameter name of folder when $configResolver is null; name of parameter when it is not
28
     * @param ConfigResolverInterface $configResolver
29
     * @throws \Exception
30
     */
31 149
    public function __construct(KernelInterface $kernel, $versionDirectoryParameter = 'Migrations', ConfigResolverInterface $configResolver = null)
32
    {
33 149
        $this->kernel = $kernel;
34 149
        $this->versionDirectory = $configResolver ? $configResolver->getParameter($versionDirectoryParameter) : $versionDirectoryParameter;
35 149
    }
36
37
    /**
38
     * @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir
39
     * @return MigrationDefinition[] migrations definitions. key: name, value: file path
40
     * @throws \Exception
41
     */
42 145
    public function listAvailableDefinitions(array $paths = array())
43
    {
44 145
        return $this->getDefinitions($paths, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getDefinitions($paths, true) returns an array which contains values of type string which are incompatible with the documented value type Kaliop\eZMigrationBundle...lue\MigrationDefinition.
Loading history...
45
    }
46
47
    /**
48
     * @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir
49
     * @return MigrationDefinitionCollection migrations definitions. key: name, value: contents of the definition, as string
50
     * @throws \Exception
51
     */
52 145
    public function loadDefinitions(array $paths = array())
53
    {
54 145
        return new MigrationDefinitionCollection($this->getDefinitions($paths, false));
55
    }
56
57
    /**
58
     * @param array $paths either dir names or file names
59
     * @param bool $returnFilename return either the
60
     * @return MigrationDefinition[]|string[] migrations definitions. key: name, value: contents of the definition, as string or file path
61
     * @throws \Exception
62
     */
63 145
    protected function getDefinitions(array $paths = array(), $returnFilename = false)
64
    {
65
        // if no paths defined, we look in all bundles
66 145
        if (empty($paths)) {
67 46
            /** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */
68
            foreach ($this->kernel->getBundles() as $bundle)
69 46
            {
70
                $path = $bundle->getPath() . "/" . $this->versionDirectory;
71 46
                if (is_dir($path)) {
72 46
                    $paths[] = $path;
73 46
                }
74
            }
75
        }
76
77
        $rootDir = realpath($this->kernel->getRootDir() . '/..') . '/';
78 145
79
        $definitions = array();
80 145
        foreach ($paths as $path) {
81 145
            // we normalize all paths and try to make them relative to the root dir, both in input and output
82
            if ($path === './') {
83 145
                $path = $rootDir;
84
            } elseif ($path[0] !== '/') {
85 145
                $path = $rootDir . $path;
86
            }
87
88
            if (is_file($path)) {
89 145
                $path = realpath($path);
90 145
                $definitions[basename($path)] = $returnFilename ? $this->normalizePath($path) : new MigrationDefinition(
91 145
                    basename($path),
92 145
                    $this->normalizePath($path),
93 145
                    file_get_contents($path)
94 145
                );
95
            } elseif (is_dir($path)) {
96 46
                foreach (new \DirectoryIterator($path) as $file) {
97 46
                    if ($file->isFile()) {
98 46
                        $definitions[$file->getFilename()] =
99 46
                            $returnFilename ? $this->normalizePath($file->getRealPath()) : new MigrationDefinition(
100 46
                                $file->getFilename(),
101
                                $this->normalizePath($file->getRealPath()),
102
                                file_get_contents($file->getRealPath())
103
                            );
104
                    }
105
                }
106
            } else {
107
                throw new MigrationBundleException("Path '$path' is neither a file nor directory");
108
            }
109
        }
110
        ksort($definitions);
111 145
112
        return $definitions;
113 145
    }
114
115
    /**
116
     * @param string $path should be an absolute path
117
     * @return string the same path, but relative to current root dir if it is a subpath
118
     */
119
    protected function normalizePath($path)
120 145
    {
121
        $rootDir = realpath($this->kernel->getRootDir() . '/..') . '/';
122 145
        // note: we handle the case of 'path = root dir', but path is expected to include a filename...
123
        return $path === $rootDir ? './' : preg_replace('#^' . preg_quote($rootDir, '#'). '#', '', $path);
124 145
    }
125
}
126