Passed
Push — master ( 9534e6...9c83c0 )
by Rob
02:17
created

AbstractBase::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace devtoolboxuk\cerberus\Handlers;
4
5
use ReflectionClass;
6
7
use Exception;
8
9
abstract class AbstractBase
10
{
11
12
    private $wrappers = [];
13
    private $input;
14
    private $output;
15
16
    private $name;
17
//    private $prefixes;
18
    private $score;
19
20
21
    private $reference;
22
23
    public function __construct($value)
24
    {
25
        $this->setInput($value);
26
    }
27
28
    public function getInput()
29
    {
30
        return $this->input;
31
    }
32
    public function getReference()
33
    {
34
        return $this->reference;
35
    }
36
37
    public function setReference($reference = null)
38
    {
39
        $this->reference = $this->getName();
40
        if ($reference) {
41
            $this->reference = $reference;
42
        }
43
        return $this;
44
    }
45
46
    protected function setInput($value)
47
    {
48
        $this->input = $value;
49
        return $this;
50
    }
51
52
    public function getName()
53
    {
54
        return $this->name;
55
    }
56
57
    protected function setName($value)
58
    {
59
        $this->name = $value;
60
        return $this;
61
    }
62
63
64
65
    public function getOutput()
66
    {
67
        return $this->output;
68
    }
69
70
    protected function setOutput($value)
71
    {
72
        $this->output = $value;
73
        return $this;
74
    }
75
76
    public function getWrappers()
77
    {
78
        return $this->wrappers;
79
    }
80
//
81
//    public function isActive()
82
//    {
83
//        return $this->active;
84
//    }
85
86
    public function getScore()
87
    {
88
        return $this->score;
89
    }
90
91
    public function setScore($score)
92
    {
93
        $this->score = $score;
94
        return $this;
95
    }
96
97
    public function pushWrapper($wrapper)
98
    {
99
        array_unshift($this->wrappers, $wrapper);
100
        return $this;
101
    }
102
103
104
    public function build($method, $arguments = [])
105
    {
106
        $className = __NAMESPACE__ . '\\' . ucfirst($method) . 'Handler';
107
108
        if (class_exists($className)) {
109
110
            $reflection = new ReflectionClass($className);
111
112
            if (!$reflection->isInstantiable()) {
113
                throw new Exception(sprintf('"%s" must be instantiable', $className));
114
            }
115
116
            return $reflection->newInstanceArgs($arguments);
117
        }
118
        throw new Exception(sprintf('"%s" is not a valid rule name', $method));
119
    }
120
121
}