LocaleSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A handleLocale() 0 13 2
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\LocaleBehavior;
15
use Sulu\Component\DocumentManager\DocumentRegistry;
16
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
17
use Sulu\Component\DocumentManager\Events;
18
use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
/**
22
 * Maps the locale.
23
 */
24
class LocaleSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * @var DocumentRegistry
28
     */
29
    private $registry;
30
31
    public function __construct(DocumentRegistry $registry)
32
    {
33
        $this->registry = $registry;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function getSubscribedEvents()
40
    {
41
        return [
42
            Events::HYDRATE => ['handleLocale', 250],
43
            Events::PERSIST => ['handleLocale', 250],
44
        ];
45
    }
46
47
    /**
48
     * @param AbstractMappingEvent $event
49
     *
50
     * @throws DocumentManagerException
51
     */
52
    public function handleLocale(AbstractMappingEvent $event)
53
    {
54
        $document = $event->getDocument();
55
56
        if (!$document instanceof LocaleBehavior) {
57
            return;
58
        }
59
60
        $event->getAccessor()->set(
61
            'locale',
62
            $this->registry->getLocaleForDocument($document)
63
        );
64
    }
65
}
66