Completed
Pull Request — master (#59)
by Eric
33:48
created

TranslatableResourceSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 85
ccs 23
cts 23
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A postLoad() 0 16 2
A getSubscribedEvents() 0 4 1
A getResource() 0 8 3
A getTranslationFactory() 0 6 1
A getLocaleContext() 0 4 1
A getResourceRegistry() 0 4 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\Bundle\TranslationBundle\EventSubscriber;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ORM\Event\LifecycleEventArgs;
16
use Doctrine\ORM\Events;
17
use Lug\Bundle\ResourceBundle\Util\ClassUtils;
18
use Lug\Component\Registry\Model\RegistryInterface;
19
use Lug\Component\Resource\Factory\FactoryInterface;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
use Lug\Component\Translation\Context\LocaleContextInterface;
22
use Lug\Component\Translation\Model\TranslatableInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class TranslatableResourceSubscriber implements EventSubscriber
29
{
30
    /**
31
     * @var ContainerInterface
32
     */
33
    private $container;
34
35
    /**
36 5
     * @param ContainerInterface $container
37
     */
38 5
    public function __construct(ContainerInterface $container)
39 5
    {
40
        $this->container = $container;
41
    }
42
43
    /**
44 2
     * @param LifecycleEventArgs $event
45
     */
46 2
    public function postLoad(LifecycleEventArgs $event)
47
    {
48 2
        $entity = $event->getEntity();
49 1
50
        if (!$entity instanceof TranslatableInterface) {
51
            return;
52 1
        }
53
54 1
        $localeContext = $this->getLocaleContext();
55 1
        $resource = $this->getResource(ClassUtils::getClass($entity));
56
        $translationFactory = $this->getTranslationFactory($resource->getRelation('translation')->getName());
57 1
58
        $entity->setLocales($localeContext->getLocales());
59 1
        $entity->setFallbackLocale($localeContext->getFallbackLocale());
60 1
        $entity->setTranslationFactory($translationFactory);
61 1
    }
62
63 1
    /**
64
     * {@inheritdoc}
65 1
     */
66 1
    public function getSubscribedEvents()
67
    {
68
        return [Events::postLoad];
69
    }
70
71 1
    /**
72
     * @param string $class
73 1
     *
74
     * @return ResourceInterface|null
75
     */
76
    private function getResource($class)
77
    {
78
        foreach ($this->getResourceRegistry() as $resource) {
79 1
            if ($resource->getModel() === $class) {
80
                return $resource;
81 1
            }
82
        }
83
    }
84
85
    /**
86
     * @param string $resource
87 1
     *
88
     * @return FactoryInterface
89 1
     */
90
    private function getTranslationFactory($resource)
91
    {
92
        $factoryRegistry = $this->container->get('lug.resource.registry.factory');
93
94
        return $factoryRegistry[$resource];
95
    }
96
97
    /**
98
     * @return LocaleContextInterface
99
     */
100
    private function getLocaleContext()
101
    {
102
        return $this->container->get('lug.translation.context.locale');
103
    }
104
105
    /**
106
     * @return RegistryInterface
107
     */
108
    private function getResourceRegistry()
109
    {
110
        return $this->container->get('lug.resource.registry');
111
    }
112
}
113