AbstractContainerAwareFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 26
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A injectContainer() 0 9 2
createDefinition() 0 1 ?
1
<?php
2
3
namespace Knp\RadBundle\DependencyInjection\Definition;
4
5
use Symfony\Component\DependencyInjection\Definition;
6
7
use Knp\RadBundle\Reflection\ReflectionFactory;
8
use Knp\RadBundle\DependencyInjection\ReferenceFactory;
9
10
abstract class AbstractContainerAwareFactory
11
{
12
    const CONTAINER_AWARE_INTERFACE = 'Symfony\Component\DependencyInjection\ContainerAwareInterface';
13
    const CONTAINER_SERVICE_ID = 'service_container';
14
15
    private $reflectionFactory;
16
    private $referenceFactory;
17
18
    public function __construct(ReflectionFactory $reflectionFactory = null, ReferenceFactory $referenceFactory = null)
19
    {
20
        $this->reflectionFactory = $reflectionFactory ?: new ReflectionFactory();
21
        $this->referenceFactory  = $referenceFactory ?: new ReferenceFactory();
22
    }
23
24
    protected function injectContainer(Definition $definition)
25
    {
26
        $reflClass = $this->reflectionFactory->createReflectionClass($definition->getClass());
27
28
        if ($reflClass->implementsInterface(static::CONTAINER_AWARE_INTERFACE)) {
29
            $containerRef = $this->referenceFactory->createReference(static::CONTAINER_SERVICE_ID);
30
            $definition->addMethodCall('setContainer', array($containerRef));
31
        }
32
    }
33
34
    abstract public function createDefinition($className);
35
}
36