PdoTransaction::rollback()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\Pdo;
19
20
use Exception;
21
22
/**
23
 * Transaction for handling PDO connections.
24
 *
25
 */
26
class PdoTransaction
27
{
28
    /**
29
     * Driver to be used for transaction.
30
     *
31
     * @var PdoDriver
32
     */
33
    protected PdoDriver $driver;
34
35
    /**
36
     * Whether the transaction is finalized.
37
     * @var bool
38
     */
39
    protected bool $isFinalized = false;
40
41
    /**
42
     * Creates a new transaction from the driver.
43
     *
44
     * @param PdoDriver $driver Driver to be used
45
     * @return static
46
     */
47 5
    public static function create(PdoDriver $driver): static
48
    {
49 5
        $instance = new static();
50 5
        $instance->driver = $driver;
51
52 5
        $instance->driver->getPdo()->beginTransaction();
53
54 5
        return $instance;
55
    }
56
57
58
    /**
59
     * Executes a method and commits the result
60
     * if method throws an exception, transaction
61
     * is rolled back.
62
     *
63
     * @param callable $method Method to be executed.
64
     * @return mixed Result from the methods
65
     * @throws Exception
66
     */
67 2
    public function execute(callable $method): mixed
68
    {
69
        try {
70 2
            $result = $method($this);
71 1
            $this->commit();
72 1
            return $result;
73 1
        } catch (Exception $e) {
74 1
            $this->rollback();
75 1
            throw $e;
76
        }
77
    }
78
79
    /**
80
     * Commits all queries executed on the driver since this
81
     * transaction started.
82
     *
83
     * If this or rollback method is called before this method, subsequent
84
     * calls are ignored.
85
     *
86
     * @return void
87
     */
88 2
    public function commit(): void
89
    {
90 2
        if ($this->isFinalized) {
91 1
            return;
92
        }
93
94 2
        $this->driver->getPdo()->commit();
95 2
        $this->isFinalized = true;
96
    }
97
98
    /**
99
     * Rollbacks all queries executed on this driver since
100
     * this transaction started.
101
     *
102
     * If this or rollback method is called before this method, subsequent
103
     * calls are ignored.
104
     *
105
     * @return void
106
     */
107 3
    public function rollback(): void
108
    {
109 3
        if ($this->isFinalized) {
110 1
            return;
111
        }
112
113 3
        $this->driver->getPdo()->rollBack();
114 3
        $this->isFinalized = true;
115
    }
116
}
117