Completed
Pull Request — master (#7)
by Sergey
05:28 queued 03:19
created

CollectMappersCompilePass::configureBindings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 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
}