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

CriteriaBuilderCollection::checkServiceExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
ccs 3
cts 6
cp 0.5
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
crap 2.5
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 CriteriaBuilderCollection 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['builder_collection'];
27 4
        self::checkCollectionParameters($contextId, $collectionConfig);
28
29
        // Build from service
30 3
        if (isset($collectionConfig['service'])) {
31 1
            self::checkServiceExists(
32
                $container,
33
                $contextId,
34
                $collectionConfig
35
            );
36
37 1
            return $container->setDefinition(
38 1
                sprintf('k_gzocha_searcher.%s.builder_collection', $contextId),
39 1
                $container->getDefinition($collectionConfig['service'])
40
            );
41
        }
42
43
        // Build from class
44 2
        return $container->setDefinition(
45 2
            sprintf('k_gzocha_searcher.%s.builder_collection', $contextId),
46 2
            new Definition($collectionConfig['class'])
47
        );
48
    }
49
50
    /**
51
     * @param string $contextId
52
     * @param array $collectionConfig
53
     */
54 4
    private static function checkCollectionParameters(
55
        $contextId,
56
        array &$collectionConfig
57
    ) {
58 4
        if (!isset($collectionConfig['class'])
59 4
            && !isset($collectionConfig['service'])) {
60 1
            throw new InvalidDefinitionException(sprintf(
61
                'You have to specify "class" or "service" for '.
62 1
                'builder_collection in searching context "%s"',
63
                $contextId
64
            ));
65
        }
66 3
    }
67
68
    /**
69
     * @param ContainerBuilder $container
70
     * @param string $contextId
71
     * @param array $collectionConfig
72
     */
73 1
    private static function checkServiceExists(
74
        ContainerBuilder $container,
75
        $contextId,
76
        array &$collectionConfig
77
    ) {
78 1
        if (!$container->hasDefinition($collectionConfig['service'])) {
79
            throw new InvalidDefinitionException(sprintf(
80
                'Service "%s" configured for builder_collection in'.
81
                'searching context "%s" does not exist',
82
                $collectionConfig['service'],
83
                $contextId
84
            ));
85
        }
86 1
    }
87
}
88