Completed
Pull Request — master (#84)
by Robbie
08:08
created

LDAPGroupMapping   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 31 3
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Model;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class LDAPGroupMapping
10
 *
11
 * An individual mapping of an LDAP group to a SilverStripe {@link Group}
12
 */
13
class LDAPGroupMapping extends DataObject
14
{
15
    /**
16
     * {@inheritDoc}
17
     * @var string
18
     */
19
    private static $table_name = 'LDAPGroupMapping';
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...
20
21
    /**
22
     * @var array
23
     */
24
    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...
25
        'DN' => 'Text', // the DN value of the LDAP object in AD, e.g. CN=Users,DN=playpen,DN=local
26
        'Scope' => 'Enum("Subtree,OneLevel","Subtree")' // the scope of the mapping
27
    ];
28
29
    /**
30
     * @var array
31
     */
32
    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...
33
        'Group' => 'SilverStripe\\Security\\Group'
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    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...
40
        'DN'
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    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...
47
        'ldapService' => '%$SilverStripe\\ActiveDirectory\\Services\\LDAPService'
48
    ];
49
50
    /**
51
     * {@inheritDoc}
52
     * @return FieldList
53
     */
54
    public function getCMSFields()
55
    {
56
        $fields = parent::getCMSFields();
57
        $fields->removeByName('DN');
58
59
        $field = DropdownField::create('DN', _t('LDAPGroupMapping.LDAPGROUP', 'LDAP Group'));
60
        $field->setEmptyString(_t('LDAPGroupMapping.SELECTONE', 'Select one'));
61
        $groups = $this->ldapService->getGroups(true, ['dn', 'name']);
0 ignored issues
show
Documentation introduced by
The property ldapService does not exist on object<SilverStripe\Acti...Model\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...
62
        if ($groups) {
63
            foreach ($groups as $dn => $record) {
64
                $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...
65
            }
66
        }
67
        asort($source);
68
        $field->setSource($source);
69
        $fields->addFieldToTab('Root.Main', $field);
70
71
        $fields->removeByName('Scope');
72
        $fields->addFieldToTab(
73
            'Root.Main',
74
            DropdownField::create('Scope', _t('LDAPGroupMapping.SCOPE', 'Scope'), [
75
                'Subtree' => _t(
76
                    'LDAPGroupMapping.SUBTREE_DESCRIPTION',
77
                    'Users within this group and all nested groups within'
78
                ),
79
                'OneLevel' => _t('LDAPGroupMapping.ONELEVEL_DESCRIPTION', 'Only users within this group'),
80
            ])
81
        );
82
83
        return $fields;
84
    }
85
}
86