Test Failed
Push — master ( ccf43a...ee4cc3 )
by Jean-Christophe
17:42
created

DAOBulkUpdatesTrait::insertGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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.3
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($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($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
	public static function updateGroups($count = 5) {
88
		$bulks = self::$bulks ['update'];
89
		foreach ( $bulks as $bulk ) {
90
			$bulk->groupOp ( $count );
91
		}
92
	}
93
94
	public static function insertGroups($count = 5) {
95
		$bulks = self::$bulks ['insert'];
96
		foreach ( $bulks as $bulk ) {
97
			$bulk->groupOp ( $count );
98
		}
99
	}
100
101
	/**
102
	 * Executes all waiting update operations
103
	 */
104
	public static function flushUpdates(): void {
105
		$bulks = self::$bulks ['update'];
106
		foreach ( $bulks as $bulk ) {
107
			$bulk->flush ();
108
		}
109
	}
110
111
	/**
112
	 * Adds an instance in the bulk list of objects to delete.
113
	 * Call flush to commit the operation
114
	 *
115
	 * @param object $instance
116
	 */
117
	public static function toDelete($instance): void {
118
		self::toOperation ( $instance, 'delete' );
119
	}
120
121
	/**
122
	 * Adds an array of instances in the bulk list of objects to delete.
123
	 * Call flush to commit the operation
124
	 *
125
	 * @param array $instances
126
	 */
127
	public static function toDeletes(array $instances): void {
128
		self::toOperations ( $instances, 'delete' );
129
	}
130
131
	/**
132
	 * Executes all waiting delete operations
133
	 */
134
	public static function flushDeletes(): void {
135
		$bulks = self::$bulks ['delete'];
136
		foreach ( $bulks as $bulk ) {
137
			$bulk->flush ();
138
		}
139
	}
140
141
	/**
142
	 * Executes all waiting operations (inserts, updates, deletes)
143
	 */
144
	public static function flush(): void {
145
		foreach ( self::$bulks as $bulks ) {
146
			foreach ( $bulks as $bulk ) {
147
				$bulk->flush ();
148
			}
149
		}
150
	}
151
}
152
153