Completed
Push — feature/transaction-helper ( bc7588 )
by Daan van
05:54
created

TransactionHelper::rollBack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * Copyright 2017 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\MiddlewareBundle\Doctrine\DBAL;
20
21
use Doctrine\DBAL\Connection;
22
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException;
23
24
class TransactionHelper
25
{
26
    /**
27
     * @var Connection[]
28
     */
29
    private $connections;
30
31
    /**
32
     * @param Connection[] $connections
33
     */
34
    public function __construct(array $connections)
35
    {
36
        foreach ($connections as $connection) {
37
            if (!$connection instanceof Connection) {
38
                throw InvalidArgumentException::invalidType('\Doctrine\DBAL\Connection', 'connection', $connection);
39
            }
40
        }
41
42
        $this->connections = $connections;
43
    }
44
45
    /**
46
     * Start transaction on each connection
47
     */
48
    public function beginTransaction()
49
    {
50
        foreach ($this->connections as $connection) {
51
            $connection->beginTransaction();
52
        }
53
    }
54
55
    /**
56
     * Commit transaction on each connection
57
     */
58
    public function commit()
59
    {
60
        foreach ($this->connections as $connection) {
61
            $connection->commit();
62
        }
63
    }
64
65
    /**
66
     * Roll back the transaction on each connection
67
     */
68
    public function rollBack()
69
    {
70
        foreach ($this->connections as $connection) {
71
            $connection->rollBack();
72
        }
73
    }
74
}
75