Completed
Pull Request — master (#7)
by Sergey
04:52
created

CollectMappersCompilePass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A configureBindings() 0 16 2
1
<?php
2
3
namespace Fesor\RequestObject\DependeyInjection;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
9
class CollectMappersCompilePass implements CompilerPassInterface
10
{
11
    public function process(ContainerBuilder $container)
12
    {
13
        $mappers = $container->findTaggedServiceIds(RequestObjectExtension::MAPPER_TAG);
14
15
        $bindings = [];
16
        foreach ($mappers as $mapperId) {
17
            $bindings = array_merge($bindings, $this->configureBindings($mapperId, $container->getDefinition($mapperId)));
18
        }
19
20
        $requestMapperDefinition = $container->getDefinition(RequestObjectExtension::REQUEST_MAPPER_ID);
21
        $requestMapperDefinition->addMethodCall('registerBindings', [$bindings]);
22
    }
23
24
    private function configureBindings(string $id, Definition $mapper)
25
    {
26
        $classReflection = new \ReflectionClass($mapper->getClass());
27
        $publicMethods = $classReflection->getMethods(\ReflectionMethod::IS_PUBLIC);
28
29
        $mapperMethods = array_filter($publicMethods, function (\ReflectionMethod $method) {
30
            return $method->hasReturnType();
31
        });
32
33
        $bindings = [];
34
        foreach ($mapperMethods as $method) {
35
            $bindings[(string) $method->getReturnType()] = [$id, $method->getName()];
36
        }
37
38
        return $bindings;
39
    }
40
}