Completed
Push — master ( ea880d...ae1663 )
by Michael
02:21
created

StatsModel::getItems()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 66
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 6.0192

Importance

Changes 11
Bugs 0 Features 1
Metric Value
c 11
b 0
f 1
dl 0
loc 66
ccs 34
cts 37
cp 0.9189
rs 8.6045
cc 6
eloc 34
nc 5
nop 1
crap 6.0192

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	 * The query batch size
17
	 *
18
	 * @var    integer
19
	 * @since  1.0
20
	 */
21
	private $batchSize = 25000;
22
23
	/**
24
	 * Loads the statistics data from the database.
25
	 *
26
	 * @param   string  $column  A single column to filter on
27
	 *
28
	 * @return  \array[]  Array of data arrays.
29
	 *
30
	 * @since   1.0
31
	 * @throws  \InvalidArgumentException
32
	 */
33 3
	public function getItems($column = null)
34
	{
35 3
		$db = $this->getDb();
36
37
		// Validate the requested column is actually in the table
38 3
		if ($column !== null)
39 3
		{
40 2
			$columnList = $db->getTableColumns('#__jstats');
41
42
			// The column should exist in the table and be part of the API
43 2
			if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified']))
44 2
			{
45 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
46
			}
47
48 1
			return $db->setQuery(
49 1
				$db->getQuery(true)
50 1
					->select($column)
51 1
					->from('#__jstats')
52 1
					->group('unique_id')
53 1
			)->loadAssocList();
54
		}
55
56
		// If fetching all data from the table, we need to break this down a fair bit otherwise we're going to run out of memory
57 1
		$totalRecords = $db->setQuery(
58 1
			$db->getQuery(true)
59 1
				->select('COUNT(unique_id)')
60 1
				->from('#__jstats')
61 1
		)->loadResult();
62
63 1
		$return = [];
64
65 1
		$query = $db->getQuery(true)
66 1
			->select(['php_version', 'db_type', 'db_version', 'cms_version', 'server_os'])
67 1
			->from('#__jstats')
68 1
			->group('unique_id');
69
70 1
		$limitable = $query instanceof LimitableInterface;
71
72
		// We can't have this as a single array, we run out of memory... This is gonna get interesting...
73 1
		for ($offset = 0; $offset < $totalRecords; $offset + $this->batchSize)
74
		{
75
			if ($limitable)
76 1
			{
77
				$query->setLimit($this->batchSize, $offset);
78
79
				$db->setQuery($query);
80
			}
81
			else
82
			{
83 1
				$db->setQuery($query, $offset, $this->batchSize);
84
			}
85
86 1
			$return[] = $db->loadAssocList();
87
88 1
			$offset += $this->batchSize;
89 1
		}
90
91
		// Disconnect the DB to free some memory
92 1
		$db->disconnect();
93
94
		// And unset some variables
95 1
		unset($db, $query, $offset, $totalRecords);
96
97 1
		return $return;
98
	}
99
100
	/**
101
	 * Saves the given data.
102
	 *
103
	 * @param   \stdClass  $data  Data object to save.
104
	 *
105
	 * @return  void
106
	 *
107
	 * @since   1.0
108
	 */
109 2
	public function save($data)
110
	{
111 2
		$db = $this->getDb();
112
113
		// Set the modified date of the record
114 2
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat());
115
116
		// Check if a row exists for this unique ID and update the existing record if so
117 2
		$recordExists = $db->setQuery(
118 2
			$db->getQuery(true)
119 2
				->select('unique_id')
120 2
				->from('#__jstats')
121 2
				->where('unique_id = ' . $db->quote($data->unique_id))
122 2
		)->loadResult();
123
124
		if ($recordExists)
125 2
		{
126 1
			$db->updateObject('#__jstats', $data, ['unique_id']);
127 1
		}
128
		else
129
		{
130 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...
131
		}
132 2
	}
133
}
134