Kohana_Jam_Countcache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 6
dl 0
loc 31
ccs 10
cts 16
cp 0.625
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A update_counters() 0 18 3
A increment() 0 4 1
A decrement() 0 4 1
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