FetchFromSharedInstancePoolOrCreateByFactoryBuilder   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 30 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-06-22 
5
 */
6
7
namespace Net\Bazzline\Component\Locator\MethodBodyBuilder;
8
9
use Net\Bazzline\Component\CodeGenerator\BlockGenerator;
10
11
/**
12
 * Class FetchFromSharedInstancePoolOrCreateByFactoryBuilder
13
 * @package Net\Bazzline\Component\Locator\MethodBodyBuilder
14
 */
15
class FetchFromSharedInstancePoolOrCreateByFactoryBuilder extends AbstractMethodBodyBuilder
16
{
17
    /**
18
     * @param BlockGenerator $body
19
     * @return BlockGenerator
20
     * @throws RuntimeException
21
     */
22
    public function build(BlockGenerator $body)
23
    {
24
        $this->assertMandatoryProperties();
25
26
        if ($this->instance->hasReturnValue()) {
27
            $returnValue = $this->instance->getReturnValue();
28
        } else {
29
            throw new RuntimeException(
30
                'return value in instance is mandatory'
31
            );
32
        }
33
34
        //@todo does it make sense to store the factory in the instance
35
        //  pool since we are using it only once?
36
        $body
37
            ->add('$className = \'' . $returnValue . '\';')
38
            ->add('')
39
            ->add('if ($this->isNotInSharedInstancePool($className)) {')
40
            ->startIndention()
41
            ->add('$factoryClassName = \'' . $this->instance->getClassName() . '\';')
42
            ->add('$factory = $this->fetchFromFactoryInstancePool($factoryClassName);')
43
            ->add('')
44
            ->add('$this->addToSharedInstancePool($className, $factory->create());')
45
            ->stopIndention()
46
            ->add('}')
47
            ->add('')
48
            ->add('return $this->fetchFromSharedInstancePool($className);');
49
50
        return $body;
51
    }
52
}