Passed
Push — master ( 3e0ae5...66dbef )
by Melech
01:47 queued 20s
created

SqlFileMigration::executeSql()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\Schema\Abstract;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Throwable;
18
use Valkyrja\Throwable\Exception\RuntimeException;
19
20
use function explode;
21
use function file_get_contents;
22
use function trim;
23
24
/**
25
 * Class SqlFileMigration.
26
 */
27
abstract class SqlFileMigration extends TransactionalMigration
28
{
29
    /**
30
     * @inheritDoc
31
     */
32
    #[Override]
33
    protected function runMigration(): void
34
    {
35
        $this->executeSql($this->getRunMigrationFilePath());
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    #[Override]
42
    protected function rollbackMigration(): void
43
    {
44
        $this->executeSql($this->getRollbackMigrationFilePath());
45
    }
46
47
    /**
48
     * Execute sql.
49
     *
50
     * @param string $filePath The sql file path
51
     *
52
     * @return void
53
     */
54
    protected function executeSql(string $filePath): void
55
    {
56
        $sql = file_get_contents($filePath);
57
58
        if ($sql === false) {
59
            throw new RuntimeException("Invalid file $filePath given");
60
        }
61
62
        foreach (explode(';', trim($sql)) as $queryString) {
63
            if (! $queryString) {
64
                continue;
65
            }
66
67
            $statement = $this->orm->prepare($queryString);
68
69
            if (! $statement->execute()) {
70
                throw new RuntimeException($statement->errorMessage() ?? 'Error occurred');
71
            }
72
        }
73
    }
74
75
    /**
76
     * Do on run failure.
77
     *
78
     * @param Throwable $exception The exception
79
     *
80
     * @return void
81
     */
82
    #[Override]
83
    protected function runFailure(Throwable $exception): void
84
    {
85
    }
86
87
    /**
88
     * Get the run sql file path.
89
     *
90
     * @return string
91
     */
92
    abstract protected function getRunMigrationFilePath(): string;
93
94
    /**
95
     * Get the rollback sql file path.
96
     *
97
     * @return string
98
     */
99
    abstract protected function getRollbackMigrationFilePath(): string;
100
}
101