SqlSchemaCreator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Event Sourcing implementation module.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcingModule;
14
15
use function Amp\call;
16
use Amp\Promise;
17
use ServiceBus\Storage\Common\DatabaseAdapter;
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
final class SqlSchemaCreator
23
{
24
    private const FIXTURES = [
25
        /** common fixtures */
26
        '/src/EventStream/Store/schema/extensions.sql'                => false,
27
        /** event streams */
28
        '/src/EventStream/Store/schema/event_store_stream.sql'        => false,
29
        '/src/EventStream/Store/schema/event_store_stream_events.sql' => false,
30
        '/src/EventStream/Store/schema/indexes.sql'                   => true,
31
        /** snapshots */
32
        '/src/Snapshots/Store/schema/event_store_snapshots.sql'       => false,
33
        '/src/Snapshots/Store/schema/indexes.sql'                     => true,
34
        /** indexer */
35
        '/src/Indexes/Store/schema/event_sourcing_indexes.sql'        => false,
36
    ];
37
38
    /**
39
     * @var DatabaseAdapter
40
     */
41
    private $adapter;
42
43
    /**
44
     * @var string
45
     */
46
    private $rootDirectoryPath;
47
48
    /**
49
     * @param DatabaseAdapter $adapter
50
     * @param string          $rootDirectoryPath
51
     */
52
    public function __construct(DatabaseAdapter $adapter, string $rootDirectoryPath)
53
    {
54
        $this->adapter           = $adapter;
55
        $this->rootDirectoryPath = \rtrim($rootDirectoryPath, '/');
56
    }
57
58
    /**
59
     * Import fixtures.
60
     *
61
     * @return Promise
62
     */
63
    public function import(): Promise
64
    {
65
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
66
        return call(
67
            function(array $fixtures): \Generator
68
            {
69
                /**
70
                 * @var string $filePath
71
                 * @var bool   $multipleQueries
72
                 */
73
                foreach ($fixtures as $filePath => $multipleQueries)
74
                {
75
                    $filePath = $this->rootDirectoryPath . $filePath;
76
77
                    $queries = true === $multipleQueries
78
                        ? \array_map('trim', \file($filePath))
0 ignored issues
show
Bug introduced by
It seems like file($filePath) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                        ? \array_map('trim', /** @scrutinizer ignore-type */ \file($filePath))
Loading history...
79
                        : [(string) \file_get_contents($filePath)];
80
81
                    foreach ($queries as $query)
82
                    {
83
                        if ('' !== $query)
84
                        {
85
                            /** @psalm-suppress TooManyTemplateParams Wrong Promise template */
86
                            yield $this->adapter->execute($query);
87
                        }
88
                    }
89
                }
90
            },
91
            self::FIXTURES
92
        );
93
    }
94
}
95