Completed
Push — master ( 4eed4e...7b7e84 )
by Michael
01:53
created

StatsModel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.81%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 134
ccs 35
cts 54
cp 0.6481
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getItems() 0 41 6
B getRecentlyUpdatedItems() 0 28 3
B save() 0 25 2
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Models;
10
11
use Joomla\Database\DatabaseDriver;
12
use Joomla\Database\Query\LimitableInterface;
13
use Joomla\Model\{
14
	DatabaseModelInterface, DatabaseModelTrait
15
};
16
17
/**
18
 * Statistics database model
19
 */
20
class StatsModel implements DatabaseModelInterface
21
{
22
	use DatabaseModelTrait;
23
24
	/**
25
	 * Instantiate the model.
26
	 *
27
	 * @param   DatabaseDriver  $db  The database driver.
28
	 */
29
	public function __construct(DatabaseDriver $db)
30
	{
31
		$this->setDb($db);
32
	}
33
34
	/**
35
	 * Loads the statistics data from the database.
36
	 *
37
	 * @param   string  $column  A single column to filter on
38
	 *
39
	 * @return  array  An array containing the response data
40
	 *
41
	 * @throws  \InvalidArgumentException
42
	 */
43 3
	public function getItems(string $column = '') : array
44
	{
45 3
		$db         = $this->getDb();
46 3
		$query      = $db->getQuery(true);
47 3
		$columnList = $db->getTableColumns('#__jstats');
48
49
		// Validate the requested column is actually in the table
50 3
		if ($column !== '')
51
		{
52
			// The column should exist in the table and be part of the API
53 2
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
54
			{
55 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
56
			}
57
58 1
			return $db->setQuery(
59
				$query
60 1
					->select('*')
61 1
					->from($db->quoteName('#__jstats_counter_' . $column))
62 1
			)->loadAssocList();
63
		}
64
65 1
		$return = [];
66
67 1
		foreach (array_keys($columnList) as $column)
68
		{
69
			// The column should exist in the table and be part of the API
70 1
			if (in_array($column, ['unique_id', 'modified']))
71
			{
72 1
				continue;
73
			}
74
75 1
			$return[$column] = $db->setQuery(
76 1
				$query->clear()
77 1
					->select('*')
78 1
					->from($db->quoteName('#__jstats_counter_' . $column))
79 1
			)->loadAssocList();
80
		}
81
82 1
		return $return;
83
	}
84
85
	/**
86
	 * Loads the recently updated statistics data from the database.
87
	 *
88
	 * Recently updated is an arbitrary 90 days, submit a pull request for a different behavior.
89
	 *
90
	 * @return  array  An array containing the response data
91
	 */
92
	public function getRecentlyUpdatedItems() : array
93
	{
94
		$db         = $this->getDb();
95
		$columnList = $db->getTableColumns('#__jstats');
96
97
		$return = [];
98
99
		foreach (array_keys($columnList) as $column)
100
		{
101
			// The column should exist in the table and be part of the API
102
			if (in_array($column, ['unique_id', 'modified']))
103
			{
104
				continue;
105
			}
106
107
			$query = $db->getQuery(true);
108
109
			$return[$column] = $db->setQuery(
110
				$query->select($column)
111
					->select('COUNT(' . $column . ') AS count')
112
					->from($db->quoteName('#__jstats'))
113
					->where('modified BETWEEN DATE_SUB(NOW(), INTERVAL 90 DAY) AND NOW()')
114
					->group($column)
115
			)->loadAssocList();
116
		}
117
118
		return $return;
119
	}
120
121
	/**
122
	 * Saves the given data.
123
	 *
124
	 * @param   \stdClass  $data  Data object to save.
125
	 *
126
	 * @return  void
127
	 */
128 2
	public function save(\stdClass $data)
129
	{
130 2
		$db = $this->getDb();
131
132
		// Set the modified date of the record
133 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
134
135
		// Check if a row exists for this unique ID and update the existing record if so
136 2
		$recordExists = $db->setQuery(
137 2
			$db->getQuery(true)
138 2
				->select('unique_id')
139 2
				->from('#__jstats')
140 2
				->where('unique_id = :unique_id')
141 2
				->bind('unique_id', $data->unique_id, \PDO::PARAM_STR)
142 2
		)->loadResult();
143
144 2
		if ($recordExists)
145
		{
146 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
147
		}
148
		else
149
		{
150 1
			$db->insertObject('#__jstats', $data, ['unique_id']);
0 ignored issues
show
Documentation introduced by
array('unique_id') is of type array<integer,string,{"0":"string"}>, but the function expects a string|null.

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...
151
		}
152 2
	}
153
}
154