Jobs_model::update_active_ad_users()   D
last analyzed

Complexity

Conditions 34
Paths 132

Size

Total Lines 106
Code Lines 75

Duplication

Lines 55
Ratio 51.89 %

Importance

Changes 0
Metric Value
cc 34
eloc 75
nc 132
nop 0
dl 55
loc 106
rs 4.1167
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
defined('BASEPATH') OR exit('No direct script access allowed');
4
5
class Jobs_model extends CI_Model {
6
7
    public function __construct() {
8
        parent::__construct();
9
    }
10
11
    function update_active_ad_users() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
13
        $ldap_password = $this->config->item('ldapbindpass');
14
        $ldap_username = $this->config->item('ldapshortdomain').$this->config->item('ldapbindun');
15
        $ldap_connection = ldap_connect($this->config->item('ldapserver'));
16
17
        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
18
        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
19
20
        if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)) {
21
            $search_filter = '(&(objectCategory=person)(samaccountname=*))';
22
            $attributes = array();
23
            $attributes[] = 'givenname';
24
            $attributes[] = 'samaccountname';
25
            $attributes[] = 'sn';
26
            $attributes[] = 'mail';
27
            $ad_users = array();
28
29
            //empty database
30
            $this->db->truncate('users_ad');
31
32
            $result_admin = ldap_search($ldap_connection, $this->config->item('ldapuserjobouone'), $search_filter, $attributes);
33 View Code Duplication
            if (FALSE !== $result_admin) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
                $entries = ldap_get_entries($ldap_connection, $result_admin);
35
                for ($x = 0; $x < $entries['count']; $x++) {
36
                    if (!empty($entries[$x]['givenname'][0]) &&
37
                            !empty($entries[$x]['samaccountname'][0]) &&
38
                            !empty($entries[$x]['sn'][0]) &&
39
                            !empty($entries[$x]['mail'][0])) {  
40
                        $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0]));
41
                    }
42
                }
43
            }
44
            $result_canterbury = ldap_search($ldap_connection, $this->config->item('ldapuserjoboutwo'), $search_filter, $attributes);
45 View Code Duplication
            if (FALSE !== $result_canterbury) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
                $entries = ldap_get_entries($ldap_connection, $result_canterbury);
47
                for ($x = 0; $x < $entries['count']; $x++) {
48
                    if (!empty($entries[$x]['givenname'][0]) &&
49
                            !empty($entries[$x]['samaccountname'][0]) &&
50
                            !empty($entries[$x]['sn'][0]) &&
51
                            !empty($entries[$x]['mail'][0])) {
52
                        $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0]));
53
                    }
54
                }
55
            }
56
            $result_sheppy = ldap_search($ldap_connection, $this->config->item('ldapuserjobouthree'), $search_filter, $attributes);
57 View Code Duplication
            if (FALSE !== $result_sheppy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
                $entries = ldap_get_entries($ldap_connection, $result_sheppy);
59
                for ($x = 0; $x < $entries['count']; $x++) {
60
                    if (!empty($entries[$x]['givenname'][0]) &&
61
                            !empty($entries[$x]['samaccountname'][0]) &&
62
                            !empty($entries[$x]['sn'][0]) &&
63
                            !empty($entries[$x]['mail'][0])) {
64
                        $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0]));
65
                    }
66
                }
67
            }
68
            $result_nicks = ldap_search($ldap_connection, $this->config->item('ldapuserjoboufour'), $search_filter, $attributes);
69 View Code Duplication
            if (FALSE !== $result_nicks) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
                $entries = ldap_get_entries($ldap_connection, $result_nicks);
71
                for ($x = 0; $x < $entries['count']; $x++) {
72
                    if (!empty($entries[$x]['givenname'][0]) &&
73
                            !empty($entries[$x]['samaccountname'][0]) &&
74
                            !empty($entries[$x]['sn'][0]) &&
75
                            !empty($entries[$x]['mail'][0])) {
76
                        $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0]));
77
                    }
78
                }
79
            }
80
            $result_nicks = ldap_search($ldap_connection, $this->config->item('ldapuserjoboufive'), $search_filter, $attributes);
81 View Code Duplication
            if (FALSE !== $result_nicks) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                $entries = ldap_get_entries($ldap_connection, $result_nicks);
83
                for ($x = 0; $x < $entries['count']; $x++) {
84
                    if (!empty($entries[$x]['givenname'][0]) &&
85
                            !empty($entries[$x]['samaccountname'][0]) &&
86
                            !empty($entries[$x]['sn'][0]) &&
87
                            !empty($entries[$x]['mail'][0])) {
88
                        $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0]));
89
                    }
90
                }
91
            }
92
93
            ldap_unbind($ldap_connection);
94
        }
95
96
        if ($this->db->insert_batch('users_ad', $ad_users)) {
0 ignored issues
show
Bug introduced by
The variable $ad_users does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
            
98
            $ip = $_SERVER['REMOTE_ADDR'];
99
            $data = array(
100
                'timestamp' => date("Y-m-d H:i:s", time()),
101
                'ip' => $ip,
102
                'job' => 'Users updated successfully.',
103
            );
104
105
            return $this->db->insert('job_log', $data);
106
        } else {
107
            
108
            $ip = $_SERVER['REMOTE_ADDR'];
109
            $data = array(
110
                'timestamp' => date("Y-m-d H:i:s", time()),
111
                'ip' => $ip,
112
                'job' => 'Users updated failed.',
113
            );
114
            return $this->db->insert('job_log', $data);
115
        }
116
    }
117
}