VersionManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 98
rs 10
c 2
b 0
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 2
A setVersionStore() 0 4 1
A versionOf() 0 4 1
A persistVersionOf() 0 4 1
A versionOfClass() 0 4 1
A persistVersionOfClass() 0 4 1
A getAggregateRootVersion() 0 4 1
A setAggregateRootVersion() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\EventSourcing\Versioning;
13
14
use Cubiche\Domain\EventSourcing\EventSourcedAggregateRootInterface;
15
16
/**
17
 * VersionManager class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class VersionManager
22
{
23
    /**
24
     * @var VersionManager
25
     */
26
    private static $instance = null;
27
28
    /**
29
     * @var VersionStoreInterface
30
     */
31
    protected $versionStore;
32
33
    /**
34
     * VersionManager constructor.
35
     *
36
     * @param VersionStoreInterface $versionStore
37
     */
38
    private function __construct(VersionStoreInterface $versionStore)
39
    {
40
        $this->versionStore = $versionStore;
41
    }
42
43
    /**
44
     * @return VersionManager
45
     */
46
    public static function create()
47
    {
48
        if (static::$instance === null) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
49
            static::$instance = new static(new InMemoryVersionStore());
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
50
        }
51
52
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
53
    }
54
55
    /**
56
     * @param VersionStoreInterface $versionStore
57
     */
58
    public static function setVersionStore(VersionStoreInterface $versionStore)
59
    {
60
        static::create()->versionStore = $versionStore;
61
    }
62
63
    /**
64
     * @param EventSourcedAggregateRootInterface $aggregate
65
     *
66
     * @return int
67
     */
68
    public static function versionOf(EventSourcedAggregateRootInterface $aggregate)
69
    {
70
        return static::versionOfClass(get_class($aggregate));
71
    }
72
73
    /**
74
     * @param EventSourcedAggregateRootInterface $aggregate
75
     */
76
    public static function persistVersionOf(EventSourcedAggregateRootInterface $aggregate)
77
    {
78
        static::persistVersionOfClass(get_class($aggregate), $aggregate->version());
79
    }
80
81
    /**
82
     * @param string $className
83
     *
84
     * @return int
85
     */
86
    public static function versionOfClass($className)
87
    {
88
        return static::create()->getAggregateRootVersion($className);
89
    }
90
91
    /**
92
     * @param string $className
93
     * @param int    $version
94
     */
95
    public static function persistVersionOfClass($className, $version)
96
    {
97
        static::create()->setAggregateRootVersion($className, $version);
98
    }
99
100
    /**
101
     * @param string $aggregateClassName
102
     *
103
     * @return int
104
     */
105
    protected function getAggregateRootVersion($aggregateClassName)
106
    {
107
        return $this->versionStore->load($aggregateClassName);
108
    }
109
110
    /**
111
     * @param string $aggregateClassName
112
     * @param int    $version
113
     */
114
    protected function setAggregateRootVersion($aggregateClassName, $version)
115
    {
116
        $this->versionStore->persist($aggregateClassName, $version);
117
    }
118
}
119