EventName::parseName()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace PhpDDD\Domain\Event\Utils;
4
5
use PhpDDD\Domain\Event\EventInterface;
6
7
/**
8
 * slugify an Event class name in order to display a nicer name than the FQCN.
9
 */
10
final class EventName
11
{
12
    /**
13
     * @var EventInterface
14
     */
15
    private $event;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @param EventInterface $event
24
     */
25
    public function __construct(EventInterface $event)
26
    {
27
        $this->event = $event;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function __toString()
34
    {
35
        if ($this->name === null) {
36
            $this->name = $this->parseName();
37
        }
38
39
        return $this->name;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    private function parseName()
46
    {
47
        $class = get_class($this->event);
48
49
        if ('Event' === substr($class, -5)) {
50
            $class = substr($class, 0, -5);
51
        }
52
53
        if (false === strpos($class, '\\')) {
54
            return $class;
55
        }
56
57
        $parts = explode('\\', $class);
58
59
        return end($parts);
60
    }
61
}
62