Completed
Push — count-subquery-clone ( d5815f...6cc328 )
by Haralan
03:48
created

Kohana_Jam_Query_Builder_Join::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
ccs 2
cts 4
cp 0.5
cc 2
eloc 5
nc 2
nop 0
crap 2.5
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * A class representing one or more joins
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_Query_Builder_Join extends Database_Query_Builder_Join {
13
14
	/**
15
	 * Create object of class Jam_Query_Builder_Join
16
	 * @param  string $model
17
	 * @param  string $type LEFT, RIGHT, NATURAL...
18
	 * @return Jam_Query_Builder_Join
19
	 */
20 43
	public static function factory($model, $type = NULL)
21
	{
22 43
		return new Jam_Query_Builder_Join($model, $type);
23
	}
24
25
	protected $_joins = array();
26
27
	protected $_context_model = NULL;
28
29
	protected $_model = NULL;
30
31
	protected $_end = NULL;
32
33 4
	public function join($model, $type = NULL)
34
	{
35 4
		$this->_joins[] = Jam_Query_Builder::resolve_join($model, $type, $this->model() ? $this->model() : $this->_table);
36
37 4
		return $this;
38
	}
39
40 1
	public function join_nested($model, $type = NULL)
41
	{
42 1
		$join = Jam_Query_Builder::resolve_join($model, $type, $this->model() ? $this->model() : $this->_table)
43 1
			->end($this);
44
45 1
		$this->_joins[] = $join;
46 1
		return $join;
47
	}
48
49 10
	public function join_table($model, $type = NULL)
50
	{
51 10
		$join = Jam_Query_Builder::resolve_join($model, $type, $this->_table, FALSE)
52 10
			->end($this);
53
54 10
		$this->_joins[] = $join;
55 10
		return $join;
56
	}
57
58 55
	public function compile($db = NULL)
59
	{
60 55
		if ($this->context_model() AND $meta = Jam::meta(Jam_Query_Builder::aliased_model($this->context_model())))
61 55
		{
62 51
			$db = Database::instance($meta->db());
0 ignored issues
show
Bug introduced by
It seems like $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...
63 51
		}
64
65 55
		$original_on = $this->_on;
66 55
		$original_using = $this->_using;
67 55
		$original_table = $this->_table;
68
69 55
		if ( ! empty($this->_on))
70 55
		{
71 52
			foreach ($this->_on as & $condition)
72
			{
73 52
				$condition[0] = Jam_Query_Builder::resolve_attribute_name($condition[0], $this->model() ? $this->model() : $this->_table);
74 52
				$condition[2] = Jam_Query_Builder::resolve_attribute_name($condition[2], $this->context_model());
75 52
			}
76 52
		}
77 55
		$this->_table = Jam_Query_Builder::resolve_table_alias($this->_table);
78
79 55
		if ( ! empty($this->_using))
80 55
		{
81
			foreach ($this->_using as & $column)
82
			{
83
				$column = Jam_Query_Builder::resolve_attribute_name($column, $this->meta());
0 ignored issues
show
Bug introduced by
The method meta() does not seem to exist on object<Kohana_Jam_Query_Builder_Join>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
			}
85
		}
86
87 55
		$additional_joins = '';
88 55
		foreach ($this->_joins as $join)
89
		{
90 14
			$additional_joins .= ' '.$join->compile($db);
91 55
		}
92
93 55
		$compiled = parent::compile($db).$additional_joins;
94 55
		$this->_on = $original_on;
95 55
		$this->_using = $original_using;
96 55
		$this->_table = $original_table;
97
98 55
		return $compiled;
99
	}
100
101 57
	public function context_model($context_model = NULL)
102
	{
103 57
		if ($context_model !== NULL)
104 57
		{
105 53
			$this->_context_model = $context_model;
106 53
			return $this;
107
		}
108 55
		return $this->_context_model;
109
	}
110
111 52
	public function model($model = NULL)
112
	{
113 52
		if ($model !== NULL)
114 52
		{
115 10
			$this->_model = $model;
116 10
			return $this;
117
		}
118 52
		return $this->_model;
119
	}
120
121 23
	public function end(Database_Query_Builder $end = NULL)
122
	{
123 23
		if ($end !== NULL)
124 23
		{
125 23
			$this->_end = $end;
126 23
			return $this;
127
		}
128 23
		return $this->_end;
129
	}
130
131 37
	public function __toString()
132
	{
133
		try
134
		{
135
			// Return the SQL string
136 37
			return $this->compile();
137
		}
138
		catch (Exception $e)
139
		{
140
			return Kohana_Exception::text($e);
141
		}
142
	}
143
144
} // End Kohana_Jam_Association
145