FlysystemFixtureLoader::getBaseDir()   A
last analyzed

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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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