BasePathSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 46
loc 46
wmc 4
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A getSubscribedEvents() 6 6 1
A handlePersist() 11 11 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
namespace Sulu\Component\DocumentManager\Subscriber\Behavior\Path;
4
5
use Sulu\Component\DocumentManager\Behavior\Path\BasePathBehavior;
6
use Sulu\Component\DocumentManager\Event\PersistEvent;
7
use Sulu\Component\DocumentManager\Events;
8
use Sulu\Component\DocumentManager\NodeManager;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
/**
12
 * Sets the base path for the node.
13
 */
14 View Code Duplication
class BasePathSubscriber implements EventSubscriberInterface
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * @var NodeManager
18
     */
19
    private $nodeManager;
20
21
    /**
22
     * @var string
23
     */
24
    private $basePath;
25
26
    /**
27
     * @param NodeManager $nodeManager
28
     * @param string $basePath
29
     */
30
    public function __construct(
31
        NodeManager $nodeManager,
32
        $basePath
33
    ) {
34
        $this->nodeManager = $nodeManager;
35
        $this->basePath = $basePath;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            Events::PERSIST => ['handlePersist', 500],
45
        ];
46
    }
47
48
    public function handlePersist(PersistEvent $event)
49
    {
50
        $document = $event->getDocument();
51
52
        if (!$document instanceof BasePathBehavior) {
53
            return;
54
        }
55
56
        $parentNode = $this->nodeManager->createPath($this->basePath);
57
        $event->setParentNode($parentNode);
58
    }
59
}
60