CerberusService::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace devtoolboxuk\cerberus;
4
5
use devtoolboxuk\cerberus\handlers\Handler;
6
use ReflectionClass;
7
8
class CerberusService extends AbstractService 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
     * @return $this
21
     */
22 4
    public function resetHandlers()
23 4
    {
24 4
        $this->references = [];
25 4
        $this->handlers = [];
26
        self::$instance = null;
27
        $this->initiateServices();
28
        return $this;
29
    }
30
31
    /**
32
     * @param $method
33
     * @param array $arguments
34
     * @return $this
35
     * @throws \Exception
36 4
     */
37
    public function __call($method, $arguments = [])
38 4
    {
39 4
        $handlers = new Handler($arguments);
40 4
        $handler = $handlers->build($method, $arguments);
41
        $this->pushHandler($handler, null);
42
        return $this;
43
    }
44
45
    /**
46
     * @param $handler
47
     * @param string|null $reference
48
     * @return $this
49
     */
50
    public function pushHandler($handler, $reference = null)
51 4
    {
52
53 4
        $handler->setHandlerReference($reference);
54 4
        array_unshift($this->handlers, $handler);
55 4
        $this->clearResults();
56
        return $this;
57
    }
58 4
59 4
    /**
60 4
     * @return mixed
61
     * @throws \ReflectionException
62
     */
63 4
    public function toArray()
64
    {
65
        return $this->process()->toArray();
66
    }
67
68
    /**
69
     * @return object|null
70
     * @throws \ReflectionException
71
     */
72 4
    private function process()
73
    {
74 4
        foreach ($this->handlers as $key => $handler) {
75
            $this->processWrappers($handler);
76
            array_unshift($this->references, ['handler' => $handler->getHandlerName(), 'input' => $handler->getInput(), 'output' => $this->getOutput(), 'name' => $this->getHandlerName()]);
77 4
        }
78
79 4
        if (self::$instance === null) {
80
            $reflection = new ReflectionClass('devtoolboxuk\\cerberus\\Models\\Cerberus');
81
            self::$instance = $reflection->newInstance($this->references, $this->score, $this->result);
82
        }
83
84
        return self::$instance;
85
    }
86
87
    /**
88
     * @return mixed
89
     * @throws \ReflectionException
90
     */
91
    public function getLogsAsJson()
92
    {
93
        return $this->process()->getJsonLogs();
94
    }
95
96
    /**
97
     * @return mixed
98
     * @throws \ReflectionException
99
     */
100
    public function getLogsAsArray()
101
    {
102
        return $this->process()->getArrayLogs();
103
    }
104
105
    /**
106
     * @return mixed
107
     * @throws \ReflectionException
108
     */
109
    public function getReferences()
110
    {
111
        return $this->process()->getReferences();
112
    }
113
114
    /**
115
     * @param $name
116
     * @return mixed
117
     * @throws \ReflectionException
118
     */
119
    public function getOutputByName($name)
120
    {
121
        foreach ($this->getReferences() as $reference)
122
        {
123
            if ($name == $reference->getName()) {
124
                return $reference->getOutPut();
125
            }
126
        }
127
    }
128
129
    /**
130
     * @param $name
131
     * @return mixed
132
     * @throws \ReflectionException
133
     */
134
    public function getInputByName($name)
135
    {
136
        foreach ($this->getReferences() as $reference)
137
        {
138
            if ($name == $reference->getName()) {
139
                return $reference->getInput();
140
            }
141
        }
142
    }
143
144
    /**
145
     * Alias of getReferences
146
     * @return mixed
147
     * @throws \ReflectionException
148
     */
149
    public function references()
150
    {
151
        return $this->getReferences();
152
    }
153
154
    /**
155
     * @return mixed
156
     * @throws \ReflectionException
157
     */
158
    public function outputs()
159
    {
160
        return $this->process()->getOutputs();
161
    }
162
163
    /**
164
     * @return mixed
165
     * @throws \ReflectionException
166
     */
167
    public function inputs()
168
    {
169
        return $this->process()->getInputs();
170
    }
171
172
    /**
173
     * @return mixed
174
     * @throws \ReflectionException
175
     */
176
    public function hasScore()
177
    {
178
        return $this->process()->hasScore();
179
    }
180
181
    /**
182
     * @return mixed
183
     * @throws \ReflectionException
184
     */
185
    public function getResult()
186
    {
187
        return $this->process()->getResult();
188
    }
189
190
    /**
191
     * @return mixed
192
     * @throws \ReflectionException
193
     */
194
    public function getScore()
195
    {
196
        return $this->process()->getScore();
197
    }
198
199
}