Completed
Push — master ( f76706...507944 )
by Rob
02:03
created

CerberusService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 75
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resetHandlers() 0 7 1
A __call() 0 7 1
A pushHandler() 0 6 1
A toArray() 0 4 1
A process() 0 14 3
A hasScore() 0 4 1
A getResult() 0 4 1
A getScore() 0 4 1
1
<?php
2
3
namespace devtoolboxuk\cerberus;
4
5
use devtoolboxuk\cerberus\handlers\Handler;
6
use ReflectionClass;
7
8
class CerberusService extends AbstractCerberus implements CerberusInterface
9
{
10
    private static $instance = null;
11
    protected $handlers = [];
12
    protected $references = [];
13
14 4
    public function __construct()
15
    {
16 4
        $this->resetHandlers();
17 4
    }
18
19
20 4
    public function resetHandlers()
21
    {
22 4
        $this->references = [];
23 4
        $this->handlers = [];
24 4
        self::$instance = null;
25 4
        return $this;
26
    }
27
28
    public function __call($method, $arguments = [])
29
    {
30
        $handlers = new Handler($arguments);
31
        $handler = $handlers->build($method, $arguments);
32
        $this->pushHandler($handler);
33
        return $this;
34
    }
35
36 4
    public function pushHandler($handler)
37
    {
38 4
        array_unshift($this->handlers, $handler);
39 4
        $this->clearResults();
40 4
        return $this;
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function toArray()
47
    {
48
        return $this->process()->toArray();
49
    }
50
51 4
    public function process()
52
    {
53 4
        foreach ($this->handlers as $handler) {
54 4
            array_unshift($this->references, ['name' => $handler->getName(), 'value' => $handler->getValue()]);
55 4
            $this->processWrappers($handler);
56
        }
57
58 4
        if (self::$instance === null) {
59 4
            $reflection = new ReflectionClass('devtoolboxuk\\cerberus\\Models\\CerberusModel');
60 4
            self::$instance = $reflection->newInstance($this->references, $this->score, $this->result);
61
        }
62
63 4
        return self::$instance;
64
    }
65
66
    public function hasScore()
67
    {
68
        return $this->process()->hasScore();
69
    }
70
71
72 4
    public function getResult()
73
    {
74 4
        return $this->process()->getResult();
75
    }
76
77 4
    public function getScore()
78
    {
79 4
        return $this->process()->getScore();
80
    }
81
82
}