Completed
Pull Request — develop (#61)
by Kevin
09:23
created

MagiumRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 37
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 32 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