PathSubscriber::handleHydrate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 7
nc 2
nop 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\Mapping;
13
14
use Sulu\Component\DocumentManager\Behavior\Mapping\PathBehavior;
15
use Sulu\Component\DocumentManager\DocumentInspector;
16
use Sulu\Component\DocumentManager\Event\HydrateEvent;
17
use Sulu\Component\DocumentManager\Events;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
/**
21
 * Set the path on the document.
22
 */
23
class PathSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * @var DocumentInspector
27
     */
28
    private $documentInspector;
29
30
    /**
31
     * @param DocumentInspector $documentInspector
32
     */
33
    public function __construct(DocumentInspector $documentInspector)
34
    {
35
        $this->documentInspector = $documentInspector;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            Events::HYDRATE => 'handleHydrate',
45
        ];
46
    }
47
48
    /**
49
     * @param HydrateEvent $event
50
     */
51
    public function handleHydrate(HydrateEvent $event)
52
    {
53
        $document = $event->getDocument();
54
55
        if (!$document instanceof PathBehavior) {
56
            return;
57
        }
58
59
        $event->getAccessor()->set(
60
            'path',
61
            $this->documentInspector->getPath($document)
62
        );
63
    }
64
}
65