Completed
Push — master ( b86c68...476207 )
by Krzysztof
12:15 queued 09:59
created

Criteria   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.8%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 124
loc 124
ccs 36
cts 41
cp 0.878
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A defineServices() 9 9 2
B defineModel() 40 40 3
A checkParameters() 11 11 3
A checkServiceExists() 14 14 2
A addToCollection() 15 15 1

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 KGzocha\Bundle\SearcherBundle\DependencyInjection\ServiceDefiner;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * @author Krzysztof Gzocha <[email protected]>
12
 */
13 View Code Duplication
class Criteria implements ServiceDefinerInterface
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @param $contextId
17
     * @param array $contextConfig
18
     * @param ContainerBuilder $container
19
     *
20
     * @return Definition
21
     */
22 4
    public static function defineServices(
23
        $contextId,
24
        array &$contextConfig,
25
        ContainerBuilder $container
26
    ) {
27 4
        foreach ($contextConfig['criteria'] as &$model) {
28 4
            self::defineModel($contextId, $model, $container);
29
        }
30 3
    }
31
32
    /**
33
     * @param $contextId
34
     * @param array $model
35
     * @param ContainerBuilder $container
36
     *
37
     * @return Definition
38
     */
39 4
    private static function defineModel(
40
        $contextId,
41
        array &$model,
42
        ContainerBuilder $container
43
    ) {
44 4
        if (!isset($model['name'])) {
45 1
            throw new InvalidDefinitionException(sprintf(
46
                'At least one model is missing name parameter'.
47 1
                ' in searching context "%s"',
48
                $contextId
49
            ));
50
        }
51
52 3
        $definitionName = sprintf(
53 3
            'k_gzocha_searcher.%s.criteria.%s',
54
            $contextId,
55 3
            $model['name']
56
        );
57
58 3
        self::checkParameters($contextId, $model);
59
60 3
        if (isset($model['service'])) {
61 1
            self::checkServiceExists($container, $contextId, $model);
62 1
            $definition = $container->setDefinition(
63
                $definitionName,
64 1
                $container->getDefinition($model['service'])
65
            );
66 1
            self::addToCollection($container, $contextId, $definitionName);
67
68 1
            return $definition;
69
        }
70
71 2
        $definition = $container->setDefinition(
72
            $definitionName,
73 2
            new Definition($model['class'])
74
        );
75 2
        self::addToCollection($container, $contextId, $definitionName);
76
77 2
        return $definition;
78
    }
79
80
    /**
81
     * @param string $contextId
82
     * @param array $model
83
     */
84 3
    private static function checkParameters($contextId, array &$model)
85
    {
86 3
        if (!isset($model['class'])
87 3
            && !isset($model['service'])) {
88
            throw new InvalidDefinitionException(sprintf(
89
                'You have to specify "class" or "service" for '.
90
                'all criteria in searching context "%s"',
91
                $contextId
92
            ));
93
        }
94 3
    }
95
96
    /**
97
     * @param ContainerBuilder $container
98
     * @param string$contextId
99
     * @param array $model
100
     */
101 1
    private static function checkServiceExists(
102
        ContainerBuilder $container,
103
        $contextId,
104
        array &$model
105
    ) {
106 1
        if (!$container->hasDefinition($model['service'])) {
107
            throw new InvalidDefinitionException(sprintf(
108
                'Service "%s" configured for criteria in'.
109
                'searching context "%s" does not exist',
110
                $model['service'],
111
                $contextId
112
            ));
113
        }
114 1
    }
115
116
    /**
117
     * @param ContainerBuilder $container
118
     * @param $contextId
119
     * @param $name
120
     */
121 3
    private static function addToCollection(
122
        ContainerBuilder $container,
123
        $contextId,
124
        $name
125
    ) {
126
        $container
127 3
            ->getDefinition(sprintf(
128 3
                'k_gzocha_searcher.%s.criteria_collection',
129
                $contextId
130
            ))
131 3
            ->addMethodCall(
132 3
                'addNamedCriteria',
133 3
                [$name, new Reference($name)]
134
            );
135 3
    }
136
}
137