Completed
Push — master ( 898500...e76c1b )
by Nikolas
03:28
created

ActionPanelRenderer::makeUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ActionPanelRenderer::headElements() 0 8 2
1
<?php
2
namespace rtens\domin\delivery\web\renderers\dashboard;
3
4
use rtens\domin\ActionRegistry;
5
use rtens\domin\delivery\FieldRegistry;
6
use rtens\domin\delivery\RendererRegistry;
7
use rtens\domin\delivery\web\Element;
8
use rtens\domin\delivery\web\renderers\dashboard\types\ActionPanel;
9
use rtens\domin\delivery\web\renderers\dashboard\types\Panel;
10
use rtens\domin\delivery\web\Url;
11
use rtens\domin\delivery\web\WebRenderer;
12
use rtens\domin\Parameter;
13
14
class ActionPanelRenderer implements WebRenderer {
15
16
    /** @var RendererRegistry */
17
    private $renderers;
18
19
    /** @var ActionRegistry */
20
    private $actions;
21
22
    private $results = [];
23
24
    /** @var FieldRegistry */
25
    private $fields;
26
27
    /**
28
     * @param RendererRegistry $renderers
29
     * @param ActionRegistry $actions
30
     * @param FieldRegistry $fields
31
     */
32
    public function __construct(RendererRegistry $renderers, ActionRegistry $actions, FieldRegistry $fields) {
33
        $this->renderers = $renderers;
34
        $this->actions = $actions;
35
        $this->fields = $fields;
36
    }
37
38
    /**
39
     * @param mixed $value
40
     * @return bool
41
     */
42
    public function handles($value) {
43
        return $value instanceof ActionPanel;
44
    }
45
46
    /**
47
     * @param ActionPanel $value
48
     * @return mixed
49
     */
50
    public function render($value) {
51
        $heading = $this->actions->getAction($value->getActionId())->caption();
52
        return (string)(new Panel($heading, $this->getContent($value)))
53
            ->setMaxHeight($value->getMaxHeight())
54
            ->setRightHeading([new Element('a', [
55
                'href' => (string)Url::relative($value)
0 ignored issues
show
Documentation introduced by
$value is of type object<rtens\domin\deliv...oard\types\ActionPanel>, but the function expects a array|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
            ], [new Element('span', ['class' => 'glyphicon glyphicon-circle-arrow-right'])])])
57
            ->render($this->renderers);
58
    }
59
60
    /**
61
     * @param mixed $value
62
     * @return array|Element[]
63
     */
64
    public function headElements($value) {
65
        $content = $this->getContent($value);
66
        $renderer = $this->renderers->getRenderer($content);
67
        if ($renderer instanceof WebRenderer) {
68
            return $renderer->headElements($content);
69
        }
70
        return [];
71
    }
72
73
    private function getContent(ActionPanel $value) {
74
        $key = spl_object_hash($value);
75
76
        if (!isset($this->results[$key])) {
77
            $action = $this->actions->getAction($value->getActionId());
78
            $this->results[$key] = $action->execute($this->inflate($action->parameters(), $value->getParameters()));
79
        }
80
        return $this->results[$key];
81
    }
82
83
    /**
84
     * @param Parameter[] $parameters
85
     * @param mixed[] $values
86
     * @return mixed[]
87
     */
88
    private function inflate($parameters, $values) {
89
        $inflated = [];
90
        foreach ($parameters as $parameter) {
91
            $name = $parameter->getName();
92
            if (isset($values[$name])) {
93
                $inflated[$name] = $this->fields->getField($parameter)->inflate($parameter, $values[$name]);
94
            }
95
        }
96
        return $inflated;
97
    }
98
}