Completed
Push — master ( a46bee...2cce8e )
by Gaetano
09:14
created

Filesystem::loadDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 Symfony\Component\HttpKernel\KernelInterface;
10
11
/**
12
 * Loads migration definitions from disk (by default from a specific dir in enabled bundles)
13
 */
14
class Filesystem implements LoaderInterface
15
{
16
    /**
17
     * Name of the directory where migration versions are located
18
     * @var string
19
     */
20
    protected $versionDirectory;
21
    protected $kernel;
22
23
    /**
24
     * Filesystem constructor.
25
     * @param KernelInterface $kernel
26
     * @param string $versionDirectoryParameter name of folder when $configResolver is null; name of parameter when it is not
27
     * @param ConfigResolverInterface $configResolver
28
     * @throws \Exception
29
     */
30
    public function __construct(KernelInterface $kernel, $versionDirectoryParameter = 'Migrations', ConfigResolverInterface $configResolver = null)
31
    {
32
        $this->kernel = $kernel;
33
        $this->versionDirectory = $configResolver ? $configResolver->getParameter($versionDirectoryParameter) : $versionDirectoryParameter;
34
    }
35
36
    /**
37
     * @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir
38
     * @return MigrationDefinition[] migrations definitions. key: name, value: file path
39
     * @throws \Exception
40
     */
41
    public function listAvailableDefinitions(array $paths = array())
42
    {
43
        return $this->getDefinitions($paths, true);
0 ignored issues
show
Best Practice introduced by
The expression return $this->getDefinitions($paths, true); seems to be an array, but some of its elements' types (Kaliop\eZMigrationBundle...lue\MigrationDefinition) are incompatible with the return type declared by the interface Kaliop\eZMigrationBundle...istAvailableDefinitions of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
44
    }
45
46
    /**
47
     * @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir
48
     * @return MigrationDefinitionCollection migrations definitions. key: name, value: contents of the definition, as string
49
     * @throws \Exception
50
     */
51
    public function loadDefinitions(array $paths = array())
52
    {
53
        return new MigrationDefinitionCollection($this->getDefinitions($paths, false));
54
    }
55
56
    /**
57
     * @param array $paths either dir names or file names
58
     * @param bool $returnFilename return either the
59
     * @return MigrationDefinition[]|string[] migrations definitions. key: name, value: contents of the definition, as string or file path
60
     * @throws \Exception
61
     */
62
    protected function getDefinitions(array $paths = array(), $returnFilename = false)
63
    {
64
        // if no paths defined, we look in all bundles
65 View Code Duplication
        if (empty($paths)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $paths = array();
67
            /** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */
68
            foreach ($this->kernel->getBundles() as $bundle)
69
            {
70
                $path = $bundle->getPath() . "/" . $this->versionDirectory;
71
                if (is_dir($path)) {
72
                    $paths[] = $path;
73
                }
74
            }
75
        }
76
77
        $definitions = array();
78
        foreach ($paths as $path) {
79
            if (is_file($path)) {
80
                $definitions[basename($path)] = $returnFilename ? $path : new MigrationDefinition(
81
                    basename($path),
82
                    $path,
83
                    file_get_contents($path)
84
                );
85
            } elseif (is_dir($path)) {
86
                foreach (new \DirectoryIterator($path) as $file) {
87
                    if ($file->isFile()) {
88
                        $definitions[$file->getFilename()] =
89
                            $returnFilename ? $file->getRealPath() : new MigrationDefinition(
90
                                $file->getFilename(),
91
                                $file->getRealPath(),
92
                                file_get_contents($file->getRealPath())
93
                            );
94
                    }
95
                }
96
            } else {
97
                throw new \Exception("Path '$path' is neither a file nor directory");
98
            }
99
        }
100
        ksort($definitions);
101
102
        return $definitions;
103
    }
104
}
105