Completed
Push — master ( 17a370...d70e31 )
by Michael
02:18
created

StatsModel::getItems()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

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