Passed
Push — master ( a4f7f2...d3cc39 )
by Melech
04:09
created

TransactionalMigration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 19
c 0
b 0
f 0
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runFailure() 0 2 1
A run() 0 16 2
A rollback() 0 16 2
A rollbackFailure() 0 2 1
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;
15
16
use Throwable;
17
18
/**
19
 * Abstract Class TransactionalMigration.
20
 *
21
 * @author Melech Mizrachi
22
 */
23
abstract class TransactionalMigration extends Migration
24
{
25
    /**
26
     * @inheritDoc
27
     *
28
     * @throws Throwable
29
     */
30
    public function run(): void
31
    {
32
        $orm = $this->orm;
33
34
        try {
35
            $orm->ensureTransaction();
36
37
            $this->runMigration();
38
39
            $orm->commit();
40
        } catch (Throwable $exception) {
41
            $orm->rollback();
42
43
            $this->runFailure($exception);
44
45
            throw $exception;
46
        }
47
    }
48
49
    /**
50
     * @inheritDoc
51
     *
52
     * @throws Throwable
53
     */
54
    public function rollback(): void
55
    {
56
        $orm = $this->orm;
57
58
        try {
59
            $orm->ensureTransaction();
60
61
            $this->rollbackMigration();
62
63
            $orm->commit();
64
        } catch (Throwable $exception) {
65
            $orm->rollback();
66
67
            $this->rollbackFailure($exception);
68
69
            throw $exception;
70
        }
71
    }
72
73
    /**
74
     * Do on run failure.
75
     *
76
     * @param Throwable $exception The exception
77
     *
78
     * @return void
79
     */
80
    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

80
    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...
81
    {
82
    }
83
84
    /**
85
     * Do on rollback failure.
86
     *
87
     * @param Throwable $exception The exception
88
     *
89
     * @return void
90
     */
91
    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

91
    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...
92
    {
93
    }
94
95
    /**
96
     * Run the migration.
97
     *
98
     * @return void
99
     */
100
    abstract protected function runMigration(): void;
101
102
    /**
103
     * Rollback the migration.
104
     *
105
     * @return void
106
     */
107
    abstract protected function rollbackMigration(): void;
108
}
109