LDAPGroupMapping   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 28 3
1
<?php
2
/**
3
 * Class LDAPGroupMapping
4
 *
5
 * An individual mapping of an LDAP group to a SilverStripe {@link Group}
6
 */
7
class LDAPGroupMapping extends DataObject
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...
8
{
9
    /**
10
     * @var array
11
     */
12
    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...
13
        'DN' => 'Text', // the DN value of the LDAP object in AD, e.g. CN=Users,DN=playpen,DN=local
14
        'Scope' => 'Enum("Subtree,OneLevel","Subtree")' // the scope of the mapping
15
    ];
16
17
    /**
18
     * @var array
19
     */
20
    private static $has_one = [
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 $has_one 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...
21
        'Group' => 'Group'
22
    ];
23
24
    /**
25
     * @var array
26
     */
27
    private static $summary_fields = [
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 $summary_fields 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
        'DN'
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    private static $dependencies = [
0 ignored issues
show
Unused Code introduced by
The property $dependencies 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...
35
        'ldapService' => '%$LDAPService'
36
    ];
37
38
    public function getCMSFields()
39
    {
40
        $fields = parent::getCMSFields();
41
        $fields->removeByName('DN');
42
43
        $field = new DropdownField('DN', _t('LDAPGroupMapping.LDAPGROUP', 'LDAP Group'));
44
        $field->setEmptyString(_t('LDAPGroupMapping.SELECTONE', 'Select one'));
45
        $groups = $this->ldapService->getGroups(true, ['dn', 'name']);
0 ignored issues
show
Documentation introduced by
The property ldapService does not exist on object<LDAPGroupMapping>. 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...
46
        if ($groups) {
47
            foreach ($groups as $dn => $record) {
48
                $source[$dn] = sprintf('%s (%s)', $record['name'], $dn);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$source was never initialized. Although not strictly required by PHP, it is generally a good practice to add $source = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
49
            }
50
        }
51
        asort($source);
52
        $field->setSource($source);
53
        $fields->addFieldToTab('Root.Main', $field);
54
55
        $fields->removeByName('Scope');
56
        $fields->addFieldToTab(
57
            'Root.Main',
58
            new DropdownField('Scope', _t('LDAPGroupMapping.SCOPE', 'Scope'), [
59
                'Subtree' => _t('LDAPGroupMapping.SUBTREE_DESCRIPTION', 'Users within this group and all nested groups within'),
60
                'OneLevel' => _t('LDAPGroupMapping.ONELEVEL_DESCRIPTION', 'Only users within this group'),
61
            ])
62
        );
63
64
        return $fields;
65
    }
66
}
67