Completed
Pull Request — master (#39)
by
unknown
08:25 queued 12s
created

Repository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Rawkode\Eidetic\EventStore\DBALEventStore;
4
5
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity;
6
use Rawkode\Eidetic\EventStore\EventStore;
7
8
/**
9
 * Class Repository
10
 * @package Rawkode\Eidetic\EventStore\DBALEventStore
11
 */
12
final class Repository
13
{
14
    /** @var $entityClass */
15
    private $entityClass;
16
17
    /** @var EventStore $eventStore */
18
    private $eventStore;
19
20
    /**
21
     * @param EventSourcedEntity $class
22
     * @param EventStore $eventStore
23
     */
24
    private function __construct($class, EventStore $eventStore)
25
    {
26
        $this->entityClass = $class;
27
        $this->eventStore = $eventStore;
28
    }
29
30
    /**
31
     * @param $class
32
     * @param EventStore $eventStore
33
     * @return Repository
34
     */
35
    public static function createForType($class, EventStore $eventStore)
36
    {
37
        return new self($class, $eventStore);
38
    }
39
40
    /**
41
     * @param $key
42
     * @return mixed
43
     */
44
    public function load($key)
45
    {
46
        $class = $this->eventStore->getClassForKey($key);
47
48
        $this->enforceTypeConstraint($class);
49
50
        $events = $this->eventStore->retrieve($key);
51
52
        return $class::initialise($events);
53
    }
54
55
    /**
56
     * @param EventSourcedEntity $class
57
     * @throws IncorrectEntityException
58
     */
59
    public function save(EventSourcedEntity $class)
60
    {
61
        $this->enforceTypeConstraint($class);
0 ignored issues
show
Documentation introduced by
$class is of type object<Rawkode\Eidetic\E...ing\EventSourcedEntity>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        $this->eventStore->store($class->identifier(), $class->stagedEvents());
63
    }
64
65
    /**
66
     * @param string $class
67
     * @throws IncorrectEntityException
68
     */
69
    private function enforceTypeConstraint($class)
70
    {
71
        if (get_class($class) !== get_class($this->entityClass)) {
72
            throw new IncorrectEntityException(get_class($class) . " is not the same as " . get_class($this->entityClass));
73
        }
74
    }
75
}
76