Completed
Push — master ( 8a4be1...fe7d42 )
by Constantin
06:22
created

ReadModelRecreator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 79
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A recreateRead() 0 12 1
A getListenerEventClasses() 0 16 3
A tryToExtractEventClassFromMethod() 0 12 3
A tryToExtractEventClassFromParameter() 0 8 3
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\ReadModel;
7
8
9
use Gica\Cqrs\Event\EventsApplier\EventsApplierOnListener;
10
use Gica\Cqrs\EventStore;
11
use Psr\Log\LoggerInterface;
12
13
class ReadModelRecreator
14
{
15
16
    /**
17
     * @var EventStore
18
     */
19
    private $eventStore;
20
    /**
21
     * @var EventsApplierOnListener
22
     */
23
    private $eventsApplierOnListener;
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
29 1
    public function __construct(
30
        EventStore $eventStore,
31
        EventsApplierOnListener $eventsApplierOnListener,
32
        LoggerInterface $logger
33
    )
34
    {
35 1
        $this->eventStore = $eventStore;
36 1
        $this->eventsApplierOnListener = $eventsApplierOnListener;
37 1
        $this->logger = $logger;
38 1
    }
39
40 1
    public function recreateRead(ReadModelInterface $readModel)
41
    {
42 1
        $eventClasses = $this->getListenerEventClasses(get_class($readModel));
43
44 1
        $this->logger->info(print_r($eventClasses, true));
45 1
        $this->logger->info("loading events...");
46 1
        $allEvents = $this->eventStore->loadEventsByClassNames($eventClasses);
47
48 1
        $this->logger->info("applying events...");
49
50 1
        $this->eventsApplierOnListener->applyEventsOnListener($readModel, $allEvents);
51 1
    }
52
53 1
    private function getListenerEventClasses(string $readModelClass)
54
    {
55 1
        $result = [];
56
57 1
        $classReflection = new \ReflectionClass($readModelClass);
58
59 1
        foreach ($classReflection->getMethods() as $reflectionMethod) {
60 1
            $eventClass = $this->tryToExtractEventClassFromMethod($reflectionMethod);
61
62 1
            if (false !== $eventClass) {
63 1
                $result[] = $eventClass;
64
            }
65
        }
66
67 1
        return $result;
68
    }
69
70 1
    private function tryToExtractEventClassFromMethod(\ReflectionMethod $reflectionMethod)
71
    {
72 1
        foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
73 1
            $eventClass = $this->tryToExtractEventClassFromParameter($reflectionParameter);
74
75 1
            if (false !== $eventClass) {
76 1
                return $eventClass;
77
            }
78
        }
79
80 1
        return false;
81
    }
82
83 1
    private function tryToExtractEventClassFromParameter(\ReflectionParameter $reflectionParameter)
84
    {
85 1
        if ($reflectionParameter->getClass() && is_subclass_of($reflectionParameter->getClass()->getName(), \Gica\Cqrs\Event::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Gica\Cqrs\Event::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
Bug introduced by
Consider using $reflectionParameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
86 1
            return $reflectionParameter->getClass()->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflectionParameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
87
        }
88
89 1
        return false;
90
    }
91
}