Completed
Push — master ( 59db0f...abafe8 )
by Kamil
18:55
created

DoctrineORMDriver::addRepository()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Doctrine;
13
14
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
15
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\Form\Builder\DefaultFormBuilder;
16
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
17
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
18
use Sylius\Component\Resource\Metadata\MetadataInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Paweł Jędrzejewski <[email protected]>
25
 * @author Arnaud Langlade <[email protected]>
26
 * @author Gonzalo Vilaseca <[email protected]>
27
 */
28
class DoctrineORMDriver extends AbstractDoctrineDriver
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getType()
34
    {
35
        return SyliusResourceBundle::DRIVER_DOCTRINE_ORM;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata)
42
    {
43
        $repositoryClassParameterName = sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName());
44
        $repositoryClass = EntityRepository::class;
45
46
        if ($container->hasParameter($repositoryClassParameterName)) {
47
            $repositoryClass = $container->getParameter($repositoryClassParameterName);
48
        }
49
50
        if ($metadata->hasClass('repository')) {
51
            $repositoryClass = $metadata->getClass('repository');
52
        }
53
54
        $repositoryReflection = new \ReflectionClass($repositoryClass);
55
56
        $definition = new Definition($repositoryClass);
57
        $definition->setArguments([
58
            new Reference($metadata->getServiceId('manager')),
59
            $this->getClassMetadataDefinition($metadata),
60
        ]);
61
        $definition->setLazy(!$repositoryReflection->isFinal());
62
63
        $container->setDefinition($metadata->getServiceId('repository'), $definition);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function addDefaultForm(ContainerBuilder $container, MetadataInterface $metadata)
70
    {
71
        $defaultFormBuilderDefinition = new Definition(DefaultFormBuilder::class);
72
        $defaultFormBuilderDefinition->setArguments([new Reference($metadata->getServiceId('manager'))]);
73
74
        $definition = new Definition(DefaultResourceType::class);
75
        $definition
76
            ->setArguments([
77
                $this->getMetdataDefinition($metadata),
78
                $defaultFormBuilderDefinition,
79
            ])
80
            ->addTag('form.type', ['alias' => sprintf('%s_%s', $metadata->getApplicationName(), $metadata->getName())])
81
        ;
82
83
        $container->setDefinition(sprintf('%s.form.type.%s', $metadata->getApplicationName(), $metadata->getName()), $definition);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function getManagerServiceId(MetadataInterface $metadata)
90
    {
91
        return sprintf('doctrine.orm.%s_entity_manager', $this->getObjectManagerName($metadata));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function getClassMetadataClassname()
98
    {
99
        return 'Doctrine\\ORM\\Mapping\\ClassMetadata';
100
    }
101
}
102