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

CriteriaCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 3
dl 74
loc 74
ccs 20
cts 23
cp 0.8696
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B defineServices() 26 26 2
A checkCollectionParameters() 13 13 3
A checkServiceExists() 14 14 2

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
9
/**
10
 * @author Krzysztof Gzocha <[email protected]>
11
 */
12 View Code Duplication
class CriteriaCollection 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...
13
{
14
    /**
15
     * @param $contextId
16
     * @param array $contextConfig
17
     * @param ContainerBuilder $container
18
     *
19
     * @return Definition
20
     */
21 4
    public static function defineServices(
22
        $contextId,
23
        array &$contextConfig,
24
        ContainerBuilder $container
25
    ) {
26 4
        $collectionConfig = $contextConfig['criteria_collection'];
27 4
        self::checkCollectionParameters($contextId, $collectionConfig);
28
29 3
        if (isset($collectionConfig['service'])) {
30 1
            self::checkServiceExists(
31
                $container,
32
                $contextId,
33
                $collectionConfig
34
            );
35
36 1
            return $container->setDefinition(
37 1
                sprintf('k_gzocha_searcher.%s.criteria_collection', $contextId),
38 1
                $container->getDefinition($collectionConfig['service'])
39
            );
40
        }
41
42 2
        return $container->setDefinition(
43 2
            sprintf('k_gzocha_searcher.%s.criteria_collection', $contextId),
44 2
            new Definition($collectionConfig['class'])
45
        );
46
    }
47
48
    /**
49
     * @param string $contextId
50
     * @param array $collectionConfig
51
     */
52 4
    private static function checkCollectionParameters(
53
        $contextId,
54
        array &$collectionConfig
55
    ) {
56 4
        if (!isset($collectionConfig['class'])
57 4
            && !isset($collectionConfig['service'])) {
58 1
            throw new InvalidDefinitionException(sprintf(
59
                'You have to specify "class" or "service" for '.
60 1
                'criteria_collection in searching context "%s"',
61
                $contextId
62
            ));
63
        }
64 3
    }
65
66
    /**
67
     * @param ContainerBuilder $container
68
     * @param string $contextId
69
     * @param array $collectionConfig
70
     */
71 1
    private static function checkServiceExists(
72
        ContainerBuilder $container,
73
        $contextId,
74
        array &$collectionConfig
75
    ) {
76 1
        if (!$container->hasDefinition($collectionConfig['service'])) {
77
            throw new InvalidDefinitionException(sprintf(
78
                'Service "%s" configured for criteria_collection in'.
79
                'searching context "%s" does not exist',
80
                $collectionConfig['service'],
81
                $contextId
82
            ));
83
        }
84 1
    }
85
}
86