Completed
Push — master ( cd3b2e...50c111 )
by Craig
07:10
created

SearchBlock   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 4 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 4
loc 100
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
C display() 0 45 8
A getFormClassName() 0 4 1
A getFormOptions() 4 20 4
A getFormTemplate() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\SearchModule\Block;
13
14
use Zikula\BlocksModule\AbstractBlockHandler;
15
16
/**
17
 * Block to display a search form
18
 */
19
class SearchBlock extends AbstractBlockHandler
20
{
21
    /**
22
     * display block
23
     *
24
     * @param array $properties
25
     * @return string the rendered bock
26
     */
27
    public function display(array $properties)
28
    {
29
        $title = !empty($properties['title']) ? $properties['title'] : '';
30
        if (!$this->hasPermission('Searchblock::', "$title::", ACCESS_READ)) {
31
            return '';
32
        }
33
        // set defaults
34
        $properties['displaySearchBtn'] = isset($properties['displaySearchBtn']) ? $properties['displaySearchBtn'] : false;
35
        $properties['active'] = isset($properties['active']) ? $properties['active'] : [];
36
37
        // get Core-2.0 searchable modules
38
        $searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll();
39
        $moduleFormBuilder = $this->get('form.factory')
40
            ->createNamedBuilder('modules', 'Symfony\Component\Form\Extension\Core\Type\FormType', [], [
41
                'auto_initialize' => false,
42
                'required' => false
43
            ]);
44
        foreach ($searchableModules as $moduleName => $searchableInstance) {
45
            if (!in_array($moduleName, $properties['active'])) {
46
                continue;
47
            }
48
            if (!$this->hasPermission('ZikulaSearchModule::Item', $moduleName . '::', ACCESS_READ)) {
49
                continue;
50
            }
51
            $moduleFormBuilder->add($moduleName, 'Zikula\SearchModule\Form\Type\AmendableModuleSearchType', [
52
                'label' => $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName(),
53
                'translator' => $this->getTranslator(),
54
                'active' => true,
55
                'permissionApi' => $this->get('zikula_permissions_module.api.permission')
56
            ]);
57
            $searchableInstance->amendForm($moduleFormBuilder->get($moduleName));
58
        }
59
        $form = $this->get('form.factory')->create('Zikula\SearchModule\Form\Type\SearchType', [], [
60
            'translator' => $this->get('translator.default'),
61
            'action' => $this->get('router')->generate('zikulasearchmodule_search_execute')
62
        ]);
63
        $form->add($moduleFormBuilder->getForm());
64
65
        $templateParameters = [
66
            'form' => $form->createView(),
67
            'properties' => $properties,
68
        ];
69
70
        return $this->renderView('@ZikulaSearchModule/Block/search.html.twig', $templateParameters);
71
    }
72
73
    /**
74
     * Returns the fully qualified class name of the block's form class.
75
     *
76
     * @return string Template path
77
     */
78
    public function getFormClassName()
79
    {
80
        return 'Zikula\SearchModule\Block\Form\Type\SearchBlockType';
81
    }
82
83
    /**
84
     * Returns any array of form options.
85
     *
86
     * @return array Options array
87
     */
88
    public function getFormOptions()
89
    {
90
        $searchModules = [];
91
        $searchableModules = $this->get('zikula_search_module.internal.searchable_module_collector')->getAll();
92 View Code Duplication
        foreach (array_keys($searchableModules) as $moduleName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            $displayName = $this->get('kernel')->getModule($moduleName)->getMetaData()->getDisplayName();
94
            $searchModules[$displayName] = $moduleName;
95
        }
96
        // remove disabled
97
        foreach ($searchModules as $displayName => $moduleName) {
98
            if ((bool) $this->getVar('disable_' . $moduleName, false)) {
99
                unset($searchModules[$displayName]);
100
            }
101
        }
102
103
        // get all the search plugins
104
        return [
105
            'activeModules' => $searchModules
106
        ];
107
    }
108
109
    /**
110
     * Returns the template used for rendering the editing form.
111
     *
112
     * @return string Template path
113
     */
114
    public function getFormTemplate()
115
    {
116
        return '@ZikulaSearchModule/Block/search_modify.html.twig';
117
    }
118
}
119