LinkPrinter::createLinks()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
rs 8.5125
cc 5
eloc 16
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\web\renderers\link;
3
4
use rtens\domin\ActionRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\ExecutionToken;
7
use rtens\domin\delivery\web\resources\ExecutionResource;
8
use rtens\domin\delivery\web\Url;
9
use rtens\domin\delivery\web\WebCommentParser;
10
11
class LinkPrinter {
12
13
    /** @var LinkRegistry */
14
    private $links;
15
16
    /** @var ActionRegistry */
17
    private $actions;
18
19
    /** @var WebCommentParser */
20
    private $parser;
21
22
    /** @var ExecutionToken */
23
    private $token;
24
25
    public function __construct(LinkRegistry $links, ActionRegistry $actions, WebCommentParser $parser,
26
                                ExecutionToken $token = null) {
27
        $this->links = $links;
28
        $this->actions = $actions;
29
        $this->parser = $parser;
30
        $this->token = $token;
31
    }
32
33
    /**
34
     * @param mixed $object
35
     * @return array
36
     */
37
    public function createLinkElements($object) {
38
        return $this->createLinks($object, 'btn btn-xs btn-primary');
39
    }
40
41
    /**
42
     * @param mixed $object
43
     * @param string|null $caption
44
     * @return array|\rtens\domin\delivery\web\Element[]
45
     */
46
    public function createDropDown($object, $caption = null) {
47
        $links = $this->createLinks($object);
48
        if (empty($links)) {
49
            return [];
50
        }
51
52
        if (!$caption) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $caption of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
            if (is_object($object)) {
54
                $caption = (new \ReflectionClass($object))->getShortName();
55
            } else {
56
                $caption = 'Actions';
57
            }
58
        }
59
60
        return [
61
            new Element('div', ['class' => 'dropdown'], [
62
                new Element('button', [
63
                    'class' => 'btn btn-xs btn-primary dropdown-toggle',
64
                    'type' => 'button',
65
                    'data-toggle' => 'dropdown',
66
                    'aria-haspopup' => 'true',
67
                    'aria-expanded' => 'false'
68
                ], [
69
                    $caption,
70
                    new Element('span', ['class' => 'caret'])
71
                ]),
72
                new Element('ul', ['class' => 'dropdown-menu'], array_map(function (Element $element) {
73
                    return new Element('li', [], [$element]);
74
                }, $links))
75
            ])
76
        ];
77
    }
78
79
    private function createLinks($object, $classes = '') {
80
        return array_map(function (Link $link) use ($object, $classes) {
81
            $action = $this->actions->getAction($link->actionId());
82
83
            $parameters = $link->parameters($object);
84
            if ($action->isModifying() && $this->token) {
85
                $parameters[ExecutionResource::TOKEN_ARG] = $this->token->generate($link->actionId());
86
            }
87
88
            $url = (string)Url::relative($link->actionId(), $parameters);
89
90
            $attributes = ['class' => $classes, 'href' => $url];
91
            if ($link->confirm() !== null) {
92
                $attributes['onclick'] = "return confirm('{$link->confirm()}');";
93
            }
94
            $description = $action->description();
95
            if (!is_null($description)) {
96
                $attributes['title'] = str_replace('"', "'", strip_tags($this->parser->shorten($description)));
97
            }
98
            return new Element('a', $attributes, [
99
                $action->caption()
100
            ]);
101
        }, $this->links->getLinks($object));
102
    }
103
}