AliasFilingSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 65
wmc 6
lcom 1
cbo 5
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 6 1
A generatePath() 0 12 2
A supports() 0 4 1
A getParentName() 0 4 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 Doctrine\Common\Inflector\Inflector;
15
use Sulu\Component\DocumentManager\Behavior\Path\AliasFilingBehavior;
16
use Sulu\Component\DocumentManager\Event\PersistEvent;
17
use Sulu\Component\DocumentManager\Events;
18
use Sulu\Component\DocumentManager\MetadataFactoryInterface;
19
use Sulu\Component\DocumentManager\NodeManager;
20
21
/**
22
 * Automatically set the parent at a pre-determined location.
23
 */
24
class AliasFilingSubscriber extends AbstractFilingSubscriber
25
{
26
    /**
27
     * @var MetadataFactoryInterface
28
     */
29
    private $metadataFactory;
30
31
    /**
32
     * @param NodeManager $nodeManager
33
     * @param MetadataFactoryInterface $metadataFactory
34
     */
35
    public function __construct(
36
        NodeManager $nodeManager,
37
        MetadataFactoryInterface $metadataFactory
38
    ) {
39
        parent::__construct($nodeManager);
40
        $this->metadataFactory = $metadataFactory;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getSubscribedEvents()
47
    {
48
        return [
49
            Events::PERSIST => ['handlePersist', 490],
50
        ];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function generatePath(PersistEvent $event)
57
    {
58
        $document = $event->getDocument();
59
60
        $currentPath = '';
61
        if ($event->hasParentNode()) {
62
            $currentPath = $event->getParentNode()->getPath();
63
        }
64
        $parentName = $this->getParentName($document);
65
66
        return sprintf('%s/%s', $currentPath, Inflector::pluralize($parentName));
67
    }
68
69
    /**
70
     * @param object $document
71
     *
72
     * @return bool
73
     */
74
    protected function supports($document)
75
    {
76
        return $document instanceof AliasFilingBehavior;
77
    }
78
79
    /**
80
     * @param $document
81
     *
82
     * @return string
83
     */
84
    protected function getParentName($document)
85
    {
86
        return $this->metadataFactory->getMetadataForClass(get_class($document))->getAlias();
87
    }
88
}
89