Completed
Push — develop ( ddf000...96b89c )
by Kevin
10:14 queued 06:44
created

MagiumRenderer::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 21
cts 21
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 6
nop 4
crap 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kschr
5
 * Date: 3/20/2017
6
 * Time: 4:54 PM
7
 */
8
9
namespace Magium\Configuration\View;
10
11
use Zend\Form\Element;
12
use Zend\Form\ElementInterface;
13
use Zend\Form\View\Helper\FormElement;
14
use Zend\Form\View\Helper\FormInput;
15
use Zend\Form\View\Helper\FormSelect;
16
use Zend\View\Helper\AbstractHelper;
17
18
class MagiumRenderer extends AbstractHelper
19
{
20 1
    function __invoke($section, $group, $id, $options)
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...
21
    {
22 1
        $type = 'text';
23 1
        if (isset($options['type'])) {
24 1
            $type = $options['type'];
25
        }
26 1
        $value = $options['value'];
27
28 1
        $viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type));
29 1
        $formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type));
30 1
        $reflectionClass = new \ReflectionClass($viewClass);
31 1
        if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) {
32
            throw new InvalidViewConfigurationException('Invalid setting input type');
33
        }
34
35 1
        $instance = $reflectionClass->newInstance();
36 1
        $formInstance = new \ReflectionClass($formClass);
37
38 1
        $instance->setView($this->getView());
39 1
        $name = sprintf('%s_%s_%s', $section, $group, $id);
40 1
        $formElement = $formInstance->newInstance($name, $options);
41
        /* @var $formElement Element */
42 1
        if ($formElement instanceof Element\Select) {
43
            $formElement->setOptions(['options' => $options['source']]);
44
        }
45 1
        $formElement->setAttribute('onchange', 'magiumRegisterChange(event)');
46 1
        $formElement->setAttribute('class', 'form-control');
47 1
        $formElement->setAttribute('data-path', $options['path']);
48 1
        $formElement->setValue($value);
49 1
        $output = $instance->render($formElement);
50 1
        return $output;
51
    }
52
53
54
}
55