DBALConnectionHelper::beginTransaction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Service;
20
21
use Doctrine\DBAL\Connection;
22
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException;
23
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\UnknownDBALConnectionException;
24
25
class DBALConnectionHelper
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DBALConnectionHelper
Loading history...
26
{
27
    /**
28
     * @var Connection[]
29
     */
30
    private array $connections;
31
32
    /**
33
     * @param Connection[] $connections
34
     */
35
    public function __construct(array $connections)
36
    {
37
        foreach ($connections as $connection) {
38
            if (!$connection instanceof Connection) {
39
                throw InvalidArgumentException::invalidType(Connection::class, 'connection', $connection);
40
            }
41
            if (!$connection->getDatabasePlatform()->supportsSavepoints()) {
42
                throw new InvalidArgumentException(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
43
                    "Connection  for database '%s' does not support nested savepoints",
44
                    $connection->getDatabase()
45
                ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
46
            }
47
            $connection->setNestTransactionsWithSavepoints(true);
48
        }
49
50
        $this->connections = $connections;
51
    }
52
53
    /**
54
     * Start transaction on each connection
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56
    public function beginTransaction(): void
57
    {
58
        foreach ($this->connections as $connection) {
59
            $connection->beginTransaction();
60
        }
61
    }
62
63
    /**
64
     * Commit transaction on each connection
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66
    public function commit(): void
67
    {
68
        foreach ($this->connections as $connection) {
69
            $connection->commit();
70
        }
71
    }
72
73
    /**
74
     * Roll back the transaction on each connection
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public function rollBack(): void
77
    {
78
        foreach ($this->connections as $connection) {
79
            $connection->rollBack();
80
        }
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
84
     * @return Connection
85
     */
86
    public function getConnection(string $connectionName): Connection
87
    {
88
        if (!array_key_exists($connectionName, $this->connections)) {
89
            throw new UnknownDBALConnectionException($connectionName);
90
        }
91
92
        return $this->connections[$connectionName];
93
    }
94
}
95