CreateWithNonPublicConstructor::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
c 0
b 0
f 0
rs 10
ccs 9
cts 9
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * File was created 17.05.2016 06:09
4
 */
5
6
namespace PeekAndPoke\Component\Creator;
7
8
9
/**
10
 * Creator for instantiating classes with a private/protected constructor.
11
 *
12
 * The given class MUST have a constructor and the constructor MUST have zero required parameters.
13
 *
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class CreateWithNonPublicConstructor extends AbstractCreator
17
{
18
    /** @var string */
19
    private $ctorScopeFqcn;
20
21
    /**
22
     * @param \ReflectionClass $class
23
     */
24 2
    public function __construct(\ReflectionClass $class)
25
    {
26 2
        parent::__construct($class);
27
28 2
        $this->ctorScopeFqcn = $class->getConstructor()->getDeclaringClass()->getName();
29 2
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 1
    public function create($data = null)
35
    {
36 1
        $fqcn = $this->fqcn;
37
38
        // we call the private/protected constructor with the scope of the class that is defining it
39 1
        $closure = \Closure::bind(
40 1
            function () use ($fqcn) {
41 1
                return new $fqcn();
42 1
            },
43 1
            null,
44 1
            $this->ctorScopeFqcn
45
        );
46
47 1
        return $closure();
48
    }
49
}
50