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

TransactionalMigration::rollback()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 4
nop 0
dl 0
loc 17
rs 9.9332
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
19
/**
20
 * Abstract Class TransactionalMigration.
21
 *
22
 * @author Melech Mizrachi
23
 */
24
abstract class TransactionalMigration extends Migration
25
{
26
    /**
27
     * @inheritDoc
28
     *
29
     * @throws Throwable
30
     */
31
    #[Override]
32
    public function run(): void
33
    {
34
        $orm = $this->orm;
35
36
        try {
37
            $orm->ensureTransaction();
38
39
            $this->runMigration();
40
41
            $orm->commit();
42
        } catch (Throwable $exception) {
43
            $orm->rollback();
44
45
            $this->runFailure($exception);
46
47
            throw $exception;
48
        }
49
    }
50
51
    /**
52
     * @inheritDoc
53
     *
54
     * @throws Throwable
55
     */
56
    #[Override]
57
    public function rollback(): void
58
    {
59
        $orm = $this->orm;
60
61
        try {
62
            $orm->ensureTransaction();
63
64
            $this->rollbackMigration();
65
66
            $orm->commit();
67
        } catch (Throwable $exception) {
68
            $orm->rollback();
69
70
            $this->rollbackFailure($exception);
71
72
            throw $exception;
73
        }
74
    }
75
76
    /**
77
     * Do on run failure.
78
     *
79
     * @param Throwable $exception The exception
80
     *
81
     * @return void
82
     */
83
    protected function runFailure(Throwable $exception): void
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

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

83
    protected function runFailure(/** @scrutinizer ignore-unused */ Throwable $exception): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
    }
86
87
    /**
88
     * Do on rollback failure.
89
     *
90
     * @param Throwable $exception The exception
91
     *
92
     * @return void
93
     */
94
    protected function rollbackFailure(Throwable $exception): void
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

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

94
    protected function rollbackFailure(/** @scrutinizer ignore-unused */ Throwable $exception): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
    }
97
98
    /**
99
     * Run the migration.
100
     *
101
     * @return void
102
     */
103
    abstract protected function runMigration(): void;
104
105
    /**
106
     * Rollback the migration.
107
     *
108
     * @return void
109
     */
110
    abstract protected function rollbackMigration(): void;
111
}
112