Completed
Branch master (2e2c40)
by Rob
01:16
created

CerberusService::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
15
    {
16 4
        $this->resetHandlers();
17 4
    }
18
19 4
    public function resetHandlers()
20
    {
21 4
        $this->references = [];
22 4
        $this->handlers = [];
23 4
        self::$instance = null;
24 4
        return $this;
25
    }
26
27
    public function __call($method, $arguments = [])
28
    {
29
        $handlers = new Handler($arguments);
30
        $handler = $handlers->build($method, $arguments);
31
        $this->pushHandler($handler);
32
        return $this;
33
    }
34
35 4
    public function pushHandler($handler)
36
    {
37 4
        array_unshift($this->handlers, $handler);
38 4
        $this->clearResults();
39 4
        return $this;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function toArray()
46
    {
47
        return $this->process()->toArray();
48
    }
49
50 4
    public function process()
51
    {
52 4
        foreach ($this->handlers as $handler) {
53 4
            array_unshift($this->references, ['name' => $handler->getName(), 'value' => $handler->getValue()]);
54 4
            $this->processWrappers($handler);
55
        }
56
57 4
        if (self::$instance === null) {
58 4
            $reflection = new ReflectionClass('devtoolboxuk\\cerberus\\Models\\CerberusModel');
59 4
            self::$instance = $reflection->newInstance($this->references, $this->score, $this->result);
60
        }
61
62 4
        return self::$instance;
63
    }
64
65
    public function hasScore()
66
    {
67
        return $this->process()->hasScore();
68
    }
69
70
71 4
    public function getResult()
72
    {
73 4
        return $this->process()->getResult();
74
    }
75
76 4
    public function getScore()
77
    {
78 4
        return $this->process()->getScore();
79
    }
80
81
}