Completed
Push — master ( 8e5e55...6fad72 )
by Iqbal
10:57
created

TransactionalTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 23.64 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 1
cbo 0
dl 26
loc 110
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransactional() 0 4 1
A getTransactionalLevel() 0 4 1
A beginTransaction() 0 8 2
A rollback() 12 12 3
A commit() 14 14 4
A watch() 0 10 3
performCommit() 0 1 ?
performRollback() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the Borobudur-Event-Sourcing package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\EventSourcing\Storage;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/22/16
16
 */
17
trait TransactionalTrait
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $transactional = false;
23
24
    /**
25
     * @var int
26
     */
27
    private $transactionalLevel = 0;
28
29
    /**
30
     * @var array
31
     */
32
    private $watched = array();
33
34
    /**
35
     * @return boolean
36
     */
37
    public function isTransactional()
38
    {
39
        return $this->transactional;
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getTransactionalLevel()
46
    {
47
        return $this->transactionalLevel;
48
    }
49
50
    /**
51
     * Begin transaction
52
     */
53
    public function beginTransaction()
54
    {
55
        if (0 === $this->transactionalLevel) {
56
            $this->transactional = true;
57
        }
58
59
        $this->transactionalLevel += 1;
60
    }
61
62
    /**
63
     * Rollback transaction
64
     */
65 View Code Duplication
    public function rollback()
0 ignored issues
show
Duplication introduced by
This method 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...
66
    {
67
        if (true === $this->transactional) {
68
            foreach ($this->watched as $table => $records) {
69
                $this->performRollback($table, $records);
70
            }
71
72
            $this->transactionalLevel = 0;
73
            $this->transactional = false;
74
            $this->watched = array();
75
        }
76
    }
77
78
    /**
79
     * Commit transaction.
80
     */
81 View Code Duplication
    public function commit()
0 ignored issues
show
Duplication introduced by
This method 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...
82
    {
83
        if (true === $this->transactional) {
84
            $this->transactionalLevel -= 1;
85
            if (0 === $this->transactionalLevel) {
86
                foreach ($this->watched as $table => $records) {
87
                    $this->performCommit($table, $records);
88
                }
89
90
                $this->transactional = false;
91
                $this->watched = array();
92
            }
93
        }
94
    }
95
96
    /**
97
     * @param string $table
98
     * @param array  $record
99
     */
100
    protected function watch($table, array &$record)
101
    {
102
        if (true === $this->transactional) {
103
            if (!isset($this->watched[$table])) {
104
                $this->watched[$table] = array();
105
            }
106
107
            $this->watched[$table][] = &$record;
108
        }
109
    }
110
111
    /**
112
     * Commit action.
113
     *
114
     * @param string $table
115
     * @param array  $records
116
     */
117
    abstract protected function performCommit($table, array $records);
118
119
    /**
120
     * Rollback action.
121
     *
122
     * @param string $table
123
     * @param array  $records
124
     */
125
    abstract protected function performRollback($table, array $records);
126
}
127