Completed
Push — master ( e26eba...178ca9 )
by Eric
37:26 queued 30:42
created

TranslatableResourceSubscriber::postLoad()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
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
     * @param ContainerInterface $container
37
     */
38 5
    public function __construct(ContainerInterface $container)
39
    {
40 5
        $this->container = $container;
41 5
    }
42
43
    /**
44
     * @param LifecycleEventArgs $event
45
     */
46 2
    public function postLoad(LifecycleEventArgs $event)
47
    {
48 2
        $entity = $event->getEntity();
49
50 2
        if (!$entity instanceof TranslatableInterface) {
51 1
            return;
52
        }
53
54 1
        $localeContext = $this->getLocaleContext();
55 1
        $resource = $this->getResource(ClassUtils::getClass($entity));
56 1
        $translationFactory = $this->getTranslationFactory($resource->getRelation('translation')->getName());
57
58 1
        $entity->setLocales($localeContext->getLocales());
59 1
        $entity->setFallbackLocale($localeContext->getFallbackLocale());
60 1
        $entity->setTranslationFactory($translationFactory);
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function getSubscribedEvents()
67
    {
68 1
        return [Events::postLoad];
69
    }
70
71
    /**
72
     * @param string $class
73
     *
74
     * @return ResourceInterface|null
75
     */
76 1
    private function getResource($class)
77
    {
78 1
        foreach ($this->getResourceRegistry() as $resource) {
79 1
            if ($resource->getModel() === $class) {
80 1
                return $resource;
81
            }
82
        }
83
    }
84
85
    /**
86
     * @param string $resource
87
     *
88
     * @return FactoryInterface
89
     */
90 1
    private function getTranslationFactory($resource)
91
    {
92 1
        $factoryRegistry = $this->container->get('lug.resource.registry.factory');
93
94 1
        return $factoryRegistry[$resource];
95
    }
96
97
    /**
98
     * @return LocaleContextInterface
99
     */
100 1
    private function getLocaleContext()
101
    {
102 1
        return $this->container->get('lug.translation.context.locale');
103
    }
104
105
    /**
106
     * @return RegistryInterface
107
     */
108 1
    private function getResourceRegistry()
109
    {
110 1
        return $this->container->get('lug.resource.registry');
111
    }
112
}
113