Passed
Pull Request — master (#18)
by Matt
02:29
created

LDAPMemberSyncOneTask::run()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 27
nc 8
nop 1
dl 0
loc 42
rs 9.1768
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\LDAP\Tasks;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\LDAP\Services\LDAPService;
8
use SilverStripe\Security\Permission;
9
10
/**
11
 * Class LDAPMemberSyncOneTask
12
 * @package SilverStripe\LDAP\Tasks
13
 *
14
 * Debug build task that can be used to sync a single member by providing their email address registered in LDAP.
15
 *
16
 * Usage: /dev/tasks/[email protected]
17
 */
18
class LDAPMemberSyncOneTask extends LDAPMemberSyncTask
19
{
20
    /**
21
     * {@inheritDoc}
22
     * @var string
23
     */
24
    private static $segment = 'LDAPMemberSyncOneTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var array
28
     */
29
    private static $dependencies = [
0 ignored issues
show
introduced by
The private property $dependencies is not used, and could be removed.
Loading history...
30
        'ldapService' => '%$' . LDAPService::class,
31
    ];
32
33
    /**
34
     * @return string
35
     */
36
    public function getTitle()
37
    {
38
        return _t(__CLASS__ . '.SYNCONETITLE', 'Sync single user from LDAP');
39
    }
40
41
    /**
42
     * Syncs a single user based on the email address passed in the URL
43
     *
44
     * @param HTTPRequest $request
45
     */
46
    public function run($request)
47
    {
48
        $email = $request->getVar('email');
49
50
        if (!$email) {
51
            die('You must supply an email parameter to this method.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
52
        }
53
54
        $user = $this->ldapService->getUserByEmail($email);
55
56
        if (!$user) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            die(sprintf('No user found in LDAP for email %s', $email));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
58
        }
59
60
        $member = $this->findOrCreateMember($user);
61
62
        // If member exists already, we're updating - otherwise we're creating
63
        if ($member->exists()) {
64
            $this->log(sprintf(
65
                'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)',
66
                $user['objectguid'],
67
                $member->getName(),
68
                $member->ID,
69
                $user['samaccountname']
70
            ));
71
        } else {
72
            $this->log(sprintf(
73
                'Creating new Member %s: "%s" (SAM Account Name: %s)',
74
                $user['objectguid'],
75
                $user['cn'],
76
                $user['samaccountname']
77
            ));
78
        }
79
80
        $this->log('User data returned from LDAP follows:');
81
        $this->log(var_export($user));
0 ignored issues
show
Bug introduced by
Are you sure the usage of var_export($user) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
83
        try {
84
            $this->ldapService->updateMemberFromLDAP($member, $user);
85
            $this->log('Done!');
86
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type SilverStripe\LDAP\Tasks\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
87
            $this->log($e->getMessage());
88
        }
89
    }
90
}
91