Completed
Push — master ( 625a74...74c3c7 )
by Eric
07:53
created

LocaleDomainSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Locale\EventSubscriber;
13
14
use Lug\Component\Locale\Provider\LocaleProviderInterface;
15
use Lug\Component\Resource\Domain\DomainEvent;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class LocaleDomainSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * @var LocaleProviderInterface
27
     */
28
    private $localeProvider;
29
30
    /**
31
     * @var TranslatorInterface
32
     */
33
    private $translator;
34
35
    /**
36
     * @var PropertyAccessorInterface
37
     */
38
    private $propertyAccessor;
39
40
    /**
41
     * @param LocaleProviderInterface   $localeProvider
42
     * @param TranslatorInterface       $translator
43
     * @param PropertyAccessorInterface $propertyAccessor
44
     */
45 4
    public function __construct(
46
        LocaleProviderInterface $localeProvider,
47
        TranslatorInterface $translator,
48
        PropertyAccessorInterface $propertyAccessor
49
    ) {
50 4
        $this->localeProvider = $localeProvider;
51 4
        $this->translator = $translator;
52 4
        $this->propertyAccessor = $propertyAccessor;
53 4
    }
54
55
    /**
56
     * @param DomainEvent $event
57
     */
58 2
    public function validateDefaultLocale(DomainEvent $event)
59
    {
60 2
        $object = $event->getObject();
61
62 2
        if ($object !== $this->localeProvider->getDefaultLocale()) {
63 1
            return;
64
        }
65
66 1
        $resource = $event->getResource();
67
68 1
        $event->setStopped(true);
69 1
        $event->setStatusCode(409);
70 1
        $event->setMessageType('error');
71
72 1
        $event->setMessage($this->translator->trans(
73 1
            'lug.'.$resource->getName().'.'.$event->getAction().'.default',
74 1
            ['%'.$resource->getName().'%' => $this->propertyAccessor->getValue(
75 1
                $object,
76 1
                $resource->getLabelPropertyPath()
77 1
            )],
78
            'flashes'
79 1
        ));
80 1
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public static function getSubscribedEvents()
86
    {
87 1
        return ['lug.locale.pre_delete' => 'validateDefaultLocale'];
88
    }
89
}
90