Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

DAOBulkUpdatesTrait::toInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
/**
6
 * Ubiquity\orm\traits$DAOBulkUpdatesTrait
7
 * This class is part of Ubiquity
8
 *
9
 * @author jcheron <[email protected]>
10
 * @version 1.0.1
11
 *
12
 */
13
trait DAOBulkUpdatesTrait {
14
	protected static $bulks = [ 'insert' => [ ],'update' => [ ],'delete' => [ ] ];
15
16
	protected static function getBulk($class, $operation = 'update') {
17
		if (! isset ( self::$bulks [$operation] [$class] )) {
18
			$bulkClass = '\\Ubiquity\\orm\\bulk\\Bulk' . \ucfirst ( $operation ) . 's';
19
			self::$bulks [$operation] [$class] = new $bulkClass ( $class );
20
		}
21
		return self::$bulks [$operation] [$class];
22
	}
23
24
	protected static function toOperation($instance, string $operation): void {
25
		$class = \get_class ( $instance );
26
		self::getBulk ( $class, $operation )->addInstance ( $instance );
27
	}
28
29
	protected static function toOperations(array $instances, string $operation): void {
30
		$instance = \current ( $instances );
31
		if (isset ( $instance )) {
32
			$class = \get_class ( $instance );
33
			self::getBulk ( $class, $operation )->addInstances ( $instances );
34
		}
35
	}
36
37
	/**
38
	 * Adds an instance in the bulk list of objects to insert.
39
	 * Call flush to commit the operation
40
	 *
41
	 * @param object $instance
42
	 */
43
	public static function toInsert(object $instance): void {
44
		self::toOperation ( $instance, 'insert' );
45
	}
46
47
	/**
48
	 * Adds an array of instances in the bulk list of objects to insert.
49
	 * Call flush to commit the operation
50
	 *
51
	 * @param array $instances
52
	 */
53
	public static function toInserts(array $instances): void {
54
		self::toOperations ( $instances, 'insert' );
55
	}
56
57
	/**
58
	 * Executes all waiting insert operations
59
	 */
60
	public static function flushInserts(): void {
61
		$bulks = self::$bulks ['insert'];
62
		foreach ( $bulks as $bulk ) {
63
			$bulk->flush ();
64
		}
65
	}
66
67
	/**
68
	 * Adds an instance in the bulk list of objects to update.
69
	 * Call flush to commit the operation
70
	 *
71
	 * @param object $instance
72
	 */
73
	public static function toUpdate(object $instance): void {
74
		self::toOperation ( $instance, 'update' );
75
	}
76
77
	/**
78
	 * Adds an array of instances in the bulk list of objects to update.
79
	 * Call flush to commit the operation
80
	 *
81
	 * @param array $instances
82
	 */
83
	public static function toUpdates(array $instances): void {
84
		self::toOperations ( $instances, 'update' );
85
	}
86
87
	/**
88
	 * Executes all waiting update operations
89
	 */
90
	public static function flushUpdates(): void {
91
		$bulks = self::$bulks ['update'];
92
		foreach ( $bulks as $bulk ) {
93
			$bulk->flush ();
94
		}
95
	}
96
97
	/**
98
	 * Adds an instance in the bulk list of objects to delete.
99
	 * Call flush to commit the operation
100
	 *
101
	 * @param object $instance
102
	 */
103
	public static function toDelete(object $instance): void {
104
		self::toOperation ( $instance, 'delete' );
105
	}
106
107
	/**
108
	 * Adds an array of instances in the bulk list of objects to delete.
109
	 * Call flush to commit the operation
110
	 *
111
	 * @param array $instances
112
	 */
113
	public static function toDeletes(array $instances): void {
114
		self::toOperations ( $instances, 'delete' );
115
	}
116
117
	/**
118
	 * Executes all waiting delete operations
119
	 */
120
	public static function flushDeletes(): void {
121
		$bulks = self::$bulks ['delete'];
122
		foreach ( $bulks as $bulk ) {
123
			$bulk->flush ();
124
		}
125
	}
126
127
	/**
128
	 * Executes all waiting operations (inserts, updates, deletes)
129
	 */
130
	public static function flush(): void {
131
		foreach ( self::$bulks as $bulks ) {
132
			foreach ( $bulks as $bulk ) {
133
				$bulk->flush ();
134
			}
135
		}
136
	}
137
}
138
139