Completed
Push — master ( 55ea86...34c5f6 )
by Michael
01:51
created

StatsModel::getItems()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 14
cp 0
rs 8.5806
cc 4
eloc 15
nc 3
nop 1
crap 20
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
	 * The query batch size
26
	 *
27
	 * @var  integer
28
	 */
29
	private $batchSize = 25000;
30
31
	/**
32
	 * Instantiate the model.
33
	 *
34
	 * @param   DatabaseDriver  $db  The database driver.
35
	 */
36
	public function __construct(DatabaseDriver $db)
37
	{
38
		$this->setDb($db);
39
	}
40
41
	/**
42
	 * Loads the statistics data from the database.
43
	 *
44
	 * @param   string  $column  A single column to filter on
45
	 *
46
	 * @return  \Generator  A Generator containing the response data
47
	 *
48
	 * @throws  \InvalidArgumentException
49
	 */
50
	public function getItems(string $column = '') : \Generator
51
	{
52
		$db = $this->getDb();
53
54
		// Validate the requested column is actually in the table
55
		if ($column !== '')
56
		{
57
			$columnList = $db->getTableColumns('#__jstats');
58
59
			// The column should exist in the table and be part of the API
60
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
61
			{
62
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
63
			}
64
65
			$query = $db->getQuery(true)
66
				->select($column);
67
		}
68
		else
69
		{
70
			$query = $db->getQuery(true)
71
				->select(['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']);
72
		}
73
74
		$query->from('#__jstats')
75
			->group('unique_id');
76
77
		$db->setQuery($query);
78
79
		return $db->yieldAssocList();
80
	}
81
82
	/**
83
	 * Saves the given data.
84
	 *
85
	 * @param   \stdClass  $data  Data object to save.
86
	 *
87
	 * @return  void
88
	 */
89 2
	public function save(\stdClass $data)
90
	{
91 2
		$db = $this->getDb();
92
93
		// Set the modified date of the record
94 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
95
96
		// Check if a row exists for this unique ID and update the existing record if so
97 2
		$recordExists = $db->setQuery(
98 2
			$db->getQuery(true)
99 2
				->select('unique_id')
100 2
				->from('#__jstats')
101 2
				->where('unique_id = :unique_id')
102 2
				->bind('unique_id', $data->unique_id, \PDO::PARAM_STR)
103 2
		)->loadResult();
104
105 2
		if ($recordExists)
106
		{
107 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
108
		}
109
		else
110
		{
111 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...
112
		}
113 2
	}
114
}
115