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

CriteriaCollection::defineServices()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 26
loc 26
ccs 11
cts 11
cp 1
rs 8.8571
cc 2
eloc 17
nc 2
nop 3
crap 2
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