Completed
Push — mysql_improvements ( 2023bf...2e95ce )
by Michael
02:48
created

StatsModel::getItems()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6

Importance

Changes 18
Bugs 1 Features 1
Metric Value
c 18
b 1
f 1
dl 0
loc 41
ccs 25
cts 25
cp 1
rs 8.439
cc 6
eloc 22
nc 5
nop 1
crap 6
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  An array containing the response data
21
	 *
22
	 * @since   1.0
23
	 * @throws  \InvalidArgumentException
24
	 */
25 3
	public function getItems($column = null)
26
	{
27 3
		$db         = $this->getDb();
28 3
		$query      = $db->getQuery(true);
29 3
		$columnList = $db->getTableColumns('#__jstats');
30
31
		// Validate the requested column is actually in the table
32 3
		if ($column !== null)
33 3
		{
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
				$query
42 1
					->select('*')
43 1
					->from($db->quoteName('#__jstats_counter_' . $column))
44 1
			)->loadAssocList();
45
		}
46
47 1
		$return = [];
48
49 1
		foreach (array_keys($columnList) as $column)
50
		{
51
			// The column should exist in the table and be part of the API
52 1
			if (in_array($column, ['unique_id', 'modified']))
53 1
			{
54 1
				continue;
55
			}
56
57 1
			$return[$column] = $db->setQuery(
58 1
				$query->clear()
59 1
					->select('*')
60 1
					->from($db->quoteName('#__jstats_counter_' . $column))
61 1
			)->loadAssocList();
62 1
		}
63
64 1
		return $return;
65
	}
66
67
	/**
68
	 * Saves the given data.
69
	 *
70
	 * @param   \stdClass  $data  Data object to save.
71
	 *
72
	 * @return  void
73
	 *
74
	 * @since   1.0
75
	 */
76 2
	public function save($data)
77
	{
78 2
		$db = $this->getDb();
79
80
		// Set the modified date of the record
81 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
82
83
		// Check if a row exists for this unique ID and update the existing record if so
84 2
		$recordExists = $db->setQuery(
85 2
			$db->getQuery(true)
86 2
				->select('unique_id')
87 2
				->from('#__jstats')
88 2
				->where('unique_id = ' . $db->quote($data->unique_id))
89 2
		)->loadResult();
90
91
		if ($recordExists)
92 2
		{
93 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
94 1
		}
95
		else
96
		{
97 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...
98
		}
99 2
	}
100
}
101