Status_model   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 165
Duplicated Lines 10.91 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 18
loc 165
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A view() 0 7 1
A auto() 0 9 1
A auto_update_ok() 9 9 1
A auto_update_bad() 9 9 1
A auto_update_timestamp() 0 9 1
A log() 0 9 1
B percentage() 0 30 2
A reset() 0 8 1
A edit_get() 0 8 1
A edit_update() 0 21 2
A create() 0 15 1
A delete() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Status_model extends CI_Model {
4
    
5
    public function __construct() {
6
	parent::__construct();		
7
    }
8
    
9
    public function view() {
10
            
11
        $this->db->order_by("category", "asc");
12
        $this->db->order_by("name", "asc");
13
        $query = $this->db->get('status');
14
        return $query->result_array();
15
    }
16
    
17
    public function auto() {
18
        
19
        $data = array(
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
            'id', 'name', 'status', 'url', 'auto_status'
21
	);
22
            
23
        $query = $this->db->get('status');
24
        return $query->result_array();
25
    }
26
    
27 View Code Duplication
    public function auto_update_ok($service_id, $service_status) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
28
            
29
        $data = array(
30
            'auto_status' => $service_status,
31
        );
32
            
33
        $this->db->where('id', $service_id);
34
        return $this->db->update('status', $data);
35
    }
36
    
37 View Code Duplication
    public function auto_update_bad($service_id, $service_status) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
            
39
        $data = array(
40
            'auto_status' => $service_status,
41
        );
42
            
43
        $this->db->where('id', $service_id);
44
        return $this->db->update('status', $data);
45
    }
46
    
47
    public function auto_update_timestamp($service_id) {
48
            
49
        $data = array(
50
            'status_since' => date("Y-m-d H:i:s", time()),
51
        );
52
            
53
        $this->db->where('id', $service_id);
54
        return $this->db->update('status', $data);
55
    }
56
    
57
    public function log($service_id, $service_status) {
58
        
59
        $data = array(
60
            'sid' => $service_id,
61
            'auto_status' => $service_status
62
	);	
63
        
64
	return $this->db->insert('status_logs', $data);
65
    }
66
    
67
    public function percentage($sid) {
68
        
69
        $this->db->select('sid');
70
        $this->db->where('sid', $sid);
71
        $this->db->order_by('id', 'DESC');
72
        $this->db->limit('5000');
73
	$this->db->from('status_logs');
74
        $total = $this->db->get()->num_rows();
75
        
76
        $this->db->select('sid');
77
        $this->db->where('sid', $sid);
78
        $this->db->where('auto_status', 1);
79
        $this->db->order_by('id', 'DESC');
80
        $this->db->limit('5000');
81
	$this->db->from('status_logs');
82
        $up = $this->db->get()->num_rows();
83
        
84
        if (empty($total)) { 
85
            
86
            $percentage = 'No Data';
87
            return $percentage;
88
            
89
        } else {
90
        
91
            $percentage = round($up / $total * 100, 1).'%';
92
            return $percentage;
93
        
94
        }
95
        
96
    }
97
    
98
    public function reset($sid) {
99
            
100
        $this->db->select('sid');
101
        $this->db->where('sid', $sid);
102
	$this->db->from('status_logs');
103
        $this->db->delete();
104
        
105
    }
106
    
107
    public function edit_get($sid) {
108
        
109
        $this->db->select();
110
        $this->db->where('id', $sid);
111
	$this->db->from('status');
112
        $query = $this->db->get();
113
        return $query->result_array();
114
    }
115
        
116
    public function edit_update($id, $name, $icon, $url, $comment, $category, $status, $username) {
117
        
118
        if (empty($comment)) {
119
            $comment = 'No information available';
120
        }
121
        
122
        $data = array(
123
            'name'   => $name,
124
            'icon'   => $icon,
125
            'url'   => $url,
126
            'comments'   => $comment,
127
            'category'   => $category,
128
            'status'   => $status,
129
            'auto_status'   => $status,
130
            'last_update'   => date("Y-m-d H:i:s", time()),
131
            'by'   => $username,
132
	);
133
        
134
        $this->db->where('id', $id);
135
	return $this->db->update('status', $data);
136
    }
137
138
    public function create($name, $icon, $url, $category, $status) {
139
        
140
        $data = array(
141
            'name'   => $name,
142
            'icon'   => $icon,
143
            'url'   => $url,
144
            'category'   => $category,
145
            'status'   => $status,
146
            'auto_status'   => $status,
147
            'last_update'   => date("Y-m-d H:i:s", time()),
148
            'by'   => $username,
0 ignored issues
show
Bug introduced by
The variable $username does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
149
	);
150
        
151
	return $this->db->insert('status', $data);
152
    }
153
    
154
    public function delete($sid) {
155
            
156
        $this->db->select('sid');
157
        $this->db->where('sid', $sid);
158
	$this->db->from('status_logs');
159
        $this->db->delete();
160
        
161
        $this->db->select('id');
162
        $this->db->where('id', $sid);
163
	$this->db->from('status');
164
        $this->db->delete();
165
        
166
    }
167
}