VirtualTables   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 6
eloc 14
c 6
b 1
f 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 2 1
A add() 0 3 1
A get() 0 13 4
1
<?php
2
namespace Kir\MySQL\Tools;
3
4
use Closure;
5
use Kir\MySQL\Builder\Select;
6
7
class VirtualTables {
8
	/** @var array<string, Closure|Select> */
9
	private array $virtualTables = [];
10
11
	/**
12
	 * @param string $tableName
13
	 * @param Select|Closure $select
14
	 * @return $this
15
	 */
16
	public function add(string $tableName, $select) {
17
		$this->virtualTables[$tableName] = $select;
18
		return $this;
19
	}
20
21
	/**
22
	 * @param string|VirtualTable $tableName
23
	 * @return bool
24
	 */
25
	public function has($tableName): bool {
26
		return array_key_exists((string) $tableName, $this->virtualTables);
27
	}
28
29
	/**
30
	 * @param string|VirtualTable $tableName
31
	 * @return Select|null
32
	 */
33
	public function get($tableName): ?Select {
34
		if($this->has($tableName)) {
35
			$table = $this->virtualTables[(string) $tableName];
36
			if($table instanceof Closure) {
37
				if($tableName instanceof VirtualTable) {
38
					$params = $tableName->getParams();
39
					return $table($params);
40
				}
41
				return $table();
42
			}
43
			return $table;
44
		}
45
		return null;
46
	}
47
}
48