Completed
Push — master ( 4e5c99...220c92 )
by Michael
05:11
created

StatsModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Stats\Models;
4
5
use Joomla\Database\DatabaseDriver;
6
use Joomla\Database\Query\LimitableInterface;
7
use Joomla\Model\DatabaseModelInterface;
8
use Joomla\Model\DatabaseModelTrait;
9
10
/**
11
 * Statistics database model
12
 *
13
 * @since  1.0
14
 */
15
class StatsModel implements DatabaseModelInterface
16
{
17
	use DatabaseModelTrait;
18
19
	/**
20
	 * The query batch size
21
	 *
22
	 * @var    integer
23
	 * @since  1.0
24
	 */
25
	private $batchSize = 25000;
26
27
	/**
28
	 * Instantiate the model.
29
	 *
30
	 * @param   DatabaseDriver  $db  The database driver.
31
	 *
32
	 * @since   1.0
33
	 */
34
	public function __construct(DatabaseDriver $db)
35
	{
36
		$this->setDb($db);
37
	}
38
39
	/**
40
	 * Loads the statistics data from the database.
41
	 *
42
	 * @param   string  $column  A single column to filter on
43
	 *
44
	 * @return  \Generator  A Generator containing the response data
45
	 *
46
	 * @since   1.0
47
	 * @throws  \InvalidArgumentException
48
	 */
49 3
	public function getItems(string $column = '') : \Generator
50
	{
51 3
		$db = $this->getDb();
52
53
		// To keep from running out of memory, we need to know how many records are in the database to be able to loop correctly
54 3
		$totalRecords = $db->setQuery(
55 3
			$db->getQuery(true)
56 3
				->select('COUNT(unique_id)')
57 3
				->from('#__jstats')
58 3
		)->loadResult();
59
60
		// Validate the requested column is actually in the table
61 3
		if ($column !== '')
62
		{
63 2
			$columnList = $db->getTableColumns('#__jstats');
64
65
			// The column should exist in the table and be part of the API
66 2
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
67
			{
68 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
69
			}
70
71 1
			$query = $db->getQuery(true)
72 1
				->select($column);
73
		}
74
		else
75
		{
76 1
			$query = $db->getQuery(true)
77 1
				->select(['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']);
78
		}
79
80 2
		$query->from('#__jstats')
81 2
			->group('unique_id');
82
83 2
		$limitable = $query instanceof LimitableInterface;
84
85
		// We can't have this as a single array, we run out of memory... This is gonna get interesting...
86 2
		for ($offset = 0; $offset < $totalRecords; $offset + $this->batchSize)
87
		{
88 2
			if ($limitable)
89
			{
90
				$query->setLimit($this->batchSize, $offset);
91
92
				$db->setQuery($query);
93
			}
94
			else
95
			{
96 2
				$db->setQuery($query, $offset, $this->batchSize);
97
			}
98
99 2
			yield $db->loadAssocList();
100
101 2
			$offset += $this->batchSize;
102
		}
103
104
		// Disconnect the DB to free some memory
105 2
		$db->disconnect();
106
107
		// And unset some variables
108 2
		unset($db, $query, $offset, $totalRecords);
109 2
	}
110
111
	/**
112
	 * Saves the given data.
113
	 *
114
	 * @param   \stdClass  $data  Data object to save.
115
	 *
116
	 * @return  void
117
	 *
118
	 * @since   1.0
119
	 */
120 2
	public function save(\stdClass $data)
121
	{
122 2
		$db = $this->getDb();
123
124
		// Set the modified date of the record
125 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
126
127
		// Check if a row exists for this unique ID and update the existing record if so
128 2
		$recordExists = $db->setQuery(
129 2
			$db->getQuery(true)
130 2
				->select('unique_id')
131 2
				->from('#__jstats')
132 2
				->where('unique_id = ' . $db->quote($data->unique_id))
133 2
		)->loadResult();
134
135 2
		if ($recordExists)
136
		{
137 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
138
		}
139
		else
140
		{
141 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...
142
		}
143 2
	}
144
}
145