Completed
Push — master ( 41c904...ee72ab )
by GBProd
02:47
created

Dispatcher::getClassname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace GBProd\DomainEventBundle\Event;
4
5
use GBProd\DomainEvent\Dispatcher as DomainEventDispatcher;
6
use GBProd\DomainEvent\DomainEvent;
7
use GBProd\DomainEvent\EventProvider;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
/**
11
 * Domain dispatcher implementation for symfony dispatcher
12
 * 
13
 * @author gbprod <[email protected]>
14
 */
15
class Dispatcher implements DomainEventDispatcher
16
{
17
    /**
18
     * @var EventDispatcherInterface
19
     */
20
    private $dispatcher;
21
    
22
    /**
23
     * @param EventDispatcherInterface $dispatcher
24
     */
25 3
    public function __construct(EventDispatcherInterface $dispatcher)
26
    {
27 3
        $this->dispatcher = $dispatcher;
28 3
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function dispatch(EventProvider $provider)
34
    {
35 1
        foreach ($provider->popEvents() as $event) {
36 1
            $this->dispatcher->dispatch(
37 1
                $this->resolveEventName($provider, $event),
38 1
                new Event($event)
39 1
            );
40 1
        }
41 1
    }
42
    
43 1
    private function resolveEventName(EventProvider $provider, DomainEvent $event)
44
    {
45 1
        return sprintf(
46 1
            '%s.%s',
47 1
            $this->getClassname($provider),
48 1
            $this->getClassname($event)
49 1
        );
50
    }
51
    
52 1
    private function getClassname($object)
53
    {
54 1
        $name = get_class($object);
55 1
        $pos = strrpos($name, '\\');
56
57 1
        return $this->name = false === $pos ? $name : substr($name, $pos + 1);
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
    }
59
}