Completed
Push — master ( 8f71bf...ff33fd )
by Angus
04:36
created

History_Model::userGetHistory()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 76
Code Lines 58

Duplication

Lines 42
Ratio 55.26 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 58
nc 2
nop 1
dl 42
loc 76
ccs 0
cts 0
cp 0
crap 132
rs 5.4429
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 declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class History_Model extends CI_Model {
4 103
	public function __construct() {
5 103
		parent::__construct();
6
7 103
		$this->load->database();
8 103
	}
9
10
	/*** TITLE HISTORY ***/
11
	public function updateTitleHistory(int $titleID, $oldChapter, string $newChapter, string $newChapterTimestamp) {
12
		$success = TRUE;
13
		if($oldChapter !== $newChapter) {
14
			$success = $this->db->insert('tracker_titles_history', [
15
				'title_id'    => $titleID,
16
17
				'old_chapter' => $oldChapter,
18
				'new_chapter' => $newChapter,
19
20
				'updated_at'  => $newChapterTimestamp
21
			]);
22
			$this->db->cache_delete('history', (string) $titleID);
23
		}
24
		return (bool) $success;
25
	}
26
27
	public function getTitleHistory(int $titleID, int $page = 1) : array {
28
		$rowsPerPage = 50;
29
		$this->db->cache_on();
30
		$query = $this->db
31
			->select('SQL_CALC_FOUND_ROWS
32
			          tt.title_url,
33
			          ts.site_class,
34
			          tth.updated_at, tth.new_chapter', FALSE)
35
			->from('tracker_titles_history AS tth')
36
			->join('tracker_titles AS tt', 'tth.title_id = tt.id', 'left')
37
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
38
			->where('tt.id', $titleID)
39
			->order_by('tth.id DESC')
40
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
41
			->get();
42
		$this->db->cache_off();
43
44
		$arr = ['rows' => [], 'totalPages' => 1];
45
		if($query->num_rows() > 0) {
46
			foreach($query->result() as $row) {
47
				$arrRow = [];
48
49
				$arrRow['updated_at']  = $row->updated_at;
50
51
				$newChapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->new_chapter);
52
				$arrRow['new_chapter']      = "<a href=\"{$newChapterData['url']}\">{$newChapterData['number']}</a>";
53
				$arrRow['new_chapter_full'] = $row->new_chapter;
54
55
				$arr['rows'][] = $arrRow;
56
			}
57
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
58
		}
59
		return $arr;
60
	}
61
62
	/*** USER HISTORY ***/
63
	/*
64
	 * --User history types--
65
	 * 1: Title added
66
	 * 2: Title updated
67
	 * 3: Title removed
68
	 * 4: Tags updated
69
	 * 5: Category updated
70
	 * 6: Favourite added
71
	 * 7: Favourite removed
72
	 * 8: Title ignored
73
	 */
74
75 View Code Duplication
	public function userAddTitle(int $chapterID, string $chapter, string $category) : bool {
76
		$success = $this->db->insert('tracker_user_history', [
77
			'chapter_id'  => $chapterID,
78
79
			'type'        => '1',
80
			'custom1'     => $chapter,
81
			'custom2'     => $category,
82
83
			'updated_at'  => date('Y-m-d H:i:s')
84
		]);
85
86
		return $success;
87
	}
88 View Code Duplication
	public function userUpdateTitle(int $chapterID, string $new_chapter) : bool {
89
		$success = $this->db->insert('tracker_user_history', [
90
			'chapter_id'  => $chapterID,
91
92
			'type'        => '2',
93
			'custom1'     => $new_chapter,
94
95
			'updated_at'  => date('Y-m-d H:i:s')
96
		]);
97
98
		return $success;
99
	}
100
	public function userRemoveTitle(int $chapterID) : bool {
101
		$success = $this->db->insert('tracker_user_history', [
102
			'chapter_id'  => $chapterID,
103
104
			'type'        => '3',
105
106
			'updated_at'  => date('Y-m-d H:i:s')
107
		]);
108
109
		return $success;
110
	}
111 View Code Duplication
	public function userUpdateTags(int $chapterID, string $new_tags) : bool {
112
		$success = $this->db->insert('tracker_user_history', [
113
			'chapter_id'  => $chapterID,
114
115
			'type'        => '4',
116
			'custom1'     => $new_tags,
117
118
			'updated_at'  => date('Y-m-d H:i:s')
119
		]);
120
121
		return $success;
122
	}
123 View Code Duplication
	public function userUpdateCategory(int $chapterID, string $new_category) : bool {
124
		$success = $this->db->insert('tracker_user_history', [
125
			'chapter_id'  => $chapterID,
126
127
			'type'        => '5',
128
			'custom1'     => $new_category,
129
130
			'updated_at'  => date('Y-m-d H:i:s')
131
		]);
132
133
		return $success;
134
	}
135 View Code Duplication
	public function userAddFavourite(int $chapterID, string $chapter) : bool {
136
		$success = $this->db->insert('tracker_user_history', [
137
			'chapter_id'  => $chapterID,
138
139
			'type'        => '6',
140
			'custom1'     => $chapter,
141
142
			'updated_at'  => date('Y-m-d H:i:s')
143
		]);
144
145
		return $success;
146
	}
147 View Code Duplication
	public function userRemoveFavourite(int $chapterID, string $chapter) : bool {
148
		$success = $this->db->insert('tracker_user_history', [
149
			'chapter_id'  => $chapterID,
150
151
			'type'        => '7',
152
			'custom1'     => $chapter,
153
154
			'updated_at'  => date('Y-m-d H:i:s')
155
		]);
156
157
		return $success;
158
	}
159 View Code Duplication
	public function userIgnoreTitle(int $chapterID, string $new_chapter) : bool {
160
		$success = $this->db->insert('tracker_user_history', [
161
			'chapter_id'  => $chapterID,
162
163
			'type'        => '8',
164
			'custom1'     => $new_chapter,
165
166
			'updated_at'  => date('Y-m-d H:i:s')
167
		]);
168
169
		return $success;
170
	}
171
172
	public function userGetHistory(int $page) : array {
173
		$rowsPerPage = 50;
174
		$query = $this->db
175
			->select('SQL_CALC_FOUND_ROWS
176
			          tt.title, tt.title_url,
177
			          ts.site, ts.site_class,
178
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
179
			->from('tracker_user_history AS tuh')
180
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
181
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
182
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
183
			->where('tc.user_id', $this->User->id)
184
			->order_by('tuh.id DESC')
185
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
186
			->get();
187
188
		$arr = ['rows' => [], 'totalPages' => 1];
189
		if($query->num_rows() > 0) {
190
			foreach($query->result() as $row) {
191
				$arrRow = [];
192
193
				$arrRow['updated_at'] = $row->updated_at;
194
				$arrRow['title']      = $row->title;
195
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
196
197
				$arrRow['site'] = $row->site;
198
				$arrRow['site_sprite'] = str_replace('.', '-', $row->site);
199
200 View Code Duplication
				switch($row->type) {
201
					case 1:
202
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
203
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
204
						break;
205
206
					case 2:
207
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
208
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
209
						break;
210
211
					case 3:
212
						$arrRow['status'] = "Series removed";
213
						break;
214
215
					case 4:
216
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
217
						break;
218
219
					case 5:
220
						$arrRow['status'] = "Category set to '{$row->custom1}'";
221
						break;
222
223
					case 6:
224
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
225
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
226
						break;
227
228
					case 7:
229
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
230
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
231
						break;
232
233
					case 8:
234
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
235
						$arrRow['status'] = "Chapter ignored: '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
236
						break;
237
238
					default:
239
						$arrRow['status'] = "Something went wrong!";
240
						break;
241
				}
242
				$arr['rows'][] = $arrRow;
243
			}
244
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
245
		}
246
		return $arr;
247
	}
248
249
	public function userGetHistoryAll() : array {
250
		$rowsPerPage = 50;
0 ignored issues
show
Unused Code introduced by
$rowsPerPage 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...
251
		$query = $this->db
252
			->select('SQL_CALC_FOUND_ROWS
253
			          tt.title, tt.title_url,
254
			          ts.site, ts.site_class,
255
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
256
			->from('tracker_user_history AS tuh')
257
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
258
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
259
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
260
			->where('tc.user_id', $this->User->id)
261
			->order_by('tuh.id DESC')
262
			->get();
263
264
		$arr = [];
265
		if($query->num_rows() > 0) {
266
			foreach($query->result() as $row) {
267
				$arrRow = [];
268
269
				$arrRow['updated_at'] = $row->updated_at;
270
				$arrRow['title']      = $row->title;
271
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
272
273
				$arrRow['site'] = $row->site;
274
275 View Code Duplication
				switch($row->type) {
276
					case 1:
277
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
278
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
279
						break;
280
281
					case 2:
282
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
283
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
284
						break;
285
286
					case 3:
287
						$arrRow['status'] = "Series removed";
288
						break;
289
290
					case 4:
291
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
292
						break;
293
294
					case 5:
295
						$arrRow['status'] = "Category set to '{$row->custom1}'";
296
						break;
297
298
					case 6:
299
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
300
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
301
						break;
302
303
					case 7:
304
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
305
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
306
						break;
307
308
					case 8:
309
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
310
						$arrRow['status'] = "Chapter ignored: '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
311
						break;
312
313
					default:
314
						$arrRow['status'] = "Something went wrong!";
315
						break;
316
				}
317
				$arr[] = $arrRow;
318
			}
319
		}
320
		return $arr;
321
	}
322
}
323