Completed
Push — symfony3-wololo ( ab614e )
by Kamil
65:18 queued 29:29
created

DoctrineODMDriver::addDefaultForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 1
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\ODM\MongoDB\TranslatableRepository;
15
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
16
use Sylius\Component\Resource\Metadata\MetadataInterface;
17
use Sylius\Component\Resource\Model\TranslatableInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Parameter;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Paweł Jędrzejewski <[email protected]>
25
 * @author Arnaud Langlade <[email protected]>
26
 */
27
final class DoctrineODMDriver extends AbstractDoctrineDriver
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getType()
33
    {
34
        return SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata)
41
    {
42
        $modelClass = $metadata->getClass('model');
43
44
        $repositoryClass = in_array(TranslatableInterface::class, class_implements($modelClass))
45
            ? TranslatableRepository::class
46
            : new Parameter('sylius.mongodb.odm.repository.class')
47
        ;
48
49
        if ($metadata->hasClass('repository')) {
50
            $repositoryClass = $metadata->getClass('repository');
51
        }
52
53
        $unitOfWorkDefinition = new Definition('Doctrine\\ODM\\MongoDB\\UnitOfWork');
54
        $unitOfWorkDefinition
55
            ->setFactory([new Reference($this->getManagerServiceId($metadata)), 'getUnitOfWork'])
56
            ->setPublic(false)
57
        ;
58
59
        $definition = new Definition($repositoryClass);
60
        $definition->setArguments([
61
            new Reference($metadata->getServiceId('manager')),
62
            $unitOfWorkDefinition,
63
            $this->getClassMetadataDefinition($metadata),
64
        ]);
65
66
        $container->setDefinition($metadata->getServiceId('repository'), $definition);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function getManagerServiceId(MetadataInterface $metadata)
73
    {
74
        if ($objectManagerName = $this->getObjectManagerName($metadata)) {
75
            return sprintf('doctrine_mongodb.odm.%s_document_manager', $objectManagerName);
76
        }
77
78
        return 'doctrine_mongodb.odm.document_manager';
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function getClassMetadataClassname()
85
    {
86
        return 'Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata';
87
    }
88
}
89