Completed
Push — master ( 8482f5...6f4e0b )
by max
02:05
created

Resolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace T4web\Migrations\Version;
3
4
use ArrayIterator;
5
use GlobIterator;
6
use FilesystemIterator;
7
use ReflectionClass;
8
use ReflectionProperty;
9
use T4web\Migrations\Config;
10
use T4web\Migrations\Migration\MigrationInterface;
11
12
class Resolver
13
{
14
    /**
15
     * @var Config
16
     */
17
    private $config;
18
19
    /**
20
     * @var Table
21
     */
22
    private $versionTable;
23
24
    public function __construct(Config $config, $versionTable)
25
    {
26
        $this->config = $config;
27
        $this->versionTable = $versionTable;
28
    }
29
30
    public function getAll($all = false)
31
    {
32
        $classes = new ArrayIterator();
33
34
        $iterator = new GlobIterator(
35
            sprintf('%s/Version_*.php', $this->config->getDir()),
36
            FilesystemIterator::KEY_AS_FILENAME
37
        );
38
        foreach ($iterator as $item) {
39
            /** @var $item \SplFileInfo */
40
            if (preg_match('/(Version_(\d+))\.php/', $item->getFilename(), $matches)) {
41
                $applied = $this->versionTable->applied($matches[2]);
42
                if ($all || !$applied) {
43
                    $className = $this->config->getNamespace() . '\\' . $matches[1];
44
45
                    if (!class_exists($className)) { /** @noinspection PhpIncludeInspection */
46
                        require_once $this->config->getDir() . '/' . $item->getFilename();
47
                    }
48
49
                    if (class_exists($className)) {
50
                        $reflectionClass = new ReflectionClass($className);
51
                        $reflectionDescription = new ReflectionProperty($className, 'description');
52
53
                        if ($reflectionClass->implementsInterface(MigrationInterface::class)) {
54
                            $classes->append(
55
                                [
56
                                    'version' => $matches[2],
57
                                    'class' => $className,
58
                                    'description' => $reflectionDescription->getValue(),
59
                                    'applied' => $applied,
60
                                ]
61
                            );
62
                        }
63
                    }
64
                }
65
            }
66
        }
67
68
        $classes->uasort(
69 View Code Duplication
            function ($a, $b) {
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...
70
                if ($a['version'] == $b['version']) {
71
                    return 0;
72
                }
73
74
                return ($a['version'] < $b['version']) ? -1 : 1;
75
            }
76
        );
77
78
        return $classes;
79
    }
80
}
81