Completed
Push — feature/institution-configurat... ( 98944a...bbfd28 )
by A.
08:21
created

DBALConnectionHelper::getConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
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
 */
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 View Code Duplication
class DBALConnectionHelper
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
{
27
    /**
28
     * @var Connection[]
29
     */
30
    private $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('\Doctrine\DBAL\Connection', 'connection', $connection);
40
            }
41
        }
42
43
        $this->connections = $connections;
44
    }
45
46
    /**
47
     * Start transaction on each connection
48
     */
49
    public function beginTransaction()
50
    {
51
        foreach ($this->connections as $connection) {
52
            $connection->beginTransaction();
53
        }
54
    }
55
56
    /**
57
     * Commit transaction on each connection
58
     */
59
    public function commit()
60
    {
61
        foreach ($this->connections as $connection) {
62
            $connection->commit();
63
        }
64
    }
65
66
    /**
67
     * Roll back the transaction on each connection
68
     */
69
    public function rollBack()
70
    {
71
        foreach ($this->connections as $connection) {
72
            $connection->rollBack();
73
        }
74
    }
75
76
    /**
77
     * @param string $connectionName
78
     * @return Connection
79
     */
80
    public function getConnection($connectionName)
81
    {
82
        if (!is_string($connectionName)) {
83
            throw InvalidArgumentException::invalidType('string', 'connectionName', $connectionName);
84
        }
85
86
        if (!array_key_exists($connectionName, $this->connections)) {
87
            throw new UnknownDBALConnectionException($connectionName);
88
        }
89
90
        return $this->connections[$connectionName];
91
    }
92
}
93