EventUpgrader::downgrade()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 16

Duplication

Lines 15
Ratio 93.75 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 15
loc 16
ccs 10
cts 10
cp 1
rs 9.1111
c 0
b 0
f 0
cc 6
nc 5
nop 2
crap 6
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