DBALConnectionHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beginTransaction() 0 4 2
A __construct() 0 9 3
A commit() 0 4 2
A rollBack() 0 4 2
A getConnection() 0 11 3
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
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\ManagementBundle\Service;
20
21
use Doctrine\DBAL\Connection;
22
use Surfnet\StepupMiddleware\ManagementBundle\Exception\InvalidArgumentException;
23
use Surfnet\StepupMiddleware\ManagementBundle\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
        }
42
43
        $this->connections = $connections;
44
    }
45
46
    /**
47
     * Start transaction on each connection
48
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
49
    public function beginTransaction(): void
50
    {
51
        foreach ($this->connections as $connection) {
52
            $connection->beginTransaction();
53
        }
54
    }
55
56
    /**
57
     * Commit transaction on each connection
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59
    public function commit(): void
60
    {
61
        foreach ($this->connections as $connection) {
62
            $connection->commit();
63
        }
64
    }
65
66
    /**
67
     * Roll back the transaction on each connection
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function rollBack(): void
70
    {
71
        foreach ($this->connections as $connection) {
72
            $connection->rollBack();
73
        }
74
    }
75
76
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connectionName should have a doc-comment as per coding-style.
Loading history...
77
     * @return Connection
78
     */
79
    public function getConnection(string $connectionName): Connection
80
    {
81
        if (!is_string($connectionName)) {
0 ignored issues
show
introduced by
The condition is_string($connectionName) is always true.
Loading history...
82
            throw InvalidArgumentException::invalidType('string', 'connectionName', $connectionName);
83
        }
84
85
        if (!array_key_exists($connectionName, $this->connections)) {
86
            throw new UnknownDBALConnectionException($connectionName);
87
        }
88
89
        return $this->connections[$connectionName];
90
    }
91
}
92