Tracker_Category_Model   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 11.54%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 3
cts 26
cp 0.1154
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setByID() 0 8 1
A setByIDList() 0 23 5
A getUsed() 0 10 1
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Tracker_Category_Model extends Tracker_Base_Model {
4 96
	public function __construct() {
5 96
		parent::__construct();
6 96
	}
7
8
	public function setByID(int $userID, int $chapterID, string $category) : bool {
9
		$success = $this->db->set(['category' => $category, 'active' => 'Y', 'last_updated' => NULL])
10
		                    ->where('user_id', $userID)
11
		                    ->where('id', $chapterID)
12
		                    ->update('tracker_chapters');
13
14
		return (bool) $success;
15
	}
16
17
	public function setByIDList(array $idList, string $category) : array {
18
		/*
19
		 * 0 = Success
20
		 * 1 = Invalid IDs
21
		 * 2 = Invalid category
22
		 */
23
		$status = ['code' => 0];
24
25
		if(in_array($category, array_keys($this->enabledCategories))) {
26
			foreach($idList as $id) {
27
				if(!(ctype_digit($id) && $this->setByID($this->User->id, (int) $id, $category))) {
28
					$status['code'] = 1;
29
				} else {
30
					//Category update was successful, update history too.
31
					$this->History->userUpdateCategory((int) $id, $category);
32
				}
33
			}
34
		} else {
35
			$status['code'] = 2;
36
		}
37
38
		return $status;
39
	}
40
41
	public function getUsed(int $userID) : array {
42
		$query = $this->db->distinct()
43
		                  ->select('category')
44
		                  ->from('tracker_chapters')
45
		                  ->where('tracker_chapters.active', 'Y')
46
		                  ->where('user_id', $userID)
47
		                  ->get();
48
49
		return array_column($query->result_array(), 'category');
50
	}
51
}
52