Passed
Push — master ( f461b3...0c0438 )
by Jean-Christophe
10:20
created

DAOBulkUpdatesTrait::toInserts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 5
	protected static function getBulk($class, $operation = 'update') {
17 5
		if (! isset ( self::$bulks [$operation] [$class] )) {
18 2
			$bulkClass = '\\Ubiquity\\orm\\bulk\\Bulk' . \ucfirst ( $operation ) . 's';
19 2
			self::$bulks [$operation] [$class] = new $bulkClass ( $class );
20
		}
21 5
		return self::$bulks [$operation] [$class];
22
	}
23
24 5
	protected static function toOperation($instance, string $operation): void {
25 5
		$class = \get_class ( $instance );
26 5
		self::getBulk ( $class, $operation )->addInstance ( $instance );
27 5
	}
28
29 4
	protected static function toOperations(array $instances, string $operation): void {
30 4
		$instance = \current ( $instances );
31 4
		if (isset ( $instance )) {
32 4
			$class = \get_class ( $instance );
33 4
			self::getBulk ( $class, $operation )->addInstances ( $instances );
34
		}
35 4
	}
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 3
	public static function toInsert($instance): void {
44 3
		self::toOperation ( $instance, 'insert' );
45 3
	}
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 2
	public static function toInserts(array $instances): void {
54 2
		self::toOperations ( $instances, 'insert' );
55 2
	}
56
57
	/**
58
	 * Executes all waiting insert operations
59
	 */
60 1
	public static function flushInserts(): void {
61 1
		$bulks = self::$bulks ['insert'];
62 1
		foreach ( $bulks as $bulk ) {
63 1
			$bulk->flush ();
64
		}
65 1
	}
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 3
	public static function toUpdate($instance): void {
74 3
		self::toOperation ( $instance, 'update' );
75 3
	}
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 2
	public static function toUpdates(array $instances): void {
84 2
		self::toOperations ( $instances, 'update' );
85 2
	}
86
87 1
	public static function updateGroups($count = 5) {
88 1
		$bulks = self::$bulks ['update'];
89 1
		foreach ( $bulks as $bulk ) {
90 1
			$bulk->groupOp ( $count );
91
		}
92 1
	}
93
94 1
	public static function insertGroups($count = 5) {
95 1
		$bulks = self::$bulks ['insert'];
96 1
		foreach ( $bulks as $bulk ) {
97 1
			$bulk->groupOp ( $count );
98
		}
99 1
	}
100
101 1
	public static function deleteGroups($count = 5) {
102 1
		$bulks = self::$bulks ['delete'];
103 1
		foreach ( $bulks as $bulk ) {
104 1
			$bulk->groupOp ( $count );
105
		}
106 1
	}
107
108
	/**
109
	 * Executes all waiting update operations
110
	 */
111 1
	public static function flushUpdates(): void {
112 1
		$bulks = self::$bulks ['update'];
113 1
		foreach ( $bulks as $bulk ) {
114 1
			$bulk->flush ();
115
		}
116 1
	}
117
118
	/**
119
	 * Adds an instance in the bulk list of objects to delete.
120
	 * Call flush to commit the operation
121
	 *
122
	 * @param object $instance
123
	 */
124 1
	public static function toDelete($instance): void {
125 1
		self::toOperation ( $instance, 'delete' );
126 1
	}
127
128
	/**
129
	 * Adds an array of instances in the bulk list of objects to delete.
130
	 * Call flush to commit the operation
131
	 *
132
	 * @param array $instances
133
	 */
134 2
	public static function toDeletes(array $instances): void {
135 2
		self::toOperations ( $instances, 'delete' );
136 2
	}
137
138
	/**
139
	 * Executes all waiting delete operations
140
	 */
141 1
	public static function flushDeletes(): void {
142 1
		$bulks = self::$bulks ['delete'];
143 1
		foreach ( $bulks as $bulk ) {
144 1
			$bulk->flush ();
145
		}
146 1
	}
147
148
	/**
149
	 * Executes all waiting operations (inserts, updates, deletes)
150
	 */
151 1
	public static function flush(): void {
152 1
		foreach ( self::$bulks as $bulks ) {
153 1
			foreach ( $bulks as $bulk ) {
154 1
				$bulk->flush ();
155
			}
156
		}
157 1
	}
158
}
159
160