Completed
Push — master ( a75335...e903e9 )
by Michael
10s
created

StatsModel::getItems()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 21
cts 21
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 5
nop 1
crap 6
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
	 * Saves the given data.
87
	 *
88
	 * @param   \stdClass  $data  Data object to save.
89
	 *
90
	 * @return  void
91
	 */
92 2
	public function save(\stdClass $data)
93
	{
94 2
		$db = $this->getDb();
95
96
		// Set the modified date of the record
97 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
98
99
		// Check if a row exists for this unique ID and update the existing record if so
100 2
		$recordExists = $db->setQuery(
101 2
			$db->getQuery(true)
102 2
				->select('unique_id')
103 2
				->from('#__jstats')
104 2
				->where('unique_id = :unique_id')
105 2
				->bind('unique_id', $data->unique_id, \PDO::PARAM_STR)
106 2
		)->loadResult();
107
108 2
		if ($recordExists)
109
		{
110 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
111
		}
112
		else
113
		{
114 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...
115
		}
116 2
	}
117
}
118