FlysystemFixtureLoader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 40
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseDir() 0 4 1
A __construct() 0 5 1
A load() 0 19 3
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Fixture;
10
11
use Daikon\Flysystem\Connector\FlysystemConnector;
12
use League\Flysystem\MountManager;
13
use Psr\Container\ContainerInterface;
14
15
final class FlysystemFixtureLoader implements FixtureLoaderInterface
16
{
17
    private ContainerInterface $container;
18
19
    private FlysystemConnector $connector;
20
21
    private array $settings;
22
23
    public function __construct(ContainerInterface $container, FlysystemConnector $connector, array $settings = [])
24
    {
25
        $this->container = $container;
26
        $this->connector = $connector;
27
        $this->settings = $settings;
28
    }
29
30
    public function load(): FixtureList
31
    {
32
        /** @var MountManager $filesystem */
33
        $filesystem = $this->connector->getConnection();
34
        $contents = $filesystem->listContents($this->settings['location'], true);
35
        $fixtureFiles = array_filter($contents, function (array $fileinfo): bool {
36
            return isset($fileinfo['extension']) && $fileinfo['extension'] === 'php';
37
        });
38
39
        $fixtures = [];
40
        foreach ($fixtureFiles as $fixtureFile) {
41
            // @todo better way to include fixture classes
42
            $declaredClasses = get_declared_classes();
43
            require_once $this->getBaseDir().'/'.$fixtureFile['path'];
44
            $fixtureClass = current(array_diff(get_declared_classes(), $declaredClasses));
45
            $fixtures[] = $this->container->get($fixtureClass);
46
        }
47
48
        return new FixtureList($fixtures);
49
    }
50
51
    private function getBaseDir(): string
52
    {
53
        $connectorSettings = $this->connector->getSettings();
54
        return $connectorSettings['mounts']['fixture']['location'];
55
    }
56
}
57