Completed
Push — master ( 45893c...373238 )
by Angus
02:39
created

Tracker_Stats_Model   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B get() 0 86 2
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Tracker_Stats_Model extends Tracker_Base_Model {
4 112
	public function __construct() {
5 112
		parent::__construct();
6 112
	}
7
8
9
10
	public function get() : array {
11
		if(!($stats = $this->cache->get('site_stats'))) {
12
			$stats = array();
13
14
			//CHECK: Is it possible to merge some of these queries?
15
			$queryUsers = $this->db
16
				->select([
0 ignored issues
show
Documentation introduced by
array('COUNT(*) AS total... END) AS active_users') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17
					'COUNT(*) AS total_users',
18
					'SUM(CASE WHEN api_key IS NOT NULL THEN 1 ELSE 0 END) AS validated_users',
19
					'SUM(CASE WHEN (api_key IS NOT NULL AND from_unixtime(last_login) > DATE_SUB(NOW(), INTERVAL 7 DAY)) THEN 1 ELSE 0 END) AS active_users'
20
				], FALSE)
21
				->from('auth_users')
22
				->get();
23
			$stats = array_merge($stats, $queryUsers->result_array()[0]);
24
25
			$queryCounts = $this->db
26
				->select([
0 ignored issues
show
Documentation introduced by
array('tracker_titles.ti...rs.title_id) AS count') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
					'tracker_titles.title',
28
					'COUNT(tracker_chapters.title_id) AS count'
29
				], FALSE)
30
				->from('tracker_chapters')
31
				->join('tracker_titles', 'tracker_titles.id = tracker_chapters.title_id', 'left')
32
				->group_by('tracker_chapters.title_id')
33
				->having('count > 1')
34
				->order_by('count DESC')
35
				->get();
36
			$stats['titles_tracked_more'] = count($queryCounts->result_array());
37
			$stats['top_title_name']  = $queryCounts->result_array()[0]['title'] ?? 'N/A';
38
			$stats['top_title_count'] = $queryCounts->result_array()[0]['count'] ?? 'N/A';
39
40
			$queryTitles = $this->db
41
				->select([
0 ignored issues
show
Documentation introduced by
array('COUNT(DISTINCT tr...ND) AS updated_titles') is of type array<integer,string,{"0..."string","3":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
					'COUNT(DISTINCT tracker_titles.id) AS total_titles',
43
					'COUNT(DISTINCT tracker_titles.site_id) AS total_sites',
44
					'SUM(CASE WHEN from_unixtime(auth_users.last_login) > DATE_SUB(NOW(), INTERVAL 120 HOUR) IS NOT NULL THEN 0 ELSE 1 END) AS inactive_titles',
45
					'SUM(CASE WHEN (tracker_titles.last_updated > DATE_SUB(NOW(), INTERVAL 24 HOUR)) THEN 1 ELSE 0 END) AS updated_titles'
46
				], FALSE)
47
				->from('tracker_titles')
48
				->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
49
				->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
50
				->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
51
				->get();
52
			$stats = array_merge($stats, $queryTitles->result_array()[0]);
53
54
			$querySites = $this->db
55
				->select([
0 ignored issues
show
Documentation introduced by
array('tracker_sites.site', 'COUNT(*) AS count') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
					'tracker_sites.site',
57
					'COUNT(*) AS count'
58
				], FALSE)
59
				->from('tracker_titles')
60
				->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
61
				->group_by('tracker_titles.site_id')
62
				->order_by('count DESC')
63
				->limit(3)
64
				->get();
65
			$querySitesResult = $querySites->result_array();
66
			$stats['rank1_site']       = $querySitesResult[0]['site'];
67
			$stats['rank1_site_count'] = $querySitesResult[0]['count'];
68
			$stats['rank2_site']       = $querySitesResult[1]['site'];
69
			$stats['rank2_site_count'] = $querySitesResult[1]['count'];
70
			$stats['rank3_site']       = $querySitesResult[2]['site'];
71
			$stats['rank3_site_count'] = $querySitesResult[2]['count'];
72
73
			$queryTitlesU = $this->db
74
				->select([
0 ignored issues
show
Documentation introduced by
array('COUNT(*) AS title_updated_count') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
					'COUNT(*) AS title_updated_count'
76
				], FALSE)
77
				->from('tracker_titles_history')
78
				->get();
79
			$stats = array_merge($stats, $queryTitlesU->result_array()[0]);
80
81
			$queryUsersU = $this->db
82
				->select([
0 ignored issues
show
Documentation introduced by
array('COUNT(*) AS user_updated_count') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
					'COUNT(*) AS user_updated_count'
84
				], FALSE)
85
				->from('tracker_user_history')
86
				->get();
87
			$stats = array_merge($stats, $queryUsersU->result_array()[0]);
88
89
			$stats['live_time'] = timespan(/*2016-09-10T03:17:19*/ 1473477439, time(), 2);
90
91
			$this->cache->save('site_stats', $stats, 3600); //Cache for an hour
92
		}
93
94
		return $stats;
95
	}
96
}
97