Completed
Push — master ( c2cfef...1a5a57 )
by Angus
02:24
created

Tracker_Stats_Model   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 4.17%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 3
cts 72
cp 0.0417
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 89 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 119
	public function __construct() {
5 119
		parent::__construct();
6 119
	}
7
8
	public function get() : array {
9
		if(!($stats = $this->cache->get('site_stats'))) {
10
			$stats = array();
11
12
			//CHECK: Is it possible to merge some of these queries?
13
			$queryUsers = $this->db
14
				->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...
15
					'COUNT(*) AS total_users',
16
					'SUM(CASE WHEN api_key IS NOT NULL THEN 1 ELSE 0 END) AS validated_users',
17
					'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'
18
				], FALSE)
19
				->from('auth_users')
20
				->get();
21
			$stats = array_merge($stats, $queryUsers->result_array()[0]);
22
23
			$queryCounts = $this->db
24
				->select([
0 ignored issues
show
Documentation introduced by
array('tracker_titles.ti..., 'tracker_sites.site') 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...
25
					'tracker_titles.title',
26
					'COUNT(tracker_chapters.title_id) AS count',
27
					'tracker_sites.site'
28
				], FALSE)
29
				->from('tracker_chapters')
30
				->join('tracker_titles', 'tracker_titles.id = tracker_chapters.title_id', 'left')
31
				->join('tracker_sites','tracker_titles.site_id = tracker_sites.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_site']  = $queryCounts->result_array()[0]['site'] ?? 'N/A';
39
			$stats['top_title_count'] = $queryCounts->result_array()[0]['count'] ?? 'N/A';
40
41
			$queryTitles = $this->db
42
				->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...
43
					'COUNT(DISTINCT tracker_titles.id) AS total_titles',
44
					'COUNT(DISTINCT tracker_titles.site_id) AS total_sites',
45
					'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',
46
					'SUM(CASE WHEN (tracker_titles.last_updated > DATE_SUB(NOW(), INTERVAL 24 HOUR)) THEN 1 ELSE 0 END) AS updated_titles'
47
				], FALSE)
48
				->from('tracker_titles')
49
				->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
50
				->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
51
				->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
52
				->get();
53
			$stats = array_merge($stats, $queryTitles->result_array()[0]);
54
55
			$querySites = $this->db
56
				->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...
57
					'tracker_sites.site',
58
					'COUNT(*) AS count'
59
				], FALSE)
60
				->from('tracker_titles')
61
				->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
62
				->group_by('tracker_titles.site_id')
63
				->order_by('count DESC')
64
				->limit(3)
65
				->get();
66
			$querySitesResult = $querySites->result_array();
67
			$stats['rank1_site']       = $querySitesResult[0]['site'];
68
			$stats['rank1_site_count'] = $querySitesResult[0]['count'];
69
			$stats['rank2_site']       = $querySitesResult[1]['site'];
70
			$stats['rank2_site_count'] = $querySitesResult[1]['count'];
71
			$stats['rank3_site']       = $querySitesResult[2]['site'];
72
			$stats['rank3_site_count'] = $querySitesResult[2]['count'];
73
74
			$queryTitlesU = $this->db
75
				->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...
76
					'COUNT(*) AS title_updated_count'
77
				], FALSE)
78
				->from('tracker_titles_history')
79
				->get();
80
			$stats = array_merge($stats, $queryTitlesU->result_array()[0]);
81
82
			$queryUsersU = $this->db
83
				->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...
84
					'COUNT(*) AS user_updated_count'
85
				], FALSE)
86
				->from('tracker_user_history')
87
				->get();
88
			$stats = array_merge($stats, $queryUsersU->result_array()[0]);
89
90
			$stats['live_time'] = timespan(/*2016-09-10T03:17:19*/ 1473477439, time(), 2);
91
92
			$this->cache->save('site_stats', $stats, 3600); //Cache for an hour
93
		}
94
95
		return $stats;
96
	}
97
}
98