LockingRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 63.29 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 62.16%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 11
c 5
b 1
f 1
lcom 1
cbo 4
dl 50
loc 79
ccs 23
cts 37
cp 0.6216
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 12 12 2
A load() 12 12 2
A doDelete() 13 13 3
A doSave() 13 13 3
doSaveWithLock() 0 1 ?
doDeleteWithLock() 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
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Governor\Framework\Repository;
10
11
use Governor\Framework\Domain\AggregateRootInterface;
12
use Governor\Framework\EventHandling\EventBusInterface;
13
14
/**
15
 * Description of LockingRepository
16
 *
17
 * @author    "David Kalosi" <[email protected]>  
18
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> 
19
 */
20
abstract class LockingRepository extends AbstractRepository
21
{
22
23
    private $lockManager;
24
25 23
    public function __construct($className, EventBusInterface $eventBus, LockManagerInterface $lockManager)
26
    {
27 23
        parent::__construct($className, $eventBus);
28 23
        $this->lockManager = $lockManager;
29 23
    }
30
31 3 View Code Duplication
    public function add(AggregateRootInterface $object)
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...
32
    {
33 3
        $aggregateId = $object->getIdentifier();
34 3
        $this->lockManager->obtainLock($aggregateId);
35
36
        try {
37 3
            parent::add($object);
38 3
        } catch (\Exception $ex) {
39
            $this->lockManager->releaseLock($aggregateId);
40
            throw $ex;
41
        }
42 3
    }
43
44 14 View Code Duplication
    public function load($id, $expectedVersion = null)
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...
45
    {
46 14
        $this->lockManager->obtainLock($id);
47
        try {
48 14
            $object = parent::load($id, $expectedVersion);
49
50 10
            return $object;
51 4
        } catch (\Exception $ex) {
52 4
            $this->lockManager->releaseLock($id);
53 4
            throw $ex;
54
        }
55
    }
56
57 1 View Code Duplication
    protected function doDelete(AggregateRootInterface $object)
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...
58
    {
59
        if (null !== $object->getVersion() && !$this->lockManager->validateLock($object)) {
60
            throw new ConcurrencyException(sprintf(
61 1
                "The aggregate of type [%s] with identifier [%s] could not be " .
62
                "saved, as a valid lock is not held. Either another thread has saved an aggregate, or " .
63
                "the current thread had released its lock earlier on.",
64
                get_class($object), $object->getIdentifier()
65
            ));
66
        }
67
68
        $this->doDeleteWithLock($object);
69
    }
70
71 9 View Code Duplication
    protected function doSave(AggregateRootInterface $object)
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...
72
    {
73 9
        if (null !== $object->getVersion() && !$this->lockManager->validateLock($object)) {
74
            throw new ConcurrencyException(sprintf(
75
                "The aggregate of type [%s] with identifier [%s] could not be "
76
                . "saved, as a valid lock is not held. Either another thread has saved an aggregate, or "
77
                . "the current thread had released its lock earlier on.",
78
                get_class($object), $object->getIdentifier()
79
            ));
80
        }
81
82 9
        $this->doSaveWithLock($object);
83 9
    }
84
85
    /**
86
     * Perform the actual saving of the aggregate. All necessary locks have been verified.
87
     *
88
     * @param AggregateRootInterface $aggregate the aggregate to store
89
     */
90
    protected abstract function doSaveWithLock(AggregateRootInterface $aggregate);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
91
92
    /**
93
     * Perform the actual deleting of the aggregate. All necessary locks have been verifierd.
94
     *
95
     * @param AggregateRootInterface $aggregate the aggregate to delete
96
     */
97
    protected abstract function doDeleteWithLock(AggregateRootInterface $aggregate);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
98
}
99