Completed
Push — master ( 689e98...bb1346 )
by Craig
15:41
created

ExtensionsExtension::extensionActions()   D

Complexity

Conditions 13
Paths 23

Size

Total Lines 88
Code Lines 66

Duplication

Lines 47
Ratio 53.41 %

Importance

Changes 0
Metric Value
cc 13
eloc 66
nc 23
nop 1
dl 47
loc 88
rs 4.9922
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ExtensionsModule\Twig\Extension;
13
14
use Zikula\Common\Translator\TranslatorInterface;
15
use Zikula\ExtensionsModule\Api\ExtensionApi;
16
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
17
18
class ExtensionsExtension extends \Twig_Extension
19
{
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * ExtensionsExtension constructor.
27
     * @param TranslatorInterface $translator
28
     */
29
    public function __construct(TranslatorInterface $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * Returns a list of functions to add to the existing list.
36
     *
37
     * @return array An array of functions
38
     */
39
    public function getFunctions()
40
    {
41
        return [
42
            new \Twig_SimpleFunction('stateLabel', [$this, 'stateLabel'], ['is_safe' => ['html']]),
43
        ];
44
    }
45
46
    public function getFilters()
47
    {
48
        return [
49
            new \Twig_SimpleFilter('isCoreModule', ['ZikulaKernel', 'isCoreModule']),
50
        ];
51
    }
52
53
    public function stateLabel(ExtensionEntity $extensionEntity, array $upgradedExtensions = null)
54
    {
55
        switch ($extensionEntity->getState()) {
56
            case ExtensionApi::STATE_INACTIVE:
57
                $status = $this->translator->__('Inactive');
58
                $statusclass = "warning";
59
                break;
60
            case ExtensionApi::STATE_ACTIVE:
61
                $status = $this->translator->__('Active');
62
                $statusclass = "success";
63
                break;
64
            case ExtensionApi::STATE_MISSING:
65
                $status = $this->translator->__('Files missing');
66
                $statusclass = "danger";
67
                break;
68
            case ExtensionApi::STATE_UPGRADED:
69
                $status = $this->translator->__('New version');
70
                $statusclass = "danger";
71
                break;
72
            case ExtensionApi::STATE_INVALID:
73
                $status = $this->translator->__('Invalid structure');
74
                $statusclass = "danger";
75
                break;
76
            case ExtensionApi::STATE_NOTALLOWED:
77
                $status = $this->translator->__('Not allowed');
78
                $statusclass = "danger";
79
                break;
80
            case ExtensionApi::STATE_UNINITIALISED:
81
            default:
82
                if ($extensionEntity->getState() > 10) {
83
                    $status = $this->translator->__('Incompatible');
84
                    $statusclass = "info";
85
                } else {
86
                    $status = $this->translator->__('Not installed');
87
                    $statusclass = "primary";
88
                }
89
                break;
90
        }
91
92
        $newVersionString = ($extensionEntity->getState() == ExtensionApi::STATE_UPGRADED) ? '&nbsp;<span class="label label-warning">' . $upgradedExtensions[$extensionEntity->getName()] . '</span>' : null;
93
94
        return '<span class="label label-' . $statusclass . '">' . $status . '</span>' . $newVersionString;
95
    }
96
}
97