Kohana_Jam_Countcache::decrement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Handles count cache counting for hasmany
4
 *
5
 * @package    Jam
6
 * @category   Associations
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
class Kohana_Jam_Countcache {
12
13 3
	public static function update_counters($model, $ids, array $counters)
14
	{
15 3
		$query = Jam::update($model);
16
17 3
		foreach ($counters as $name => $change)
18
		{
19 3
			$change = (int) $change;
20 3
			$operator = $change < 0 ? '-' : '+';
21
22 3
			$query->value($name, DB::expr(strtr('(COALESCE(:name, 0) :operator :value)', array(
23 3
				':value' => abs($change),
24 3
				':operator' => $operator,
25 3
				':name' => Database::instance($query->meta()->db())->quote_column($name),
0 ignored issues
show
Bug introduced by
It seems like $query->meta()->db() targeting Kohana_Jam_Meta::db() can also be of type object<Jam_Meta>; however, Kohana_Database::instance() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
			))));
27
		}
28
29 3
		return $query->where(':primary_key', 'IN', (array) $ids);
30
	}
31
32
	public static function increment($model, $counter, $id)
33
	{
34
		Jam_Countcache::update_counters($model, $id, array($counter => +1))->execute();
35
	}
36
37
	public static function decrement($model, $counter, $id)
38
	{
39
		Jam_Countcache::update_counters($model, $id, array($counter => -1))->execute();
40
	}
41
}
42