Completed
Push — master ( 2df1ff...31a2b9 )
by Michael
04:54
created

StatisticsRepository::getRecentlyUpdatedItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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\Repositories;
10
11
use Joomla\Database\DatabaseInterface;
12
use Joomla\Database\ParameterType;
13
14
/**
15
 * Statistics repository
16
 */
17
class StatisticsRepository
18
{
19
	/**
20
	 * Array containing the allowed sources
21
	 *
22
	 * @var  string[]
23
	 */
24
	public const ALLOWED_SOURCES = ['php_version', 'db_type', 'db_version', 'cms_version', 'server_os'];
25
26
	/**
27
	 * The database driver.
28
	 *
29
	 * @var    DatabaseInterface
30
	 * @since  1.3.0
31
	 */
32
	private $db;
33
34
	/**
35
	 * Instantiate the repository.
36
	 *
37
	 * @param   DatabaseInterface  $db  The database driver.
38
	 */
39 14
	public function __construct(DatabaseInterface $db)
40
	{
41 14
		$this->db = $db;
42 14
	}
43
44
	/**
45
	 * Loads the statistics data from the database.
46
	 *
47
	 * @param   string  $column  A single column to filter on
48
	 *
49
	 * @return  array  An array containing the response data
50
	 *
51
	 * @throws  \InvalidArgumentException
52
	 */
53 6
	public function getItems(string $column = ''): array
54
	{
55
		// Validate the requested column is actually in the table
56 6
		if ($column !== '')
57
		{
58
			// The column should exist in the table and be part of the API
59 3
			if (!\in_array($column, self::ALLOWED_SOURCES))
60
			{
61 1
				throw new \InvalidArgumentException('An invalid data source was requested.', 404);
62
			}
63
64 2
			return $this->db->setQuery(
65 2
				$this->db->getQuery(true)
66 2
					->select('*')
67 2
					->from($this->db->quoteName('#__jstats_counter_' . $column))
68 2
			)->loadAssocList();
69
		}
70
71 3
		$return = [];
72
73 3
		foreach (self::ALLOWED_SOURCES as $column)
74
		{
75 3
			$return[$column] = $this->db->setQuery(
76 3
				$this->db->getQuery(true)
77 3
					->select('*')
78 3
					->from($this->db->quoteName('#__jstats_counter_' . $column))
79 3
			)->loadAssocList();
80
		}
81
82 3
		return $return;
83
	}
84
85
	/**
86
	 * Loads the recently updated statistics data from the database.
87
	 *
88
	 * Recently updated is an arbitrary 90 days, submit a pull request for a different behavior.
89
	 *
90
	 * @return  array  An array containing the response data
91
	 */
92 1
	public function getRecentlyUpdatedItems(): array
93
	{
94 1
		$return = [];
95
96 1
		foreach (self::ALLOWED_SOURCES as $column)
97
		{
98 1
			$return[$column] = $this->db->setQuery(
99 1
				$this->db->getQuery(true)
100 1
					->select($column)
101 1
					->select('COUNT(' . $column . ') AS count')
102 1
					->from($this->db->quoteName('#__jstats'))
103 1
					->where('modified BETWEEN DATE_SUB(NOW(), INTERVAL 90 DAY) AND NOW()')
104 1
					->group($column)
105 1
			)->loadAssocList();
106
		}
107
108 1
		return $return;
109
	}
110
111
	/**
112
	 * Saves the given data.
113
	 *
114
	 * @param   \stdClass  $data  Data object to save.
115
	 *
116
	 * @return  void
117
	 */
118 5
	public function save(\stdClass $data): void
119
	{
120
		// Set the modified date of the record
121 5
		$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($this->db->getDateFormat());
122
123
		// Check if a row exists for this unique ID and update the existing record if so
124 5
		$recordExists = $this->db->setQuery(
125 5
			$this->db->getQuery(true)
126 5
				->select('unique_id')
127 5
				->from('#__jstats')
128 5
				->where('unique_id = :unique_id')
129 5
				->bind(':unique_id', $data->unique_id, ParameterType::STRING)
130 5
		)->loadResult();
131
132 5
		if ($recordExists)
133
		{
134 1
			$this->db->updateObject('#__jstats', $data, ['unique_id']);
135
		}
136
		else
137
		{
138 4
			$this->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...
139
		}
140 5
	}
141
}
142