Completed
Push — master ( d8eed1...b5183c )
by Peter
05:44
created

SwitchListenerTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 22.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 9
loc 40
ccs 12
cts 15
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A changeEventNameResolver() 0 4 1
A getEventNameResolver() 0 8 2
A handle() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Domain\Event\Listener;
11
12
use GpsLab\Domain\Event\EventInterface;
13
use GpsLab\Domain\Event\NameResolver\EventClassLastPartResolver;
14
use GpsLab\Domain\Event\NameResolver\EventNameResolverInterface;
15
16
trait SwitchListenerTrait
17
{
18
    /**
19
     * @var EventNameResolverInterface
20
     */
21
    private $resolver;
22
23
    /**
24
     * @param EventNameResolverInterface $resolver
25
     */
26
    protected function changeEventNameResolver(EventNameResolverInterface $resolver)
27
    {
28
        $this->resolver = $resolver;
29
    }
30
31
    /**
32
     * @return EventNameResolverInterface
33
     */
34 1
    private function getEventNameResolver()
35
    {
36 1
        if (!($this->resolver instanceof EventNameResolverInterface)) {
37 1
            $this->resolver = new EventClassLastPartResolver(); // default name resolver
38 1
        }
39
40 1
        return $this->resolver;
41
    }
42
43
    /**
44
     * @param EventInterface $event
45
     */
46 1 View Code Duplication
    public function handle(EventInterface $event)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 1
        $event_name = $this->getEventNameResolver()->getEventName($event);
49 1
        $method = 'handle' . $event_name;
50
51 1
        if (method_exists($this, $method)) {
52 1
            call_user_func([$this, $method], $event);
53 1
        }
54 1
    }
55
}
56