AbstractFilingSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
A handlePersist() 0 13 2
generatePath() 0 1 ?
supports() 0 1 ?
getParentName() 0 1 ?
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager\Subscriber\Behavior\Path;
13
14
use Sulu\Component\DocumentManager\Event\PersistEvent;
15
use Sulu\Component\DocumentManager\Events;
16
use Sulu\Component\DocumentManager\NodeManager;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19
/**
20
 * Automatically set the parent at a pre-determined location.
21
 */
22
abstract class AbstractFilingSubscriber implements EventSubscriberInterface
23
{
24
    /**
25
     * @var NodeManager
26
     */
27
    private $nodeManager;
28
29
    /**
30
     * @param NodeManager $nodeManager
31
     */
32
    public function __construct(
33
        NodeManager $nodeManager
34
    ) {
35
        $this->nodeManager = $nodeManager;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            Events::PERSIST => ['handlePersist', 490],
45
        ];
46
    }
47
48
    public function handlePersist(PersistEvent $event)
49
    {
50
        $document = $event->getDocument();
51
52
        if (!$this->supports($document)) {
53
            return;
54
        }
55
56
        $path = $this->generatePath($event);
57
58
        $parentNode = $this->nodeManager->createPath($path);
59
        $event->setParentNode($parentNode);
60
    }
61
62
    /**
63
     * Generates the path for the given event.
64
     *
65
     * @return string
66
     */
67
    abstract protected function generatePath(PersistEvent $event);
68
69
    /**
70
     * Return true if this subscriber should be applied to the document.
71
     *
72
     * @param object $document
73
     */
74
    abstract protected function supports($document);
75
76
    /**
77
     * Return the name of the parent document.
78
     *
79
     * @param $document
80
     *
81
     * @return string
82
     */
83
    abstract protected function getParentName($document);
84
}
85