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

CriteriaBuilder::addToCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
crap 1
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
 * @package KGzocha\Bundle\SearcherBundle\DependencyInjection
13
 */
14 View Code Duplication
class CriteriaBuilder 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...
15
{
16
    /**
17
     * @param $contextId
18
     * @param array $contextConfig
19
     * @param ContainerBuilder $container
20
     *
21
     * @return Definition
22
     */
23 4
    public static function defineServices(
24
        $contextId,
25
        array &$contextConfig,
26
        ContainerBuilder $container
27
    ) {
28 4
        foreach ($contextConfig['builders'] as &$model) {
29 4
            self::defineModel($contextId, $model, $container);
30
        }
31 3
    }
32
33
    /**
34
     * @param $contextId
35
     * @param array $model
36
     * @param ContainerBuilder $container
37
     *
38
     * @return Definition
39
     */
40 4
    private static function defineModel(
41
        $contextId,
42
        array &$model,
43
        ContainerBuilder $container
44
    ) {
45 4
        if (!isset($model['name'])) {
46 1
            throw new InvalidDefinitionException(sprintf(
47
                'At least one builder is missing name parameter'.
48 1
                ' in searching context "%s"',
49
                $contextId
50
            ));
51
        }
52
53 3
        $definitionName = sprintf(
54 3
            'k_gzocha_searcher.%s.builder.%s',
55
            $contextId,
56 3
            $model['name']
57
        );
58
59 3
        self::checkParameters($contextId, $model);
60
61
        // Build from service
62 3
        if (isset($model['service'])) {
63 1
            self::checkServiceExsists($container, $contextId, $model);
64 1
            $definition = $container->setDefinition(
65
                $definitionName,
66 1
                $container->getDefinition($model['service'])
67
            );
68 1
            self::addToCollection($container, $contextId, $definitionName);
69
70 1
            return $definition;
71
        }
72
73
        // Build from class
74 2
        $definition = $container->setDefinition(
75
            $definitionName,
76 2
            new Definition($model['class'])
77
        );
78 2
        self::addToCollection($container, $contextId, $definitionName);
79
80 2
        return $definition;
81
    }
82
83
    /**
84
     * @param string $contextId
85
     * @param array $model
86
     */
87 3
    private static function checkParameters(
88
        $contextId,
89
        array &$model
90
    ) {
91 3
        if (!isset($model['class'])
92 3
            && !isset($model['service'])) {
93
            throw new InvalidDefinitionException(sprintf(
94
                'You have to specify "class" or "service" for '.
95
                'all builders in searching context "%s"',
96
                $contextId
97
            ));
98
        }
99 3
    }
100
101
    /**
102
     * @param ContainerBuilder $container
103
     * @param string $contextId
104
     * @param array $model
105
     */
106 1
    private static function checkServiceExsists(
107
        ContainerBuilder $container,
108
        $contextId,
109
        array &$model
110
    ) {
111 1
        if (!$container->hasDefinition($model['service'])) {
112
            throw new InvalidDefinitionException(sprintf(
113
                'Service "%s" configured for builder in'.
114
                'searching context "%s" does not exist',
115
                $model['service'],
116
                $contextId
117
            ));
118
        }
119 1
    }
120
121
    /**
122
     * @param ContainerBuilder $container
123
     * @param $contextId
124
     * @param $name
125
     */
126 3
    private static function addToCollection(
127
        ContainerBuilder $container,
128
        $contextId,
129
        $name
130
    ) {
131
        $container
132 3
            ->getDefinition(sprintf(
133 3
                'k_gzocha_searcher.%s.builder_collection',
134
                $contextId
135
            ))
136 3
            ->addMethodCall(
137 3
                'addCriteriaBuilder',
138 3
                [new Reference($name)]
139
            );
140 3
    }
141
}
142