AbstractContainerAwareFactory::injectContainer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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