Completed
Push — master ( e5ab12...9197f0 )
by Konstantinos
09:09
created

LinkToFunction::__invoke()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
ccs 0
cts 12
cp 0
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 20
nc 9
nop 8
crap 72

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace BZIon\Twig;
4
5
class LinkToFunction
6
{
7
    /**
8
     * Get a link literal to a Model
9
     *
10
     * @param          $context
11
     * @param  \Model  $model     The model we want to link to
12
     * @param  string  $icon      A font awesome icon identifier to show instead of text
13
     * @param  string  $action    The action to link to (e.g show or edit)
14
     * @param  bool $linkAll   Whether to link to inactive or deleted models
15
     * @param  string  $class     The CSS class(es) to apply to the link
16
     * @param  bool $forceText Whether to show both the icon and text
17
     * @param  string  $content   Override the content that will automatically be used
18
     *
19
     * @return string The HTML link
20
     */
21
    public function __invoke(
22
        $context,
23
        \Model $model,
24
        $icon = null,
25
        $action = 'show',
26
        $linkAll = false,
27
        $class = '',
28
        $forceText = false,
29
        $content = ''
30
    ) {
31
        if (empty($content)) {
32
            $content = $this->getContent($model, $icon, $forceText);
33
        } elseif ($icon) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $icon of type string|null is loosely compared to true; 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...
34
            $content = "<i class=\"fa fa-$icon\"></i> " . $content;
35
        }
36
37
        if ($model instanceof \UrlModel && ($linkAll || !isset($context['app']) || $context['app']->getController()->canSee($model))) {
38
            $params = array();
39
            if ($linkAll) {
40
                $params['showDeleted'] = true;
41
            }
42
43
            $url = $model->getURL($action, false, $params);
44
45
            return '<a' . $this->getClass($class) . ' href="' . $url . '">' . $content . '</a>';
46
        }
47
48
        return '<span' . $this->getClass("$class disabled-link") . '>' . $content . '</span>';
49
    }
50
51
    /**
52
     * Get the content of the link to show
53
     *
54
     * @param  \Model  $model     The model we want to link to
55
     * @param  string  $icon      A font awesome icon identifier to show instead of text
56
     * @param  bool $forceText Whether to show both the icon and text
57
     * @return string  The link's content
58
     */
59
    private function getContent($model, $icon, $forceText)
60
    {
61
        $content = "";
62
63
        if ($icon) {
64
            $content .= "<i class=\"fa fa-$icon\"></i>";
65
66
            if ($forceText) {
67
                $content .= " ";
68
            }
69
        }
70
71
        if (!$icon || $forceText) {
72
            $content .= \Model::escape($this->getModelName($model));
73
        }
74
75
        return $content;
76
    }
77
78
    /**
79
     * Get the name of any model
80
     *
81
     * @param  \Model $model
82
     * @return string The name of the model
83
     */
84
    private function getModelName(\Model $model)
85
    {
86
        if ($model instanceof \NamedModel) {
87
            return $model->getName();
88
        }
89
        if ($model instanceof \AliasModel) {
90
            return $model->getAlias();
91
        }
92
93
        return $model->getId();
94
    }
95
96
    /**
97
     * Create a CSS class string
98
     *
99
     * @param string The CSS class(es), without `class=".."`
100
     */
101
    private function getClass($class)
102
    {
103
        if (trim($class) == '') {
104
            return $class;
105
        }
106
107
        return ' class="' . $class . '"';
108
    }
109
110 1
    public static function get()
111
    {
112 1
        return new \Twig_SimpleFunction('link_to', new self(), array(
113 1
            'is_safe'       => array('html'),
114
            'needs_context' => true
115
        ));
116
    }
117
}
118