GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#111)
by
unknown
03:02
created

GridFieldConfigBlockManager::__construct()   C

Complexity

Conditions 10
Paths 96

Size

Total Lines 90
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 5.1578
c 0
b 0
f 0
cc 10
eloc 59
nc 96
nop 5

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
namespace SheaDawson\Blocks\forms;
4
5
use SheaDawson\Blocks\model\Block;
6
use SheaDawson\Blocks\model\BlockSet;
7
8
use SilverStripe\Control\Controller;
9
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\GridField\GridFieldConfig;
12
use SilverStripe\Forms\GridField\GridFieldDataColumns;
13
use SilverStripe\Forms\GridField\GridFieldButtonRow;
14
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
15
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
16
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
17
use SilverStripe\Forms\GridField\GridFieldDetailForm;
18
use SilverStripe\Forms\GridField\GridFieldCopyButton;
19
use SilverStripe\Forms\GridField\GridFieldEditButton;
20
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
21
22
/**
23
 * GridFieldConfig_BlockManager
24
 * Provides a reusable GridFieldConfig for managing Blocks.
25
 *
26
 * @author Shea Dawson <[email protected]>
27
 */
28
class GridFieldConfigBlockManager extends GridFieldConfig
29
{
30
    public $blockManager;
31
32
    public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false)
33
    {
34
        parent::__construct();
35
36
        $this->blockManager = Injector::inst()->get("SheaDawson\Blocks\BlockManager");
37
        $controllerClass = Controller::curr()->class;
38
        // Get available Areas (for page) or all in case of ModelAdmin
39
        if ($controllerClass == 'CMSPageEditController') {
40
            $currentPage = Controller::curr()->currentPage();
41
            $areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName);
42
        } else {
43
            $areasFieldSource = $this->blockManager->getAreasForTheme();
44
        }
45
46
        // EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved
47
        if ($editableRows) {
48
            $this->addComponent($editable = new GridFieldEditableColumns());
49
            $displayfields = array(
50
                'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'),
51
                'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'ReadonlyField'),
52
                'BlockArea' => array(
53
                    'title' => _t('Block.BlockArea', 'Block Area').'
54
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
55
                        // the &nbsp;s prevent wrapping of dropdowns
56
                    'callback' => function () use ($areasFieldSource) {
57
                            $areasField = DropdownField::create('BlockArea', 'Block Area', $areasFieldSource);
58
                            if (count($areasFieldSource) > 1) {
59
                                $areasField->setHasEmptyDefault(true);
60
                            }
61
                            return $areasField;
62
                        },
63
                ),
64
                'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'),
65
                'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'LiteralField'),
66
            );
67
68
            if ($aboveOrBelow) {
69
                $displayfields['AboveOrBelow'] = array(
70
                    'title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'),
71
                    'callback' => function () {
72
                        return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options'));
73
                    },
74
                );
75
            }
76
            $editable->setDisplayFields($displayfields);
77
        } else {
78
            $this->addComponent($dcols = new GridFieldDataColumns());
79
80
            $displayfields = array(
81
                'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'),
82
                'Title' => _t('Block.Title', 'Title'),
83
                'BlockArea' => _t('Block.BlockArea', 'Block Area'),
84
                'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'),
85
                'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'),
86
            );
87
            $dcols->setDisplayFields($displayfields);
88
            $dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw'));
89
        }
90
91
        $this->addComponent(new GridFieldButtonRow('before'));
92
        $this->addComponent(new GridFieldToolbarHeader());
93
        $this->addComponent(new GridFieldDetailForm());
94
        $this->addComponent($sort = new GridFieldSortableHeader());
95
        $this->addComponent($filter = new GridFieldFilterHeader());
96
        $this->addComponent(new GridFieldDetailForm());
97
        if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) {
98
            $this->addComponent(new GridFieldCopyButton());
99
        }
100
101
        $filter->setThrowExceptionOnBadDataType(false);
102
        $sort->setThrowExceptionOnBadDataType(false);
103
104
        if ($canAdd) {
105
            $multiClass = new GridFieldAddNewMultiClass();
106
            $classes = $this->blockManager->getBlockClasses();
107
            $multiClass->setClasses($classes);
108
            $this->addComponent($multiClass);
109
            //$this->addComponent(new GridFieldAddNewButton());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
        }
111
112
        if ($canEdit) {
113
            $this->addComponent(new GridFieldEditButton());
114
        }
115
116
        if ($canDelete) {
117
            $this->addComponent(new GridFieldDeleteAction(true));
118
        }
119
120
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
121
    }
122
123
    /**
124
     * Add the GridFieldAddExistingSearchButton component to this grid config.
125
     *
126
     * @return $this
127
     **/
128
    public function addExisting()
129
    {
130
        $classes = $this->blockManager->getBlockClasses();
131
132
        $this->addComponent($add = new GridFieldAddExistingSearchButton());
133
        $add->setSearchList(Block::get()->filter(array(
134
            'ClassName' => array_keys($classes),
135
        )));
136
137
        return $this;
138
    }
139
140
    /**
141
     * Add the GridFieldBulkManager component to this grid config.
142
     *
143
     * @return $this
144
     **/
145
    public function addBulkEditing()
146
    {
147
        if (class_exists('GridFieldBulkManager')) {
148
            $this->addComponent(new GridFieldBulkManager());
149
        }
150
151
        return $this;
152
    }
153
}
154