GenericBuilder::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace PEIP\Base;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/*
14
 * GenericBuilder
15
 * Class to act as a factory for an abritrary class.
16
 *
17
 * @author Timo Michna <timomichna/yahoo.de>
18
 * @package PEIP
19
 * @subpackage base
20
 * @implements \PEIP\INF\Base\SingletonMap
21
 */
22
23
24
use PEIP\Util\ReflectionPool;
25
use PEIP\Util\Test;
26
27
class GenericBuilder implements
28
        \PEIP\INF\Base\SingletonMap
29
{
30
    protected $className;
31
32
    protected static $instances = [];
33
34
    /**
35
     * constructor.
36
     *
37
     * @param string           $className       class-name to create objects for
38
     * @param \ReflectionClass $reflectionClass reflection-class for class. default: NULL
39
     * @param bool             $storeRef        wether to store a reference for new instance. default: true
40
     *
41
     * @return
42
     */
43
    public function __construct($className, \ReflectionClass $reflectionClass = null, $storeRef = true)
44
    {
45
        if ($reflectionClass) {
46
            if ($reflectionClass->getName() != $className) {
47
                throw new \Exception(
48
                    'Constructing GenericBuilder with wrong ReflectionClass'
49
                );
50
            }
51
52
53
            $this->reflectionClass = $reflectionClass;
0 ignored issues
show
Bug introduced by
The property reflectionClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        }
55
        $this->className = $className;
56
        if ($storeRef) {
57
            self::$instances[$this->className] = $this;
58
        }
59
    }
60
61
    /**
62
     * Creates (if not exists) and returns GenericBuilder instance for class.
63
     *
64
     * @param string $className class-name to return builder instance for
65
     *
66
     * @return GenericBuilder builder instance for class
67
     */
68
    public static function getInstance($className)
69
    {
70
        if (!array_key_exists((string) $className, self::$instances)) {
71
            new self($className);
72
        }
73
74
        return self::$instances[$className];
75
    }
76
77
    /**
78
     * Creates object instance with given arguments.
79
     *
80
     * @param array $arguments array of constructore arguments
81
     *
82
     * @return object the created object instance
83
     */
84
    public function build(array $arguments = [])
85
    {
86
        if (Test::assertClassHasConstructor($this->className)) {
87
            if (!Test::assertRequiredConstructorParameters($this->className, $arguments)) {
88
                throw new \Exception('Missing Argument '.(count($arguments) + 1).' for '.$this->className.'::__construct');
89
            }
90
91
            return $this->getReflectionClass()->newInstanceArgs($arguments);
92
        }
93
94
        return $this->getReflectionClass()->newInstance();
95
    }
96
97
    /**
98
     * returns reflection class instance.
99
     *
100
     * @return ReflectionClass
101
     */
102
    public function getReflectionClass()
103
    {
104
        return ReflectionPool::getInstance($this->className);
105
    }
106
}
107