Passed
Push — master ( b85cf8...7514fb )
by Aleksandar
02:26
created

PdoTransaction::execute()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 7
cts 7
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
class PdoTransaction
26
{
27
    /**
28
     * Driver to be used for transaction.
29
     *
30
     * @var PdoDriver
31
     */
32
    protected PdoDriver $driver;
33
34
    /**
35
     * Whether the transaction is finalized.
36
     * @var bool
37
     */
38
    protected bool $isFinalized = false;
39
40
    /**
41
     * Creates a new transaction from the driver.
42
     *
43
     * @param PdoDriver $driver Driver to be used
44
     * @return static
45
     */
46 3
    public static function create(PdoDriver $driver): static
47
    {
48 3
        $instance = new static();
49 3
        $instance->driver = $driver;
50
51 3
        $instance->driver->getPdo()->beginTransaction();
52
53 3
        return $instance;
54
    }
55
56
57
    /**
58
     * Executes a method and commits the result
59
     * if method throws an exception, transaction
60
     * is rolled back.
61
     *
62
     * @param callable $method Method to be executed.
63
     * @return mixed Result from the methods
64
     * @throws Exception
65
     */
66 2
    public function execute(callable $method): mixed
67
    {
68
        try {
69 2
            $result = $method($this);
70 1
            $this->commit();
71 1
            return $result;
72 1
        } catch (Exception $e) {
73 1
            $this->rollback();
74 1
            throw $e;
75
        }
76
    }
77
78
    /**
79
     * Commits all queries executed on the driver since this
80
     * transaction started.
81
     *
82
     * @return void
83
     */
84 1
    public function commit(): void
85
    {
86 1
        $this->driver->getPdo()->commit();
87 1
        $this->isFinalized = true;
88
    }
89
90
    /**
91
     * Rollbacks all queries executed on this driver since
92
     * this transaction started.
93
     *
94
     * @return void
95
     */
96 2
    public function rollback(): void
97
    {
98 2
        $this->driver->getPdo()->rollBack();
99 2
        $this->isFinalized = true;
100
    }
101
}
102