Completed
Push — master ( aef970...c04073 )
by Ron
02:26
created

UnionBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 1
cbo 1
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A union() 0 4 1
A unionAll() 0 4 1
A buildUnions() 0 20 4
1
<?php
2
namespace Kir\MySQL\Builder\Traits;
3
4
trait UnionBuilder {
5
	use AbstractDB;
6
7
	/** @var array */
8
	private $unions = [];
9
10
	/**
11
	 * @param string $query
12
	 * @return $this
13
	 */
14
	public function union($query) {
15
		$this->unions[] = array('', $query);
16
		return $this;
17
	}
18
19
	/**
20
	 * @param string $query
21
	 * @return $this
22
	 */
23
	public function unionAll($query) {
24
		$this->unions[] = array('ALL', $query);
25
		return $this;
26
	}
27
28
	/**
29
	 * @param string $query
30
	 * @return string
31
	 */
32
	protected function buildUnions($query) {
33
		$wrap = function ($query) {
34
			$query = trim($query);
35
			$query = join("\n\t", explode("\n", $query));
36
			return sprintf("(\n\t%s\n)", $query);
37
		};
38
		$queries = [$wrap($query)];
39
		foreach($this->unions as $unionQuery) {
40
			if($unionQuery[0] === 'ALL') {
41
				$queries[] = 'UNION ALL';
42
			} else {
43
				$queries[] = 'UNION';
44
			}
45
			$queries[] = $wrap($unionQuery[1]);
46
		}
47
		if(count($queries) > 1) {
48
			return join(" ", $queries);
49
		}
50
		return $query;
51
	}
52
}
53