LangEditor::providePermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     LangEditor
7
 *
8
 */
9
class LangEditor extends LeftAndMain implements PermissionProvider {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
    /**
12
     * @var string
13
     * @config
14
     */
15
    private static $menu_title = 'Lang Editor';
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 $menu_title 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...
16
17
    /**
18
     * @var string
19
     * @config
20
     */
21
    private static $url_segment = 'lang-editor';
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 $url_segment 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...
22
23
    /**
24
     * @var float
25
     * @config
26
     */
27
    private static $menu_priority = -0.6;
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 $menu_priority 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...
28
29
    /**
30
     * @var array
31
     * @config
32
     */
33
    private static $allowed_actions = [
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 $allowed_actions 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
        'FormEntities',
35
    ];
36
37
    /**
38
     * Default configuration of items per page.
39
     * Set "null" if unlimited.
40
     *
41
     * @var int
42
     * @config
43
     */
44
    private static $items_per_page = 30;
0 ignored issues
show
Unused Code introduced by
The property $items_per_page 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...
45
46
    /**
47
     * @return array
48
     */
49
    public function providePermissions() {
50
        $title = _t('LangEditor.MENU_TITLE', 'Lang Editor');
51
52
        return [
53
            "CMS_ACCESS_LangEditorAdmin" => [
54
                'name'     => _t('CMSMain.ACCESS', "Access to '{title}' section", ['title' => $title]),
0 ignored issues
show
Documentation introduced by
array('title' => $title) is of type array<string,string,{"title":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
                'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'),
56
            ],
57
        ];
58
    }
59
60
    /**
61
     *
62
     * @param Member $member
63
     *
64
     * @return int|bool
65
     */
66
    public function canView($member = null) {
67
        return Permission::check('CMS_ACCESS_LangEditorAdmin', 'any', $member);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Permission::check('CMS_...dmin', 'any', $member); of type boolean|string|null adds the type string to the return on line 67 which is incompatible with the return type of the parent method LeftAndMain::canView of type boolean.
Loading history...
68
    }
69
70
    public function init() {
71
        static::config()->menu_title = _t('LangEditor.MENU_TITLE', 'Lang Editor');
72
73
        return parent::init();
74
    }
75
76
    public static function flushCache() {
77
        $cache = SS_Cache::factory(static::class);
78
        $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
79
    }
80
81
    public function getModules() {
82
        $list = new ArrayList();
83
        $parameters = $this->getRequest()->getVars();
84
85
        unset($parameters['url']);
86
        unset($parameters['start']);
87
        unset($parameters['search']);
88
89
        LangModule::get()->each(function (LangModule $module) use ($parameters, &$list) {
90
            if (array_key_exists('moduleId', $parameters) && $parameters['moduleId'] == $module->ID) {
91
                $module->Current = true;
0 ignored issues
show
Documentation introduced by
The property Current does not exist on object<LangModule>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
92
            }
93
94
            $parameters['moduleId'] = $module->ID;
95
96
            $module->Link = Controller::join_links(
0 ignored issues
show
Documentation introduced by
The property Link does not exist on object<LangModule>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
97
                Controller::curr()->Link(),
98
                "?".http_build_query($parameters)
99
            );
100
101
            $list->push($module);
102
        });
103
104
        if (! $list->filter('Current', true)->first() && $list->count() > 0) {
105
            $list->first()->Current = true;
106
        }
107
108
        return $list;
109
    }
110
111
    public function getCurrentModule() {
112
        return $this->getModules()->filter('Current', true)->first();
113
    }
114
115
    public function getCurrentSearchTerm() {
116
        return $this->getRequest()->getVar('search');
117
    }
118
119
    public function FormEntities() {
120
        /** @var LangModule $module */
121
        if ($module = $this->getModules()->filter('Current', true)->first()) {
122
            $entities = $module->Entities();
123
            $searchTerm = $this->getRequest()->getVar('search');
124
125
            if (! empty($searchTerm)) {
126
                $entities = $entities->filter('SearchFields:LangFulltextBoolean', $searchTerm);
127
            }
128
129
            $parameters = $this->getRequest()->getVars();
130
131
            unset($parameters['url']);
132
133
            $parameters = count($parameters) > 0 ? '?'.http_build_query($parameters) : '';
134
135
            $form = new FormEntities(Controller::curr(), __FUNCTION__, $entities);
136
            $form->clearMessage();
137
            $form->setFormAction(Controller::join_links(
138
                $this->Link('FormEntities'),
139
                $parameters
140
            ));
141
142
            return $form;
143
        }
144
145
        return false;
146
    }
147
}