Passed
Branch main (b0ee7b)
by Gaetano
09:00
created

Filesystem::getDefinitions()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 25
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 41
rs 7.3166

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\eZWorkflowEngineBundle\Core\Loader;
4
5
use Kaliop\eZMigrationBundle\Core\Loader\Filesystem as BaseLoader;
6
use Kaliop\eZWorkflowEngineBundle\API\Value\WorkflowDefinition;
7
use Kaliop\eZMigrationBundle\API\ConfigResolverInterface;
8
use Symfony\Component\HttpKernel\KernelInterface;
9
10
class Filesystem extends BaseLoader
11
{
12
    public function __construct(KernelInterface $kernel, $versionDirectoryParameter = 'Workflows', ConfigResolverInterface $configResolver = null)
13
    {
14
        $this->versionDirectory = $configResolver ? $configResolver->getParameter($versionDirectoryParameter) : $versionDirectoryParameter;
15
        $this->kernel = $kernel;
16
    }
17
18
    /**
19
     * @param array $paths either dir names or file names
20
     * @param bool $returnFilename return either the
21
     * @return WorkflowDefinition[]|string[] migrations definitions. key: name, value: contents of the definition, as string or file path
22
     * @throws \Exception
23
     */
24
    protected function getDefinitions(array $paths = array(), $returnFilename = false)
25
    {
26
        // if no paths defined, we look in all bundles
27
        if (empty($paths)) {
28
            $paths = array();
29
            /** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */
30
            foreach ($this->kernel->getBundles() as $bundle)
31
            {
32
                $path = $bundle->getPath() . "/" . $this->versionDirectory;
33
                if (is_dir($path)) {
34
                    $paths[] = $path;
35
                }
36
            }
37
        }
38
39
        $definitions = array();
40
        foreach ($paths as $path) {
41
            if (is_file($path)) {
42
                $definitions[basename($path)] = $returnFilename ? $path : new WorkflowDefinition(
43
                    basename($path),
44
                    $path,
45
                    file_get_contents($path)
46
                );
47
            } elseif (is_dir($path)) {
48
                foreach (new \DirectoryIterator($path) as $file) {
49
                    if ($file->isFile()) {
50
                        $definitions[$file->getFilename()] =
51
                            $returnFilename ? $file->getRealPath() : new WorkflowDefinition(
52
                                $file->getFilename(),
53
                                $file->getRealPath(),
54
                                file_get_contents($file->getRealPath())
55
                            );
56
                    }
57
                }
58
            } else {
59
                throw new \Exception("Path '$path' is neither a file nor directory");
60
            }
61
        }
62
        ksort($definitions);
63
64
        return $definitions;
65
    }
66
}
67