LockingRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
crap 1
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