EventUpgrader   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 33.72 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 29
loc 86
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerUpgrade() 0 4 1
A migrate() 0 12 3
A upgrade() 14 15 6
A downgrade() 15 16 6

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
namespace DDDominio\EventSourcing\Versioning;
4
5
use DDDominio\EventSourcing\EventStore\StoredEvent;
6
7
class EventUpgrader implements EventUpgraderInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $upgrades;
13
14
    /**
15
     * @var EventAdapter
16
     */
17
    private $eventAdapter;
18
19
    /**
20
     * @param EventAdapter $eventAdapter
21
     */
22 80
    public function __construct(EventAdapter $eventAdapter)
23
    {
24 80
        $this->eventAdapter = $eventAdapter;
25 80
    }
26
27
    /**
28
     * @param UpgradeInterface $upgrade
29
     */
30 72
    public function registerUpgrade(UpgradeInterface $upgrade)
31
    {
32 72
        $this->upgrades[$upgrade->eventClass()][] = $upgrade;
33 72
    }
34
35
    /**
36
     * @param StoredEvent $storedEvent
37
     * @param Version $version
38
     */
39 41
    public function migrate(StoredEvent $storedEvent, $version = null)
40
    {
41 41
        if (is_null($version)) {
42 34
            $this->upgrade($storedEvent);
43
        } else {
44 10
            if ($storedEvent->version()->greaterThan($version)) {
45 4
                $this->downgrade($storedEvent, $version);
46
            } else {
47 6
                $this->upgrade($storedEvent, $version);
48
            }
49
        }
50 41
    }
51
52
    /**
53
     * @param StoredEvent $storedEvent
54
     * @param Version $version
55
     */
56 37 View Code Duplication
    public function upgrade(StoredEvent $storedEvent, $version = 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...
57
    {
58 37
        if (isset($this->upgrades[$storedEvent->type()])) {
59
            /** @var UpgradeInterface $upgrade */
60 12
            foreach ($this->upgrades[$storedEvent->type()] as $upgrade) {
61 12
                if ($storedEvent->version()->equalTo($upgrade->from())) {
62 11
                    $upgrade->upgrade($storedEvent);
63 11
                    $storedEvent->setVersion($upgrade->to());
64 11
                    if (!is_null($version) && $upgrade->to()->equalTo($version)) {
65 12
                        return;
66
                    }
67
                }
68
            }
69
        }
70 36
    }
71
72
    /**
73
     * @param StoredEvent $storedEvent
74
     * @param Version $version
75
     */
76 4 View Code Duplication
    public function downgrade(StoredEvent $storedEvent, $version = 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...
77
    {
78 4
        if (isset($this->upgrades[$storedEvent->type()])) {
79 3
            $upgrades = array_reverse($this->upgrades[$storedEvent->type()]);
80
            /** @var UpgradeInterface $upgrade */
81 3
            foreach ($upgrades as $upgrade) {
82 3
                if ($storedEvent->version()->equalTo($upgrade->to())) {
83 2
                    $upgrade->downgrade($storedEvent);
84 2
                    $storedEvent->setVersion($upgrade->from());
85 2
                    if (!is_null($version) && $upgrade->to()->equalTo($version)) {
86 3
                        return;
87
                    }
88
                }
89
            }
90
        }
91 4
    }
92
}
93