Completed
Pull Request — master (#108)
by Julián
26:17 queued 06:59
created

DoctrineMappingsCompilerPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 48.39 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 30
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 2
A getORMCompilerPass() 15 15 3
A getODMCompilerPass() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
6
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * Class DoctrineMappingsCompilerPass.
12
 *
13
 * We can't add DoctrineOrmMappingsPass directly, because in GesdinetJWTRefreshTokenBundle->build we don't have current
14
 * bundle configuration yet.
15
 * This CompilerPass is effectively just a wrapper for DoctrineOrmMappingsPass, which passes mappings conditionally.
16
 */
17
final class DoctrineMappingsCompilerPass implements CompilerPassInterface
18
{
19
    /**
20
     * Process Doctrine mappings based on gesdinet_jwt_refresh_token.refresh_token_entity config parameter.
21
     * If this parameter contains user-defined entity, RefreshToken will be registered as a mapped superclass, not as an
22
     * entity, to prevent Doctrine creating table for it and avoid conflicts with user-defined entity.
23
     *
24
     * @param ContainerBuilder $container
25
     */
26
    public function process(ContainerBuilder $container)
27
    {
28
        $config = $container->getExtensionConfig('gesdinet_jwt_refresh_token')[0];
29
30
        $mappingPass = 'mongodb' === strtolower($config['manager_type'])
31
            ? $this->getODMCompilerPass($config)
32
            : $this->getORMCompilerPass($config);
33
34
        $mappingPass->process($container);
35
    }
36
37
    /**
38
     * @param array $config
39
     *
40
     * @return CompilerPassInterface
41
     */
42 View Code Duplication
    protected function getORMCompilerPass(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Entity';
45
        $mappings = array(
46
            realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-orm') => $nameSpace,
47
        );
48
49
        if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) {
50
            $mappings[realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-superclass')] = $nameSpace;
51
        } else {
52
            $mappings[realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-entity')] = $nameSpace;
53
        }
54
55
        return DoctrineOrmMappingsPass::createYamlMappingDriver($mappings);
56
    }
57
58
    /**
59
     * @param array $config
60
     *
61
     * @return CompilerPassInterface
62
     */
63 View Code Duplication
    protected function getODMCompilerPass(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Document';
66
        $mappings = array(
67
            realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-mongodb') => $nameSpace,
68
        );
69
70
        if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) {
71
            $mappings[realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-superclass')] = $nameSpace;
72
        } else {
73
            $mappings[realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-document')] = $nameSpace;
74
        }
75
76
        return DoctrineMongoDBMappingsPass::createYamlMappingDriver($mappings, array());
77
    }
78
}
79