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.

BlockSet   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A pageTypeOptions() 0 14 2
A Pages() 0 15 3
A canView() 0 4 1
A canEdit() 0 4 2
A canDelete() 0 4 2
A canCreate() 0 4 2
A providePermissions() 0 17 1
B getCMSFields() 0 34 2
1
<?php
2
3
namespace SheaDawson\Blocks\Model;
4
5
use SheaDawson\Blocks\Forms\GridFieldConfigBlockManager;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\ArrayLib;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Security\PermissionProvider;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\Security\Group;
12
use SilverStripe\Forms\TreeMultiselectField;
13
use SilverStripe\Forms\CheckboxField;
14
use SilverStripe\Forms\HeaderField;
15
use SilverStripe\Forms\LiteralField;
16
use SilverStripe\Forms\GridField\GridField;
17
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
18
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
19
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
20
use Symbiote\MultiValueField\Fields\MultiValueCheckboxField;
21
use Symbiote\MultiValueField\ORM\FieldType\MultiValueField;
22
23
/**
24
 * BlockSet.
25
 *
26
 * @author Shea Dawson <[email protected]>
27
 */
28
class BlockSet extends DataObject implements PermissionProvider
29
{
30
    /**
31
     * @var array
32
     **/
33
    private static $table_name = "BlockSet";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
    /**
36
     * @var array
37
     **/
38
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
39
        'Title' => 'Varchar(255)',
40
        'PageTypes' => MultiValueField::class,
41
        'IncludePageParent' => 'Boolean',
42
    ];
43
44
    /**
45
     * @var array
46
     **/
47
    private static $many_many = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
        "Blocks" => Block::class,
49
        "PageParents" => SiteTree::class,
50
    ];
51
52
    /**
53
     * @var array
54
     **/
55
    private static $many_many_extraFields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $many_many_extraFields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
56
        'Blocks' => [
57
            'Sort' => 'Int',
58
            'BlockArea' => 'Varchar',
59
            'AboveOrBelow' => 'Varchar',
60
        ],
61
    ];
62
63
    /**
64
     * @var array
65
     **/
66
    private static $above_or_below_options = [
0 ignored issues
show
Unused Code introduced by
The property $above_or_below_options is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
67
        'Above' => 'Above Page Blocks',
68
        'Below' => 'Below Page Blocks',
69
    ];
70
71
    public function getCMSFields()
72
    {
73
        $fields = parent::getCMSFields();
74
75
        $fields->removeFieldFromTab('Root', 'PageParents');
76
77
        $fields->addFieldToTab('Root.Main', HeaderField::create('SettingsHeading', _t('BlockSet.Settings', 'Settings')), 'Title');
78
        $fields->addFieldToTab('Root.Main', MultiValueCheckboxField::create('PageTypes', _t('BlockSet.OnlyApplyToThesePageTypes', 'Only apply to these Page Types:'), $this->pageTypeOptions())
79
                ->setDescription(_t('BlockSet.OnlyApplyToThesePageTypesDescription', 'Selected Page Types will inherit this Block Set automatically. Leave all unchecked to apply to all page types.')));
80
        $fields->addFieldToTab('Root.Main', TreeMultiselectField::create('PageParents', _t('BlockSet.OnlyApplyToChildrenOfThesePages', 'Only apply to children of these Pages:'), 'SilverStripe\\CMS\\Model\\SiteTree'));
81
        $fields->addFieldToTab('Root.Main', CheckboxField::create('IncludePageParent', _t('BlockSet.ApplyBlockSetToSelectedPageParentsAsWellAsChildren','Apply block set to selected page parents as well as children')));
82
83
        if (!$this->ID) {
84
            $fields->addFieldToTab('Root.Main', LiteralField::create('NotSaved', "<p class='message warning'>"._t('BlockSet.YouCanAddBlocksToThisSetOnceYouHaveSavedIt', 'You can add Blocks to this set once you have saved it for the first time').'</p>'));
85
86
            return $fields;
87
        }
88
89
        $fields->removeFieldFromTab('Root', 'Blocks');
90
91
        /**
92
        * @todo - change relation editor back to the custom block manager config and fix issues when 'creating' Blocks from a BlockSet.
93
		*/
94
        $gridConfig = GridFieldConfig_RelationEditor::create();
95
        $gridConfig->addComponent(new GridFieldOrderableRows('Sort'));
96
        $gridConfig->addComponent(new GridFieldDeleteAction());
97
98
        $gridSource = $this->Blocks()->Sort('Sort');
0 ignored issues
show
Documentation Bug introduced by
The method Blocks does not exist on object<SheaDawson\Blocks\Model\BlockSet>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
100
        $fields->addFieldToTab('Root.Blocks', HeaderField::create('BlocksHeading', _t('Block.PLURALNAME', 'Blocks')));
101
        $fields->addFieldToTab('Root.Blocks', GridField::create('Blocks', _t('Block.PLURALNAME', 'Blocks'), $gridSource, $gridConfig));
102
103
        return $fields;
104
    }
105
106
    /**
107
     * Returns a sorted array suitable for a dropdown with pagetypes and their translated name.
108
     *
109
     * @return array
110
     */
111
    protected function pageTypeOptions()
112
    {
113
        $pageTypes = [];
114
        $classes = ArrayLib::valueKey(SiteTree::page_type_classes());
115
        unset($classes['VirtualPage']);
116
        unset($classes['ErrorPage']);
117
        unset($classes['RedirectorPage']);
118
        foreach ($classes as $pageTypeClass) {
119
            $pageTypes[$pageTypeClass] = singleton($pageTypeClass)->i18n_singular_name();
120
        }
121
        asort($pageTypes);
122
123
        return $pageTypes;
124
    }
125
126
    /**
127
     * Returns a list of pages this BlockSet features on.
128
     *
129
     * @return DataList
130
     */
131
    public function Pages()
132
    {
133
        $pages = SiteTree::get();
134
        $types = $this->PageTypes->getValue();
0 ignored issues
show
Documentation introduced by
The property PageTypes does not exist on object<SheaDawson\Blocks\Model\BlockSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
        if (count($types)) {
136
            $pages = $pages->filter('ClassName', $types);
137
        }
138
139
        $parents = $this->PageParents()->column('ID');
0 ignored issues
show
Documentation Bug introduced by
The method PageParents does not exist on object<SheaDawson\Blocks\Model\BlockSet>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
140
        if (count($parents)) {
141
            $pages = $pages->filter('ParentID', $parents);
142
        }
143
144
        return $pages;
145
    }
146
147
    public function canView($member = null)
148
    {
149
        return true;
150
    }
151
152
    public function canEdit($member = null)
153
    {
154
        return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_EDIT', 'any', $member);
155
    }
156
157
    public function canDelete($member = null)
158
    {
159
        return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_DELETE', 'any', $member);
160
    }
161
162
    public function canCreate($member = null, $context = [])
163
    {
164
        return Permission::check('ADMIN', 'any', $member) || Permission::check('BLOCK_CREATE', 'any', $member);
165
    }
166
167
    public function providePermissions()
168
    {
169
        return [
170
            'BLOCKSET_EDIT' => [
171
                'name' => _t('BlockSet.EditBlockSet','Edit a Block Set'),
172
                'category' => _t('Block.PermissionCategory', 'Blocks'),
173
            ],
174
            'BLOCKSET_DELETE' => [
175
                'name' => _t('BlockSet.DeleteBlockSet','Delete a Block Set'),
176
                'category' => _t('Block.PermissionCategory', 'Blocks'),
177
            ],
178
            'BLOCKSET_CREATE' => [
179
                'name' => _t('BlockSet.CreateBlockSet','Create a Block Set'),
180
                'category' => _t('Block.PermissionCategory', 'Blocks'),
181
            ],
182
        ];
183
    }
184
}
185