Completed
Push — master ( 031900...586888 )
by Kamil
20:21
created

DoctrineORMDriver::addRepository()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
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
        $definition = new Definition($repositoryClass);
55
        $definition->setArguments([
56
            new Reference($metadata->getServiceId('manager')),
57
            $this->getClassMetadataDefinition($metadata),
58
        ]);
59
60
        $container->setDefinition($metadata->getServiceId('repository'), $definition);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function addDefaultForm(ContainerBuilder $container, MetadataInterface $metadata)
67
    {
68
        $defaultFormBuilderDefinition = new Definition(DefaultFormBuilder::class);
69
        $defaultFormBuilderDefinition->setArguments([new Reference($metadata->getServiceId('manager'))]);
70
71
        $definition = new Definition(DefaultResourceType::class);
72
        $definition
73
            ->setArguments([
74
                $this->getMetdataDefinition($metadata),
75
                $defaultFormBuilderDefinition,
76
            ])
77
            ->addTag('form.type', ['alias' => sprintf('%s_%s', $metadata->getApplicationName(), $metadata->getName())])
78
        ;
79
80
        $container->setDefinition(sprintf('%s.form.type.%s', $metadata->getApplicationName(), $metadata->getName()), $definition);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function getManagerServiceId(MetadataInterface $metadata)
87
    {
88
        return sprintf('doctrine.orm.%s_entity_manager', $this->getObjectManagerName($metadata));
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function getClassMetadataClassname()
95
    {
96
        return 'Doctrine\\ORM\\Mapping\\ClassMetadata';
97
    }
98
}
99