Completed
Push — mysql_improvements ( 7b45c6...94ce4a )
by George
02:24
created

StatsModel::getItems()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.048

Importance

Changes 14
Bugs 1 Features 1
Metric Value
c 14
b 1
f 1
dl 0
loc 42
ccs 16
cts 26
cp 0.6153
rs 8.439
cc 6
eloc 22
nc 5
nop 1
crap 8.048
1
<?php
2
3
namespace Stats\Models;
4
5
use Joomla\Database\Query\LimitableInterface;
6
use Joomla\Model\AbstractDatabaseModel;
7
8
/**
9
 * Statistics database model
10
 *
11
 * @since  1.0
12
 */
13
class StatsModel extends AbstractDatabaseModel
14
{
15
	/**
16
	 * Loads the statistics data from the database.
17
	 *
18
	 * @param   string  $column  A single column to filter on
19
	 *
20
	 * @return  \array[]  Array of data arrays.
21
	 *
22
	 * @since   1.0
23
	 * @throws  \InvalidArgumentException
24
	 */
25 3
	public function getItems($column = null)
26
	{
27 3
		$db = $this->getDb();
28
29
		// Validate the requested column is actually in the table
30 3
		if ($column !== null)
31 3
		{
32 2
			$columnList = $db->getTableColumns('#__jstats');
33
34
			// The column should exist in the table and be part of the API
35 2
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
36 2
			{
37 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
38
			}
39
40 1
			return $db->setQuery(
41 1
				$db->getQuery(true)
42 1
					->select('*')
43 1
					->from('#__jstats_counter_' . $column)
44 1
			)->loadAssocList();
45
		}
46
47 1
		$columnList = $db->getTableColumns('#__jstats');
48 1
		$return     = [];
49
50 1
		foreach (array_keys($columnList) as $column)
51
		{
52
			// The column should exist in the table and be part of the API
53
			if (in_array($column, ['unique_id', 'modified']))
54
			{
55
				continue;
56
			}
57
58
			$return[$column] = $db->setQuery(
59
				$db->getQuery(true)
60
					->select('*')
61
					->from('#__jstats_counter_' . $column)
62
			)->loadAssocList();
63
		}
64
65
		return $return;
66
	}
67
68
	/**
69
	 * Saves the given data.
70
	 *
71
	 * @param   \stdClass  $data  Data object to save.
72
	 *
73
	 * @return  void
74
	 *
75
	 * @since   1.0
76
	 */
77 2
	public function save($data)
78
	{
79 2
		$db = $this->getDb();
80
81
		// Set the modified date of the record
82 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
83
84
		// Check if a row exists for this unique ID and update the existing record if so
85 2
		$recordExists = $db->setQuery(
86 2
			$db->getQuery(true)
87 2
				->select('unique_id')
88 2
				->from('#__jstats')
89 2
				->where('unique_id = ' . $db->quote($data->unique_id))
90 2
		)->loadResult();
91
92
		if ($recordExists)
93 2
		{
94 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
95 1
		}
96
		else
97
		{
98 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...
99
		}
100 2
	}
101
}
102