Completed
Push — master ( 461219...630231 )
by Harry
04:03
created

Builder::build()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.5125
cc 6
eloc 13
nc 6
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Helper\Builder;
15
16
use Graze\DataFile\Helper\OptionalLoggerTrait;
17
use InvalidArgumentException;
18
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LogLevel;
20
use ReflectionClass;
21
22
class Builder implements BuilderInterface, LoggerAwareInterface
23
{
24
    use OptionalLoggerTrait;
25
26
    /**
27
     * @param string|object $class
28
     * @param mixed         ...$arguments
29
     *
30
     * @return mixed
31
     */
32 59
    public function build($class, ...$arguments)
33
    {
34 59
        if (is_object($class)) {
35 1
            return $class;
36
        }
37
38 58
        if (!class_exists($class)) {
39 1
            throw new InvalidArgumentException("Unable to build class: $class it does not exist");
40
        }
41
42 57
        $reflection = new ReflectionClass($class);
43 57
        $this->log(LogLevel::DEBUG, "Building class: {class}", ['class' => $class]);
44 57
        $object = $reflection->newInstanceArgs($arguments);
45
46 57
        if ($object instanceof BuilderAwareInterface) {
47 1
            $object->setBuilder($this);
48
        }
49
50 57
        if ($this->logger && ($object instanceof LoggerAwareInterface)) {
51 1
            $object->setLogger($this->logger);
52
        }
53
54 57
        return $object;
55
    }
56
}
57