Kohana_Jam_Association::primary_key()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 12
cp 0.9167
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 2
crap 7.0283
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * Core class that all associations must extend
5
 *
6
 * @package    Jam
7
 * @category   Associations
8
 * @author     Ivan Kerin
9
 * @copyright  (c) 2011-2012 Despark Ltd.
10
 * @license    http://www.opensource.org/licenses/isc-license.txt
11
 */
12
abstract class Kohana_Jam_Association extends Jam_Attribute {
13
14
	const NULLIFY  = 'nullify';
15
	const ERASE    = 'erase';
16
	const DELETE   = 'delete';
17
18
	/**
19
	 * Get the primary key from whatever value you have
20
	 *
21
	 * @param  string $model_name The name of the model
22
	 * @param  string|integer|Jam_Validated|array $value the value or a container of the value
23
	 * @return string|integer|NULL NULL when no value is provided or could be extracted.
24
	 */
25 46
	public static function primary_key($model_name, $value)
26
	{
27 46
		if ( ! $value)
28 4
			return NULL;
29
30 42
		if ($value instanceof Jam_Validated)
31 18
			return $value->id();
32
33 26
		if (is_integer($value) OR is_numeric($value))
34 13
			return (int) $value;
35
36 14
		if (is_string($value))
37 5
			return $value;
38
39 9
		if (is_array($value))
40 9
			return Arr::get($value, Jam::meta($model_name)->primary_key());
0 ignored issues
show
Bug introduced by
It seems like \Jam::meta($model_name)->primary_key() targeting Kohana_Jam_Meta::primary_key() can also be of type object<Jam_Meta>; however, Kohana_Arr::get() does only seem to accept string, 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...
41
	}
42
43 17
	public static function is_changed($value)
44
	{
45 17
		if ($value instanceof Jam_Model AND ( ! $value->loaded() OR $value->changed()))
46 1
			return TRUE;
47
48 16
		return is_array($value);
49
	}
50
51
	public $foreign_model = NULL;
52
53
	public $readonly = NULL;
54
55
	/**
56
	 * If set to true, will delete the association object when this one gets deleted
57
	 * possible values are Jam_Association::DELETE and Jam_Association::ERASE and Jam_Association::NULLIFY
58
	 * Jam_Association::DELETE will run the delete event of the associated model, Jam_Association::ERASE will not
59
	 *
60
	 * @var boolean|string
61
	 */
62
	public $dependent = FALSE;
63
64
	/**
65
	 * See if the association is polymorphic.
66
	 * This is overloaded in the associations themselves
67
	 *
68
	 * @return boolean
69
	 */
70 5
	public function is_polymorphic()
71
	{
72 5
		return FALSE;
73
	}
74
75
	abstract public function join($table, $type = NULL);
76
77
	public function set(Jam_Validated $model, $value, $is_changed)
78
	{
79
		return $value;
80
	}
81
}
82