MongoFixturesLoader   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 120
ccs 40
cts 46
cp 0.8696
rs 10
c 1
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFromDirectory() 0 12 2
A getLoadedClasses() 0 3 1
A loadFromFile() 0 9 2
A addInstance() 0 6 2
A buildFixture() 0 7 2
A loadFromIterator() 0 32 5
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Fixtures;
6
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
final class MongoFixturesLoader
10
{
11
    /** @var array|MongoFixtureInterface[] */
12
    private $loadedClasses;
13
14
    /** @var ContainerInterface */
15
    private $container;
16
17
    public function __construct(ContainerInterface $container)
18
    {
19
        $this->container = $container;
20
    }
21
22 3
    /**
23
     * @param string $dir
24 3
     *
25 3
     * @return array
26
     */
27
    public function loadFromDirectory(string $dir)
28
    {
29
        if (! is_dir($dir)) {
30
            throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir));
31
        }
32 2
33
        $iterator = new \RecursiveIteratorIterator(
34 2
            new \RecursiveDirectoryIterator($dir),
35
            \RecursiveIteratorIterator::LEAVES_ONLY
36
        );
37
38 2
        return $this->loadFromIterator($iterator);
39 2
    }
40 2
41
    /**
42
     * @param \Iterator $iterator
43 2
     *
44
     * @return array
45
     */
46
    private function loadFromIterator(\Iterator $iterator)
47
    {
48
        $includedFiles = [];
49
        foreach ($iterator as $file) {
50
            if ($file->getBasename('.php') == $file->getBasename()) {
51 2
                continue;
52
            }
53 2
            $sourceFile = realpath($file->getPathName());
54 2
            require_once $sourceFile;
55 2
            $includedFiles[] = $sourceFile;
56 2
        }
57
58 2
        $declared = get_declared_classes();
59 2
60 2
        return array_reduce(
61
            $declared,
62
            function ($classList, string $className) use ($includedFiles) {
63 2
                $reflClass = new \ReflectionClass($className);
64
                $sourceFile = $reflClass->getFileName();
65 2
66 2
                if (
67 2
                    \in_array($sourceFile, $includedFiles) &&
68 2
                    \array_key_exists(MongoFixtureInterface::class, $reflClass->getInterfaces())
69 2
                ) {
70
                    $instance = $this->buildFixture(new $className());
71
                    $this->addInstance($instance);
72 2
                    $classList[] = $instance;
73 2
                }
74
75 2
                return $classList;
76 2
            },
77 2
            []
78
        );
79
    }
80 2
81 2
    /**
82 2
     * @param mixed $instance
83
     *
84
     * @return MongoFixtureInterface
85
     */
86
    private function buildFixture($instance): MongoFixtureInterface
87
    {
88
        if ($instance instanceof AbstractContainerAwareFixture) {
89
            $instance->setContainer($this->container);
90
        }
91 2
92
        return $instance;
93 2
    }
94 2
95
    /**
96
     * @param MongoFixtureInterface $list
97 2
     */
98
    public function addInstance(MongoFixtureInterface $list)
99
    {
100
        $listClass = \get_class($list);
101
102
        if (! isset($this->loadedClasses[$listClass])) {
103 2
            $this->loadedClasses[$listClass] = $list;
104
        }
105 2
    }
106
107 2
    /**
108 2
     * @param string $fileName
109
     *
110 2
     * @return array
111
     */
112
    public function loadFromFile(string $fileName)
113
    {
114
        if (! is_readable($fileName)) {
115
            throw new \InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName));
116
        }
117
118
        $iterator = new \ArrayIterator([new \SplFileInfo($fileName)]);
119
120
        return $this->loadFromIterator($iterator);
121
    }
122
123
    /**
124
     * @return array|MongoFixtureInterface[]
125
     */
126
    public function getLoadedClasses()
127
    {
128
        return $this->loadedClasses;
129
    }
130
}
131