LDAPMigrateExistingMembersTask::log()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Class LDAPMigrateExistingMembersTask
4
 *
5
 * Migrate existing Member records in SilverStripe into "LDAP Members"
6
 * by matching existing emails with ones that exist in AD.
7
 */
8
class LDAPMigrateExistingMembersTask extends BuildTask
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...
9
{
10
    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...
11
        'ldapService' => '%$LDAPService'
12
    ];
13
14
    public function run($request)
15
    {
16
        $users = $this->ldapService->getUsers(['objectguid', 'mail']);
0 ignored issues
show
Bug introduced by
The property ldapService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        $start = time();
18
        $count = 0;
19
20
        foreach ($users as $user) {
21
            // Empty mail attribute for the user, nothing we can do. Skip!
22
            if (empty($user['mail'])) {
23
                continue;
24
            }
25
26
            $member = Member::get()->where(
27
                sprintf('"Email" = \'%s\' AND "GUID" IS NULL', Convert::raw2sql($user['mail']))
28
            )->first();
29
30
            if (!($member && $member->exists())) {
31
                continue;
32
            }
33
34
            // Member was found, migrate them by setting the GUID field
35
            $member->GUID = $user['objectguid'];
36
            $member->write();
37
38
            $count++;
39
40
            $this->log(sprintf(
41
                'Migrated Member %s (ID: %s, Email: %s)',
42
                $member->getName(),
43
                $member->ID,
44
                $member->Email
45
            ));
46
        }
47
48
        $end = time() - $start;
49
50
        $this->log(sprintf('Done. Migrated %s Member records. Duration: %s seconds', $count, round($end, 0)));
51
    }
52
53
    protected function log($message)
54
    {
55
        $message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
56
        echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
57
    }
58
}
59